udp
import java.io.IOException;
import java.net.*;
public class UDPSocketCliet {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
String message = "hello udp";
byte[] bytes = message.getBytes();
InetAddress address = InetAddress.getLocalHost();
int port = 12345;
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, address, port);
datagramSocket.send(datagramPacket);
datagramSocket.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPSocketServer {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(12345);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
datagramSocket.receive(datagramPacket);
String hostAddress = datagramPacket.getAddress().getHostAddress();
byte[] data = datagramPacket.getData();
System.out.println("接收来自 " + hostAddress + "的信息:" + new String(data));
datagramSocket.close();
}
}
tcp
- 新建 TCPSocketClient
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class TCPSocketClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 12345);
OutputStream outputStream = socket.getOutputStream();
String message = "hello tcp";
outputStream.write(message.getBytes());
outputStream.close();
socket.close();
}
}
- 新建 TCPSocketServer
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPSocketServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(12345);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
byte[] bytes = new byte[1024];
int length = inputStream.read(bytes);
System.out.println(new String(bytes, 0, length));
inputStream.close();
accept.close();
serverSocket.close();
}
}
模拟客户端一直发送数据
- 新建客户端 TCPSocketClient
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TCPSocketClient {
private Socket socket;
private OutputStream outputStream;
public static void main(String[] args) {
TCPSocketClient tCPSocketClient = new TCPSocketClient();
tCPSocketClient.create();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
int number = scanner.nextInt();
System.out.println("输入的数据:" + number);
if(number == 1){
tCPSocketClient.sender();
}else if(number == 2){
tCPSocketClient.close();
break;
}
System.out.println("请输入一个整数:");
}
}
public void create(){
try{
socket = new Socket("127.0.0.1", 12345);
outputStream = socket.getOutputStream();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sender(){
try{
String message = "tcpclient02 hello tcp";
outputStream.write(message.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void close(){
try {
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 新建服务端
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPSocketServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(12345);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
byte[] bytes = new byte[1024];
int length = 0;
while((length=inputStream.read(bytes))>0){
System.out.println(new String(bytes, 0, length));
}
inputStream.close();
accept.close();
serverSocket.close();
}
}
|