TCP编程模型
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/078bf73b1b4a6a4dbad2f70c847a766e.png)
TCP粘包
TCP协议实现网络通信
客户端
package tcp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
socket.connect(new InetSocketAddress("localhost",20001));
System.out.println("已建立连接");
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,i m client".getBytes());
socket.shutdownOutput();
System.out.println("数据写出完毕");
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1014];
int len = -1;
while((len=inputStream.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
System.out.println("服务器返回数据完毕");
}
socket.shutdownInput();
socket.close();
}
}
服务器端
package tcp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("localhost",20001));
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len=inputStream.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
accept.shutdownInput();
OutputStream os =accept.getOutputStream();
os.write("你也好".getBytes());
accept.shutdownOutput();
serverSocket.close();
}
}
引入多线程解决阻塞问题
客户端
package tcp;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
public class clientNonBlock {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
socket.connect(new InetSocketAddress("localhost",20001));
System.out.println("已建立连接");
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
OutputStream outputStream = socket.getOutputStream();
String s = "client>" + new BufferedReader(new InputStreamReader(System.in)).readLine();
outputStream.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[1014];
int len = -1;
try {
InputStream inputStream = socket.getInputStream();
while ((len = inputStream.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
服务器端
package tcp;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class serverNonBlock {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("localhost",20001));
Socket accept = serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] buffer = new byte[1024];
int len = -1;
InputStream inputStream = accept.getInputStream();
while((len=inputStream.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
OutputStream os = null;
try {
os = accept.getOutputStream();
while(true) {
String server_msg = "server>" + new BufferedReader(new InputStreamReader(System.in)).readLine();
os.write(server_msg.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}