这里我要说一个三次握手、四次挥手
三次握手:
1:A——>B A向B发送连接请求
2:A<——B B向A发送确认连接请求
3:A——>B A向B发送确定连接
以上便是三次握手
断开连接时的四次挥手:
1:A——>B A向B发送断开请求
2:A<——B B向A发送确认断开请求
3:A<——B B向A发送已断开
4:A——>B A向B发送已断开
服务端代码:
package ip;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName Server
* @Author 瞿肖
* @Date 2022/7/12 16:00
*/
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream bos = null;
try {
serverSocket = new ServerSocket(9999);//创建端口
while (true) {//一直接收
socket = serverSocket.accept();//等待客户端连接
is = socket.getInputStream();//获取输出流
bos = new ByteArrayOutputStream();//管道流,避免1024获取完了时,是一个汉字(因为汉字有三个字节,如果没有全部获取,就会出现乱码)
byte[] arr = new byte[1024];
int len = 0;
while ((len = is.read(arr)) != -1) {
bos.write(arr, 0, len);
}
System.out.println(bos);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
}
if (socket != null) {
socket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
一直接收数据
客户端代码:
package ip;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
/**
* @ClassName client
* @Author 瞿肖
* @Date 2022/7/12 15:59
*/
public class client {
public static void main(String[] args) {
InetAddress ip;
Socket socket = null;
OutputStream os = null;
try {
ip = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(ip, port);//建立连接服务端
os = socket.getOutputStream();
os.write("你好,世界!".getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (os != null) {
os.close();//一定记得关闭资源,因为这个方法会刷新写入的数据,如果不刷新,服务端就没数据,会报错
}
if (socket != null) {
socket.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
运行代码传输一
传输图片案例:
服务端代码:
package ip;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
/**
* @ClassName Server
* @Author 瞿肖
* @Date 2022/7/12 16:00
*/
public class Server {
public static void main(String[] args) throws Exception {
//1.创建连接,端口
ServerSocket serverSocket = new ServerSocket(9999);
//2.等待客户端连接
Socket socket = serverSocket.accept();
//3.获取文件
InputStream is = socket.getInputStream();
//4.写文件
FileOutputStream fos = new FileOutputStream("D:\\我的电脑\\asd\\abc\\b.jpg");
byte[] arr = new byte[1024];
int len = 0;
while ((len = is.read(arr)) != -1) {
fos.write(arr, 0, len);
}
//5.返回接收到消息
OutputStream os = socket.getOutputStream();
os.write("我已经接收到了,你可以关闭了".getBytes(StandardCharsets.UTF_8));
//6.关闭资源
os.close();
is.close();
socket.close();
serverSocket.close();
}
}
客服端代码:
package ip;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
/**
* @ClassName client
* @Author 瞿肖
* @Date 2022/7/12 15:59
*/
public class client {
public static void main(String[] args) throws Exception {
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 9999;
//1.建立连接服务端
Socket socket = new Socket(ip, port);
//2.传输数据
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream("D:\\我的电脑\\asd\\abc\\a.jpg");
byte[] arr = new byte[1024];
int len = 0;
while ((len = fis.read(arr)) != -1) {
//将数据传出
os.write(arr, 0, len);
}
socket.shutdownOutput();//结束客服端的输出
//3.接受服务端的反馈
InputStream is = socket.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
len = 0;
arr = new byte[1024];
while ((len = is.read(arr)) != -1) {
bos.write(arr, 0, len);
}
System.out.println(bos);
//4.关闭资源
fis.close();
os.close();
socket.close();
}
}