UDP是一种面向无连接的协议,因此在通信时,发送端和接收端不用建立连接。简单来说就是,发送端不管有没有存在接收端就发,接收端收到数据后也不会向发送端反馈是否收到数据。
UDP通信
DatagramPacket类将发送端或接收端的数据封装起来,DatagramSocket类可发送和接收DatagramPacket数据包。
接收端:
import java.net.*;
public class test {
public static void main(String[] args) throws Exception{
byte[] buf = new byte[1024];
DatagramSocket ds = new DatagramSocket(8955);
DatagramPacket dp = new DatagramPacket(buf, 1024);
System.out.println("waiting...");
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength()) + " from "
+ dp.getAddress()+ ":" + dp.getPort();
System.out.println(str);
ds.close();
}
}
运行一下:
发送端:
import java.net.*;
public class test {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(); //随机绑定一个端口
String str = "hello world";
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName("localhost"), 8955); //端口号与接收端一致
System.out.println("send message");
ds.send(dp);
ds.close();
}
}
运行一下:
接收端有回应:
TCP与UDP的区别在于,UDP只有发送端和接收端,不区分客户端和服务端,计算机之间可以任意地发送数据。而TCP严格区分服务端和客户端,在通信时,必须先由客户端去连接服务端才能实现通信,服务器不可以主动连接客户端,并且服务端程序需要事先启动,等待客户端连接。
TCP通信
有两个类用于实现TCP程序,一个是服务端ServerSocket类,另一个是客户端Socket类。
服务端:
import java.net.*;
import java.io.*;
class TCPServer {
private static final int PORT = 8954;
public void listen() throws Exception {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("start accept...");
Socket client = serverSocket.accept();
OutputStream os = client.getOutputStream();
System.out.println("start interact...");
os.write(("welcome!").getBytes());
InputStream is = client.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println("read "+new String(buf, 0, len)+" from client.");
System.out.println("session end.");
is.close();
os.close();
client.close();
serverSocket.close();
}
}
public class test {
public static void main(String[] args) throws Exception {
new TCPServer().listen();
}
}
运行:
客户端:
import java.net.*;
import java.io.*;
class TCPClient {
private static final int PORT = 8954;
public void connect() throws Exception {
Socket client = new Socket(InetAddress.getLocalHost(),PORT);
OutputStream os = client.getOutputStream();
os.write(("hello world").getBytes());
InputStream is = client.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf, 0, len));
os.close();
is.close();
client.close();
}
}
public class test {
public static void main(String[] args) throws Exception {
new TCPClient().connect();
}
}
运行:
马上服务端就有回应:
TCP多线程
多个客户端访问同一个服务端时,服务端会为每一个客户端创建一个对应的Socket,并且开启一个新的线程使两个Socket建立专线进行通信。
服务端:
import java.net.*;
import java.io.*;
class TCPServer {
private static final int PORT = 8954;
public void listen() throws Exception {
ServerSocket serverSocket = new ServerSocket(PORT);
while(true) {
final Socket client = serverSocket.accept();
new Thread() {
public void run() {
OutputStream os;
try {
os = client.getOutputStream();
System.out.println("start interact...");
os.write(("welcome!").getBytes());
Thread.sleep(1000);
System.out.println("session end.");
os.close();
client.close();
} catch(Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
}
public class test {
public static void main(String[] args) throws Exception {
new TCPServer().listen();
}
}