Reputation: 505
I have a server application that may receives incoming connections from multiple clients at same time. It is used to receive data from the internet and send it to a POS Printer locally. Here is the code:
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();
}
}
}
All right, if I send multiple connections from the same client (eg. my machine), the application handles the threads with the wait() and notify(), synchronizing them. But if I send 2 connections at same time from 2 different computers, My server got stuck at "Connection received from host", and the two clients drop due to timeout.
All I need is to solve this little problem...I've read those threads, but was unsuccessful to solve my problem
JAVA threads (different stacks) synchronization
Stopping a ServerSocket accept() loop thread
Upvotes: 1
Views: 4001
Reputation: 325
First you program is running in single thread.
For what I can see in your code, your problem is you reset you server for each request/client you attend.
You should use something like this:
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket client = serverSocket.accept();
//Do your work
client.close();
}
In your case I think you will not need multi threading, but if u want to use, delegate client socket to another thread an accept another request...
If you want to use threads, I suggest you to create a thread pool, and create a mutex for exclusive access to your printer. Some useful links: thread pool and mutex
Upvotes: 2