1.IP地址
image.png
(本机地址 127.0.0.1)
2.端口号(常用端口号)
image.png
3.TCP概述
image.png
image.png
4.代码代码实现
image.png
public class TcpSeverSocket {
public static void main(String[] args) throws IOException {
//创建一个客户端
ServerSocket serverSocket = new ServerSocket(8888);
//获取客户端
Socket accept = serverSocket.accept();
//读取客户端的信息
InputStream inputStream = accept.getInputStream();
byte[] by = new byte[1024];
int read = inputStream.read(by);
System.out.println(new String(by,0,read));
//传给客户端
OutputStream outputStream = accept.getOutputStream();
outputStream.write("你好客户端".getBytes());
inputStream.close();
outputStream.close();
}
}
public class TcpSocket {
public static void main(String[] args) throws IOException {
//创建一个客户端 绑定IP和端口号
Socket socket = new Socket("127.0.0.1", 8888);
//传输
OutputStream out = socket.getOutputStream();
out.write("你好服务端".getBytes());
//接收服务端的信息
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
System.out.println(new String(bytes, 0, read));
out.close();
inputStream.close();
}
}
案例:从客户端上传图片到服务端的本机
堵塞问题与解决
image.png
image.png
BS案例分析
image.png
代码实现: