Mountain wind 2022-06-24 08:02:55 阅读数:982
Server side :
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
// 1. Server establishes communication ServerSocket
ServerSocket ss = new ServerSocket(9999);
System.out.println(" Start the server ....");
// 2. Server setup Socket Receive client connections
Socket s = ss.accept();
System.out.println(" client :"+s.getInetAddress().getHostAddress() + " Connected to server ");
// 3. establish IO The input stream reads the data sent by the client
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String mess = br.readLine();
System.out.println(" client : " + mess);
// 4. establish IO The output sends a data message to the client
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write( "【 Server side 】 Send a message \n");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
client :
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
// establish Socket signal communication , Set the address of the communication server IP and Port
Socket socket = new Socket("127.0.0.1", 9999);
// establish IO The output sends a data message to the server
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
// Data sending stream , Be careful bw.write() The data in should be added \n, otherwise Server Will be waiting
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(" Send a message 【 client 】\n");
bw.flush();
// Data receiving reservation
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String mess = br.readLine();
System.out.println(" The server : " + mess);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Running results :【 Run the server code first , Then run the client code 】
Server side :
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServer {
public static void main(String[] args) {
try {
// Create the storage object of the message to be received
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
// Create server side socket
DatagramSocket socket = new DatagramSocket(9991);
System.out.println("udp server start-up ...");
// receive messages , If the message is not received, it will be blocked all the time
socket.receive(packet);
String mess = new String(packet.getData(), 0, packet.getLength());
System.out.println(packet.getLength());
System.out.println(mess);
} catch (Exception e) {
e.printStackTrace();
}
}
}
client :
import java.io.IOException;
import java.net.*;
public class UdpClient {
public static void main(String[] args) {
String mess = "udp message from client";
try {
// obtain IP Address object
InetAddress address = InetAddress.getByName("127.0.0.1");
// establish packet Package object , Encapsulate the packet data to be sent and the server address and port number
DatagramPacket packet = new DatagramPacket(mess.getBytes(), mess.getBytes().length, address, 9991);
// establish socket object
DatagramSocket socket = new DatagramSocket();
// send data
socket.send(packet);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
copyright:author[Mountain wind],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240224254106.html