gpt4 book ai didi

java - 具有多个客户端的服务器 - JAVA

转载 作者:行者123 更新时间:2023-11-29 07:49:55 26 4
gpt4 key购买 nike

鉴于这两个代码,一个用于服务器,另一个用于客户端,我试图让多个客户端与服务器通信,但我不知道如何去做。知道服务器如何识别与他交谈的客户端并回复同一个客户端吗?

服务器:

    import java.io.*;
import java.net.*;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");

//4. The two parts communicate via the input and output streams
do{
try{
message = (String)in.readObject();

System.out.println("client>" + message);
if (message.equals("byee"))
sendMessage("Wosil");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("Zame-Server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}

客户端

import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 2004);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server

sendMessage("Hi my server");
sendMessage("SENDING");
message = "byee";
sendMessage(message);
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);


}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("Zame-Client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}

最佳答案

您的问题似乎有两个方面。第一个处理并发(如何启动多个客户端并使它们与服务器通信)。为此,您必须具备 Java 线程的基本知识。

你的问题的第二部分:服务器如何跟踪多个客户端(这也表明你的客户端和服务器之间的通信是有状态的):你可能想设计一个简单的协议(protocol),客户端和服务器通过该协议(protocol)进行通信.例如,当客户端与服务器通信时,它必须使用唯一标识符标记其消息。服务器使用该标识符来跟踪和保留它必须用来完成任务的任何资源,客户端要求它(如果这是您的要求所要求的)。

关于java - 具有多个客户端的服务器 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com