套接字服务器在同一进程中一次只接受一个连接
Socket Server accepting only one connection at a time on the same process
我正在尝试实现一个 client-server 应用程序,其中服务器可以从客户端接受一些 Object 并且对于每个 Object 它必须将其解释为消息并且妥善处理。
代码如下:
(服务器)
public class GlobalServer{
GlobalServer(){new Thread(() ->{
try {
serverSocket = new ServerSocket(1234);
Socket clientSocket;
while (true) {
clientSocket = serverSocket.accept();
handleClient(clientSocket);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}).start();
}
public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
Object [] objArr = (Object[]) is.readObject();
msgHandler(objArr, clientSocket);
}
public void msgHandler(Object [] objArr, Socket clientSocket){
int msg_type = (int) objArr[0];
switch (msg_type) {
case 1:
System.out.println("type 1 received");
break;
case 2:
System.out.println("type 2 received");
break;
case 3:
System.out.println("type 3 received");
break;
default:
break;
}
}
public static void main(String [] args){
GlobalServer s = new GlobalServer();
}
}
OBS:在应用程序上接收一个 objects 的数组是有意义的,因为每条消息都带有它的 header(类型)及其内容
(客户)
public class Client {
public static void main(String [] args){
try {
Socket client = new Socket("192.168.0.105", 1234);
ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
Object [] objArr = {3, "Type 3 message"};
os.writeObject(objArr);
Object []objArr1 = {1, "Type 1 message"};
os.writeObject(objArr1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我 运行 GlobalServer 的实例在单独的机器上,在另一台机器上我连接到服务器并顺序发送两条消息。问题是服务器只接收和处理第一条消息,第二条消息丢失,客户端在没有服务器接收第二条消息的情况下完成。奇怪的是,如果我将这两个消息作为两个不同的应用程序发送,服务器会很好地处理它。它与在同一进程中发送的两个消息有关吗?
作为不同应用程序工作的代码:
(消息 1)
public class Message1 {
public static void main(String [] args){
try {
Socket client = new Socket("192.168.0.105", 1234);
ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
Object [] objArr = {3, "Type 3 message"};
os.writeObject(objArr);
} catch (IOException e) {
e.printStackTrace();
}
}
}
(消息 2)
public class Message2 {
public static void main(String [] args){
try {
Socket client = new Socket("192.168.0.105", 1234);
ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
Object []objArr1 = {1, "Type 1 message"};
os.writeObject(objArr1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
问题出在以下代码块中:
public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
Object [] objArr = (Object[]) is.readObject();
msgHandler(objArr, clientSocket);
}
您只读了一个对象。 (Object[]) is.readObject();
应在循环中调用以读取多个对象并为每个对象调用 msgHandler
方法。
希望对您有所帮助
我正在尝试实现一个 client-server 应用程序,其中服务器可以从客户端接受一些 Object 并且对于每个 Object 它必须将其解释为消息并且妥善处理。
代码如下:
(服务器)
public class GlobalServer{
GlobalServer(){new Thread(() ->{
try {
serverSocket = new ServerSocket(1234);
Socket clientSocket;
while (true) {
clientSocket = serverSocket.accept();
handleClient(clientSocket);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}).start();
}
public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
Object [] objArr = (Object[]) is.readObject();
msgHandler(objArr, clientSocket);
}
public void msgHandler(Object [] objArr, Socket clientSocket){
int msg_type = (int) objArr[0];
switch (msg_type) {
case 1:
System.out.println("type 1 received");
break;
case 2:
System.out.println("type 2 received");
break;
case 3:
System.out.println("type 3 received");
break;
default:
break;
}
}
public static void main(String [] args){
GlobalServer s = new GlobalServer();
}
}
OBS:在应用程序上接收一个 objects 的数组是有意义的,因为每条消息都带有它的 header(类型)及其内容
(客户)
public class Client {
public static void main(String [] args){
try {
Socket client = new Socket("192.168.0.105", 1234);
ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
Object [] objArr = {3, "Type 3 message"};
os.writeObject(objArr);
Object []objArr1 = {1, "Type 1 message"};
os.writeObject(objArr1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我 运行 GlobalServer 的实例在单独的机器上,在另一台机器上我连接到服务器并顺序发送两条消息。问题是服务器只接收和处理第一条消息,第二条消息丢失,客户端在没有服务器接收第二条消息的情况下完成。奇怪的是,如果我将这两个消息作为两个不同的应用程序发送,服务器会很好地处理它。它与在同一进程中发送的两个消息有关吗?
作为不同应用程序工作的代码:
(消息 1)
public class Message1 {
public static void main(String [] args){
try {
Socket client = new Socket("192.168.0.105", 1234);
ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
Object [] objArr = {3, "Type 3 message"};
os.writeObject(objArr);
} catch (IOException e) {
e.printStackTrace();
}
}
}
(消息 2)
public class Message2 {
public static void main(String [] args){
try {
Socket client = new Socket("192.168.0.105", 1234);
ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
Object []objArr1 = {1, "Type 1 message"};
os.writeObject(objArr1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
问题出在以下代码块中:
public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
Object [] objArr = (Object[]) is.readObject();
msgHandler(objArr, clientSocket);
}
您只读了一个对象。 (Object[]) is.readObject();
应在循环中调用以读取多个对象并为每个对象调用 msgHandler
方法。
希望对您有所帮助