Socket简介
Socket是两台主机之间的一个连接。它可以完成7个基本操作:
- 连接远程机器
- 发送数据
- 接收数据
- 关闭连接
- 绑定端口
- 监听入站数据
- 在绑定端口上接受来自远程机器的连接
Socket通信流程
实例
TCP实现
- 服务端代码
public class TcpServer {
public static void main(String[] args) throws Exception {
// 创建socket 并将端口绑定在7000端口
ServerSocket serverSocket = new ServerSocket(7000);
while (true){
//监听7000 端口,直至客户端返回连接信息后才返回
Socket socket = serverSocket.accept();
new LengthCalculator(socket).start();
}
}
}
public class LengthCalculator extends Thread {
private Socket socket;
public LengthCalculator(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//获取socket输出流
OutputStream os = socket.getOutputStream();
//获取socket输入流
InputStream is = socket.getInputStream();
int ch = 0;
// 将接收流的byte数组转换成字符串,这里获取的内容是客户端发来的字符串,ch读取字符串的长度
byte[] buff = new byte[1024];
ch = is.read(buff);
String content = new String(buff, 0, ch);
System.out.println(content);
//输出流写入字符串的长度,发送给客户端
os.write(String.valueOf(content.length()).getBytes());
//关闭输入输出流以及socket
is.close();
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}
- 客户端代码
public class TcpClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 7000);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
os.write(new String("hello world").getBytes());
int ch = 0;
byte[] buff = new byte[1024];
ch = is.read(buff);
String content = new String(buff, 0, ch);
System.out.println(content);
is.close();
os.close();
socket.close();
}
}
UDP实现效果
- UDP服务端
public class UdpServer {
public static void main(String[] args) throws Exception {
// 服务端接收客户端发送的数据报
DatagramSocket socket = new DatagramSocket(7000);
byte[] buff = new byte[1024];
// 接收客户端发送过来的内,并将其内容封装进 DatagramPacket对象中
DatagramPacket packet = new DatagramPacket(buff, buff.length);
socket.receive(packet);
byte[] data = packet.getData();
// 将数据从二进制转换成字符串形式
String content = new String(data, 0, packet.getLength());
System.out.println(content);
// 将要发送给客户端的数据转换成二进制
byte[] sendContent = String.valueOf(content.length()).getBytes();
// 从DatagramPacket中获取数据的来源地和端口号
DatagramPacket packetToClient = new DatagramPacket(sendContent, sendContent.length,
packet.getAddress(), packet.getPort());
// 发送数据给客户端
socket.send(packetToClient);
}
}
- UDP客户端
public class UdpClient {
public static void main(String[] args) throws Exception {
// 客户端发送数据报给服务端
DatagramSocket socket = new DatagramSocket();
// 发送给服务器的数据
byte[] buf = "Hello World".getBytes();
// 将IP地址封装成InetAddress对象
InetAddress address = InetAddress.getByName("127.0.0.1");
// 将要发送给服务端的数据封装成DatagramPacket对象,填入Ip地址和端口号
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 7000);
//发送数据给服务端
socket.send(packet);
// 客户端接收服务端发送来的数据报
byte[] data = new byte[100];
//创建DatagramPacket 对象用来储存服务端发送过来的数据
DatagramPacket receivePacket = new DatagramPacket(data, data.length);
socket.receive(receivePacket);
String content = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println(content);
}
}
- 效果图