Java网络编程--简单的客户端与服务端交互

java

一个Socket初级应用的例子

这里写一个用socket模拟的服务端-客户端文字交互的小程序。Java网络编程会再开一文(依旧是《Java Core》学习笔记)

代码

服务器端代码

这里我为了让服务端可以接收多个客户端请求,所以服务端的处理代码写在线程中

class Server implements Runnable {

private final Socket socket; //传入ServerSocket.accept()返回的Socket,即服务端Socket

private final String serverName;

private SharedData shareData; //没用,之前想的是服务端利用共享数据实现广播功能,但是应该不能这么做

private static final int CHUNK = 1024; //读写的大小单位

public Server(Socket socket, String serverName, SharedData shareData) {

this.socket = socket;

this.serverName = serverName;

this.shareData = shareData;

}

@Override

public void run() {

try (BufferedInputStream receive = new BufferedInputStream(socket.getInputStream());

BufferedOutputStream send = new BufferedOutputStream(socket.getOutputStream());

Scanner scanner = new Scanner(System.in)) {

byte[] receiveData = new byte[CHUNK];

int size;

while ((size = receive.read(receiveData)) != -1) {

byte[] trimData = Arrays.copyOf(receiveData, size);

System.out.printf("服务器%s接收到消息-->%s\n", this.serverName,

new String(trimData, StandardCharsets.UTF_8));

while (scanner.hasNext()) {

send.write(scanner.next().getBytes(StandardCharsets.UTF_8));

send.flush();

break;

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

public String getServerName() {

return serverName;

}

}

客户端代码

客户端的代码可以不写在线程里,用进程直接模拟即可。之后要学习一下客户端之间怎么通过服务端进行交互

class Client implements Runnable {

private final String serverHost;

private final int serverPort;

private final String clientName;

private static final int CHUNK = 1024;

public Client(String serverHost, int serverPort, String clientName) {

this.serverHost = serverHost;

this.serverPort = serverPort;

this.clientName = clientName;

}

@Override

public void run() {

try (Socket socket = new Socket(serverHost, serverPort);

BufferedOutputStream send = new BufferedOutputStream(socket.getOutputStream());

BufferedInputStream receive = new BufferedInputStream(socket.getInputStream());

Scanner scanner = new Scanner(System.in)) {

while (scanner.hasNext()) {

send.write(scanner.next().getBytes(StandardCharsets.UTF_8));

send.flush();

byte[] receiveData = new byte[CHUNK];

int size = receive.read(receiveData);

System.out.printf("客户端%s收到消息-->%s\n", this.clientName,

new String(Arrays.copyOf(receiveData, size), StandardCharsets.UTF_8));

}

} catch (IOException e) {

e.printStackTrace();

}

}

public String getClientName() {

return clientName;

}

}

启动代码

public class ServerLauncher {

static final int PORT = 8888;

static final int TEXT_CHUNK_SIZE = 1024 * 1024;

public static void main(String[] args) {

int count = 1;

byte[] shareData = new byte[TEXT_CHUNK_SIZE];

SharedData sharedData = new SharedData(shareData);

try (ServerSocket serverSocket = new ServerSocket(PORT)) {

for (;;) {

System.out.printf("第%d个Server已经启动,等待连接中...\n", count);

Server server = new Server(serverSocket.accept(), "Server" + count, sharedData);

Thread t = new Thread(server);

t.setName(server.getServerName());

t.start();

System.out.printf("第%d个Client连接成功!\n", count++);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

public class ClientLauncher {

static final String IP = "127.0.0.1";

static final int PORT = 8888;

public static void main(String[] args) {

Client client = new Client(IP, PORT, "client");

Thread t = new Thread(client);

t.setName(client.getClientName());

t.start();

}

}

以上是 Java网络编程--简单的客户端与服务端交互 的全部内容, 来源链接: utcz.com/z/391937.html

回到顶部