### TCP协议 TCP是面向连接的运输层协议,比较复杂,Application 在使用 TCP 协议前必须先建立连接,才能传输数据,数据传输完毕之后需要释放已经建立的连接。 Java 通过 Socket 来完成 TCP 程序的开发,Socket 是一个类,使用该类就可以在服务端和客户端之间建立起可靠的连接。 实际开发中,使用 Socket 表示客户端,使用 ServerSocket 表示服务端,ServerSocket 也是一个类,ServerSocket 和 Socket 都存放在 java.net 包中。 具体的开发思路是在服务端创建 ServerSocket 对象,然后通过该对象的 accept() 方法接收若干个 Socket 对象。 ####ServerSocket 常用方法 public ServerSocket(int port) 根据端口创建 ServerSocket 对象 public ServerSocket(int port,int backlog) 根据端口和 backlog 创建 ServerSocket 对象 public ServerSocket(int port,int backlog,InetAddress address) 根据端口、backlog、InetAddress 创建 ServerSocket 对象 public ServerSocket() 创建一个没有绑定服务端的 ServerSocket 对象 pubilc synchronized int getSoTimeout() 获取 Sotimeout public InetAddress getInetAddress() 获取服务器的 IP 地址 public Socket accept() 等待客户端请求,并返回 Socket 对象 public void close() 关闭 ServerSocket public boolean isClosed() 返回 ServerSocket 的关闭状态 public void bind(SocketAddress endpoint) 将 ServerSocket 对象绑定到指定地址 public int getLocalPort() 获取 ServerSocket 端口 ####Socket 常用方法 public Socket(String host,int port) 根据主机、端口创建要连接的 Socket 对象 public Socket(InetAddress host,int port) 根据 IP 地址,端口创建要连接的 Socket 对象 public Socket(String host,int port,InetAddress address,int localPort) 根据主机、端口创建要连接的 Socket 对象并将其连接到指定的远程主机上的指定端口 public Socket(InetAddress host,int port,InetAddress localAddress,int localPort) 根据主机、端口创建要连接的 Socket 对象并将其连接到指定的远程主机上的指定端口 public Socket() 创建没有连接的 Socket 对象 public InputStream getInputStream() 返回 Socket 的输入流 pulic void close() 关闭 Socket pulic boolean isClosed() 返回 Socket 的关闭状态 TCP ```java import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; OutputStream outputStream = null; InputStream inputStream = null; DataInputStream dataInputStream = null; DataOutputStream dataOutputStream = null; try { serverSocket = new ServerSocket(8080); System.out.println("------服务端------"); while(true) { socket = serverSocket.accept(); inputStream = socket.getInputStream(); dataInputStream = new DataInputStream(inputStream); String request = dataInputStream.readUTF(); System.out.println("接收到了客户端请求:"+request); outputStream = socket.getOutputStream(); dataOutputStream = new DataOutputStream(outputStream); String response = "Hello World"; dataOutputStream.writeUTF(response); System.out.println("给客户端作出响应:"+response); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` ```java import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { Socket socket = null; OutputStream outputStream = null; DataOutputStream dataOutputStream = null; InputStream inputStream = null; DataInputStream dataInputStream = null; try { socket = new Socket("127.0.0.1", 8080); System.out.println("------客户端------"); String request = "你好"; System.out.println("客户端说:"+request); outputStream = socket.getOutputStream(); dataOutputStream = new DataOutputStream(outputStream); dataOutputStream.writeUTF(request); inputStream = socket.getInputStream(); dataInputStream = new DataInputStream(inputStream); String response = dataInputStream.readUTF(); System.out.println("服务端响应:"+response); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ```
27-2.txt
UDP协议 TCP协议可以建立稳定可靠的连接,保证数据的完整。 缺点:会造成系统运行效率低下。 UDP协议的特点是不可靠的连接, 不需要建立连接,直接发送数据即可。 Java 提供了 DatagramSocket 和 DatagramPacket 完成基于 UDP 协议的开发。 DatagramSocket的常用方法 public DatagramSocket(int port) 根据端口创建 DatagramSocket 对象 public void send(DatagramPacket p) 发送数据 public synchronized void receive(DatagramPacket p) 接收数据 public InetAddress getIneAddress() 获取DatagramSocket对应的InetAddress对象 public boolean isConnected() 判断是否连接到服务 DatagramPacket的常用方法 public DatagramPacket(byte buf[],int length,InetAddress address,int port) 根据发送的数据,数据长度,IP地址,端口,创建DatagramPacket对象 public synchronized byte[] getData() 获取接收的数据 public synchronized int getLength() 获取数据的长度 public synchronized int getPort() 获取发送数据的 Socket 端口 public synchronized SocketAddress getSocketAddress() 获取发送数据的 Socket 信息
Client.java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { Socket socket = null; OutputStream outputStream = null; DataOutputStream dataOutputStream = null; InputStream inputStream = null; DataInputStream dataInputStream = null; try { socket = new Socket("127.0.0.1", 8080); System.out.println("------客户端------"); String request = "你好"; System.out.println("客户端说:"+request); outputStream = socket.getOutputStream(); dataOutputStream = new DataOutputStream(outputStream); dataOutputStream.writeUTF(request); inputStream = socket.getInputStream(); dataInputStream = new DataInputStream(inputStream); String response = dataInputStream.readUTF(); System.out.println("服务端响应:"+response); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Server.java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; OutputStream outputStream = null; InputStream inputStream = null; DataInputStream dataInputStream = null; DataOutputStream dataOutputStream = null; try { serverSocket = new ServerSocket(8080); System.out.println("------服务端------"); while(true) { socket = serverSocket.accept(); inputStream = socket.getInputStream(); dataInputStream = new DataInputStream(inputStream); String request = dataInputStream.readUTF(); System.out.println("接收到了客户端请求:"+request); outputStream = socket.getOutputStream(); dataOutputStream = new DataOutputStream(outputStream); String response = "Hello World"; dataOutputStream.writeUTF(response); System.out.println("给客户端作出响应:"+response); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Test.java
import java.net.DatagramSocket; import java.net.SocketException; public class Test { public static void main(String[] args) { try { DatagramSocket datagramSocket = new DatagramSocket(8181); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }