Java网络编程之Socket
什么是Socket
通常也称作”套接字“,用于描述IP地址
和端口
,是一个通信链的句柄。应用程序通常通过”套接字”向网络发出请求或者应答网络请求。
TCP与UDP的区别
- TCP是一种面向连接的保证可靠传输的协议,可靠,效率稍低;
- UDP是一种无连接的协议,不可靠,效率高;
- 建立起一个TCP连接需要经过“三次握手”;
- TCP传输数据没有大小限制;
- UDP的每个数据报中都给出了完整的地址信息;
- UDP传输数据时是有大小限制的,每个被传输的数据报必须限定在64KB之内;
TCP连接的三次握手
Java Udp实现
服务端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class UdpServer {
private Integer port;
public UdpServer(Integer port) { this.port = port; }
public void start() throws IOException { System.out.println("UDP服务端启动"); DatagramSocket socket = new DatagramSocket(this.port); byte[] bytes = new byte[1024]; DatagramPacket packet = new DatagramPacket(bytes, bytes.length); socket.receive(packet); String data = new String(packet.getData(), packet.getLength()); System.out.println("服务端收到数据:" + data); socket.close(); } }
|
客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class UdpClient {
private Integer port;
public UdpClient(Integer port) { this.port = port; }
public void start(String targetIp, Integer targetPort) throws IOException { System.out.println("UDP客户端启动"); DatagramSocket socket = new DatagramSocket(this.port); String data = "hello world"; DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length, InetAddress.getByName(targetIp), targetPort); System.out.println("UDP客户端发送消息"); socket.send(packet); socket.close(); }
}
|
运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static void main(String[] args) throws IOException, InterruptedException { Integer serverPort = 15367; Integer clientPort = 15369; new Thread(new Runnable() { @SneakyThrows @Override public void run() { UdpServer server = new UdpServer(serverPort); server.start(); } }).start(); Thread.sleep(1000); new Thread(new Runnable() { @SneakyThrows @Override public void run() { UdpClient client = new UdpClient(clientPort); client.start("127.0.0.1", serverPort); } }).start(); }
|
Java Tcp实现
服务端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class TcpServer {
private Integer port;
public TcpServer(Integer port) { this.port = port; }
public void start() throws IOException { System.out.println("Tcp服务端启动"); ServerSocket serverSocket = new ServerSocket(this.port); try { Socket accept = serverSocket.accept(); InputStream is = accept.getInputStream(); byte[] bytes = new byte[1024]; int len = is.read(bytes); String data = new String(bytes, 0, len); System.out.println("服务端接收到数据:" + data); } catch (Exception e) { e.printStackTrace(); } finally { serverSocket.close(); } } }
|
客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class TcpClient {
@MetaData(name = "服务端ip") private String ip; @MetaData(name = "服务端端口") private Integer port;
public TcpClient(String ip, Integer port) { this.ip = ip; this.port = port; }
public void start() throws IOException { System.out.println("Tcp客户端启动"); Socket socket = new Socket(this.ip, this.port); try { System.out.println("Tcp客户端发送消息"); OutputStream os = socket.getOutputStream(); String data = "hello world"; os.write(data.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { socket.close(); } } }
|
运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static void main(String[] args) throws InterruptedException { Integer port = 16487; new Thread(new Runnable() { @SneakyThrows @Override public void run() { TcpServer server = new TcpServer(port); server.start(); } }).start(); Thread.sleep(1000); new Thread(new Runnable() { @SneakyThrows @Override public void run() { TcpClient client = new TcpClient("127.0.0.1", port); client.start(); } }).start(); }
|