JAVA ServerSocket如何同步多个传入连接
我有一个服务器应用程序,它可以同时接收来自多个客户端的传入连接。它用于从internet接收数据并将其发送到本地POS打印机。代码如下:JAVA ServerSocket如何同步多个传入连接,java,multithreading,synchronization,thread-safety,serversocket,Java,Multithreading,Synchronization,Thread Safety,Serversocket,我有一个服务器应用程序,它可以同时接收来自多个客户端的传入连接。它用于从internet接收数据并将其发送到本地POS打印机。代码如下: public static void main(String args[]) { RFServer2 server = new RFServer2(); while (true) { server.run(); } } public RFServer2() { } public synchronized void r
public static void main(String args[]) {
RFServer2 server = new RFServer2();
while (true) {
server.run();
}
}
public RFServer2() {
}
public synchronized void run() {
try {
while (printing)
wait();
printing = true;
// 1. creating a server socket
providerSocket = new ServerSocket(43520);
// 2. Wait for connection
System.out.println("Waiting connection...");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
connection.setSoTimeout(8000);
// 3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
try {
in = new ObjectInputStream(connection.getInputStream());
} catch (Exception e) {
throw new Exception("Weird connection received, could not read, aborting...");
}
// sendMessage("connected");
// 4. The two parts communicate via the input and output streams
try {
message = (String) in.readObject();
if (message.contains(".")) {
Socket printerSocket = new Socket(message, 9100);
printerSocket.setSoTimeout(3000);
printer = printerSocket.getOutputStream();
printer.flush();
sendMessage("connected");
while (!message.contentEquals("done")) {
try {
message = (String) in.readObject();
if (!message.contentEquals("done")) {
printer.write(message.getBytes("UTF-8"));
printer.flush();
}
} catch (Exception e) {
System.err.println("Failed to print.");
e.printStackTrace();
break;
}
}
this.message = "";
catch (Exception e) {
System.err.println(e.getMessage());
} finally {
// 5: Closing connection
try {
this.message = "";
if (printer != null) {
System.out.println("Closing connection to the printer...");
printer.close();
}
if (in != null) {
System.out.println("Closing input...");
in.close();
}
if (out != null) {
System.out.println("Closing output...");
out.close();
}
System.out.println(new Date() + " - letting server listening...");
if (providerSocket != null)
providerSocket.close();
printing = false;
notify();
} catch (Exception ioException) {
ioException.printStackTrace();
}
}
}
好的,如果我从同一个客户端(例如我的机器)发送多个连接,应用程序将使用wait()和notify()处理线程,并同步它们。
但是,如果我同时从两台不同的计算机发送两个连接,我的服务器将被卡在“从主机接收的连接”上,并且两个客户端由于超时而中断
我所需要的就是解决这个小问题……我读过这些帖子,但没能解决我的问题
首先,程序以单线程运行 从代码中我可以看出,您的问题是您为您参与的每个请求/客户机重置了服务器 您应该使用如下内容:
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket client = serverSocket.accept();
//Do your work
client.close();
}
在您的情况下,我认为您将不需要多线程,但如果您想使用,请将客户端套接字委托给另一个线程并接受另一个请求
如果您想使用线程,我建议您创建一个线程池,并创建一个互斥锁以独占访问打印机。
一些有用的链接:
及
您的应用程序是单线程的,为什么需要在此处等待/通知?没错,我的程序是单线程的。但是,如果我同时收到2个请求,我需要将它们放入一个队列中,并逐个处理它们。当我从互联网同时提交两个请求时,你的建议不起作用:(。我将尝试实现线程池和互斥。谢谢你的帮助。@pulu:只是出于好奇:“…当我同时提交两个请求时,你的建议不起作用…”:在这种情况下,您会遇到什么样的错误/错误功能?默认情况下,当您设置服务器套接字时,您的收入连接队列设置为50(您可以使用此构造函数ServerSocket(int port,int backlog)更改此数字。我认为您有2种可能会出现客户端断开,1)您将客户端超时设置为较短的时间。2) 您仍在重置服务器套接字。也可以是其他人认为,但尝试检查这2点我提到以上。如果您还没有解决问题,请用您最近的代码更新您的帖子。@nms:我实现了一个线程池,但它不起作用。所以我认为问题可能是我的客户机,而不是我的服务器。我现在正在客户端工作,检查正在发生的事情。谢谢大家!@ALK:我想发生的是,一个连接进入另一个连接,结果,由于超时,它们都会掉下来,导致错误的管道错误。