java篇-DAY25-TCP协议
3.TCP通信程序
3.1 TCP通信原理
TCP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信 Java对基于TCP协议的的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生IO流来进行网络通信 Java为客户端提供了Socket类,为服务器端提供了ServerSocket类
3.2TCP发送数据
发送数据的步骤
①创建客户端的Socket对象 (Socket) Socket(String host,int port) ②获取输出流,写数据 OutputStream getOutputStream() ③释放资源 void close()
package javaDemo.NetDemo.Linkprotocol.TCP.Demo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1", 4399);
OutputStream os = socket.getOutputStream();
os.write("周杰伦YYDS".getBytes());
socket.close();
}
}
3.3 TCP接收数据
接收数据的步骤
①创建服务器端的Socket对象 (ServerSocket) ServerSocket(int port) ②监听客户端连接,返回一个Socket对象 Socket accept() ③获取输入流。读数据,并把数据显示在控制台 InputStream getInputStream() ④释放资源 void close()
package javaDemo.NetDemo.Linkprotocol.TCP.Demo;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4399);
Socket s = ss.accept();
InputStream is = s.getInputStream();
byte[] bytes = new byte[1024];
System.out.println("数据是:"+new String(bytes,0,is.read(bytes)));
s.close();
ss.close();
}
}
运行服务端后,运行客户端:
3.4 TCP通信程序练习
练习1
客户端:发送数据,接收服务器反馈 服务器:接收数据。给出反馈
ClientDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1", 4399);
OutputStream os = socket.getOutputStream();
os.write("周杰伦yyds!".getBytes());
InputStream is = socket.getInputStream();
byte[] bytes = new byte[1024];
int len=is.read(bytes);
String data=new String(bytes,0,len);
System.out.println("客户端"+data);
socket.close();
}
}
ServerDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4399);
Socket s = ss.accept();
InputStream is = s.getInputStream();
byte[] bytes = new byte[1024];
int len=is.read(bytes);
String data = new String(bytes, 0, len);
System.out.println("服务端:" + data);
OutputStream os = s.getOutputStream();
os.write("数据已收到".getBytes());
ss.close();
}
}
客户端控制台:
服务端控制台:
练习2
客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束 服务器:接收到的数据在控制台输出
ClientDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework2;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1",4399);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line="";
while ((line=br.readLine())!=null){
if ("886".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}socket.close();
}
}
ServerDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework2;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4399);
Socket s = serverSocket.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
serverSocket.close();
}
}
练习3
客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束 服务器:接收到的数据写入文本文件
ClientDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework3;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1",4399);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line="";
while ((line=br.readLine())!=null){
if ("886".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}socket.close();
}
}
ServerDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework3;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4399);
Socket s = serverSocket.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive1.txt"));
String line;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
serverSocket.close();
}
}
执行ServerDemo,然后执行client在client的控制台输入信息
打开对应的文本文件结果:
练习4
客户端:数据来自于文本文件 服务器:接收到的数据写入文本文件
ClientDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework4;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1",4399);
BufferedReader br = new BufferedReader(new FileReader("D:\\ideafile\\Bigdata\\data\\1.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line="";
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}socket.close();
}
}
ServerDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework4;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4399);
Socket s = serverSocket.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive.txt"));
String line;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
serverSocket.close();
}
}
执行Server等待客户端client输入,执行client输入从本地文件1.txt中获取的信息
Server将收到的信息写入到一个新的文本文件receive2.txt中
练习5
客户端:数据来自于文本文件,接收服务器反馈 服务器:接收到的数据写入文本文件,给出反馈 出现问题:程序一直等待 原因:读数据的方法是阻塞式的 解决办法:自定义结束标记;使用shutdownOutput(方法(推荐)
ClientDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework5;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1",4399);
BufferedReader br = new BufferedReader(new FileReader("D:\\ideafile\\Bigdata\\data\\1.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line="";
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
socket.shutdownOutput();
BufferedReader brC = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data=brC.readLine();
System.out.println("服务器反馈"+data);
socket.close();
}
}
ServerDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework5;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4399);
Socket s = serverSocket.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive3.txt"));
String line;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
BufferedWriter bwS = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwS.write("文件上传成功");
bwS.newLine();
bwS.flush();
serverSocket.close();
}
}
练习6
客户端:数据来自于文本文件,接收服务器反馈 服务器:接收到的数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程
ClientDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework6;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.142.1",4399);
BufferedReader br = new BufferedReader(new FileReader("D:\\ideafile\\Bigdata\\data\\1.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line="";
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
socket.shutdownOutput();
BufferedReader brC = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data=brC.readLine();
System.out.println("服务器反馈"+data);
socket.close();
}
}
ServerDemo.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework6;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4399);
while (true) {
Socket s = serverSocket.accept();
new Thread(new ServerThread(s)).start();
}
}}
ServerThread.java
package javaDemo.NetDemo.Linkprotocol.TCP.homework6;
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable{
private Socket s;
public ServerThread(Socket s) {
this.s=s;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
int count=0;
File file = new File("D:\\ideafile\\Bigdata\\data\\receive" + count + ".txt");
while (file.exists()){
count++;
file = new File("D:\\ideafile\\Bigdata\\data\\receive" + count + ".txt");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
BufferedWriter bwS = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwS.write("文件上传成功");
bwS.newLine();
bwS.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果:
练习7
需求:(TCP,对象流)搞个登录系统
User
package javaDemo.NetDemo.Linkprotocol.TCP.Homework7;
import java.io.Serializable;
public class User implements Serializable {
private String id;
private int password;
public User() {
}
public User(String id, int password) {
this.id = id;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
}
Client
package javaDemo.NetDemo.Linkprotocol.TCP.Homework7;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception{
Socket s = new Socket("127.0.0.1",10086);
while (true){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String id = scanner.next();
System.out.println("请输入密码");
int password = scanner.nextInt();
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(new User(id,password));
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("反馈信息:"+br.readLine());
System.out.println("----是否退出?----\n 1.继续\n 2.退出");
int exit = scanner.nextInt();
if (exit==2){
break;
}
}
s.close();
}
}
Server
package javaDemo.NetDemo.Linkprotocol.TCP.Homework7;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10086);
while (true){
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
User user=(User)ois.readObject();
if (!user.getId().equals("root")){
backsend1(s,1);
}else if (user.getId().equals("root")&&user.getPassword()!=123456){
backsend1(s,2);
}else{
backsend1(s,3);
}
ss.close();
}
}
public static void backsend1(Socket s,Integer i) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
switch (i){
case 1:
bw.write("用户名不存在");
bw.newLine();
bw.flush();
break;
case 2:
bw.write("密码错误");
bw.newLine();
bw.flush();
case 3:
bw.write("登录成功!");
bw.newLine();
bw.flush();
}
}
}
| | | | | | | |
下一章节-随缘更新
|