2020-07-14TCP
2020-07-15 本文已影响0人
智障猿
TCP通信原理
TCP协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一旦建立虚拟的网络通信,程序的两端就可以通过虚拟链路进行通信。
java对基于TCP协议的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生IO流来进行网络通信。
JAVA为客户提供Socket类,为服务器端提供ServerSocket类
TCP发送数据
- 步骤
①创建客户端的Socket对象(Socket(String host,int port))
②获取输出流,写数据(OutputStream getOutputStream())
③释放资源
void close()
public class TCPSend {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket socket = new Socket(InetAddress.getByName("DESKTOP-0NL0JDK"),10086);
// Socket socket = new Socket("DESKTOP-0NL0JDK",10086);
//获得输出流,写数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,tcp".getBytes());
//释放资源
outputStream.close();
}
}
TCP接收数据
①创建服务器端的socket对象(ServerSocket(int port))
②获取客户端连接,返回一个Socket对象(Socket accept())
③获取输入流,读数据,并把数据显示在控制台(InputStream getInputStream())
④释放资源(void close())
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象
//ServerSocket(int port)创建绑定指定端口的服务器套接字
ServerSocket serverSocket = new ServerSocket(10086);
//Socket accept()倾听要链接到此套接字并接受它
Socket socket = serverSocket.accept();
//获取输入流,读数据,并把数据显示在控制台‘
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
String data = new String(bytes,0,len);
System.out.println("数据是:"+data);
serverSocket.close();
socket.close();
}
带有反馈和接收反馈的客户端和服务端
public class Send {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("DESKTOP-0NL0JDK"), 10000);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello".getBytes());
//接收服务器的反馈
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
String s = new String(bytes,0,len);
System.out.println("服务端"+s);
socket.close();
}
}
public class Receive {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10000);
Socket s = serverSocket.accept();
InputStream inputStream = s.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
String str = new String(bytes,0,len);
System.out.println("客户端发送来的数据为"+str);
//向客户端发送反馈信息
OutputStream outputStream = s.getOutputStream();
outputStream.write("服务器接收到数据".getBytes());
serverSocket.close();
}
}
练习
- 客户端:数据来自文本文件,接收服务器反馈
- 服务器:接收到的数据写入文本文件,给出反馈
- 出现问题:程序一直等待
- 原因:读数据的方法是阻塞式的
- 解决办法:
①自定义结束标记
②使用shutdownOutput()方法
public class Send {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("DESKTOP-0NL0JDK"), 10000);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader bufferedReader = new BufferedReader(new FileReader("Test\\zzt.txt"));
String line;
while((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
socket.shutdownOutput();
//接收服务器反馈
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line1 = bufferedReader1.readLine();
System.out.println(line1);
socket.close();
}
}
public class Receiver {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10000);
Socket accept = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("Test\\zzt3.txt"));
String line;
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
BufferedWriter bufferedWriter1 = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bufferedWriter1.write("文件上传成功!");
bufferedWriter1.newLine();
bufferedWriter1.flush();
serverSocket.close();
}
}