1

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

Java thread simple queue

Stopping a ServerSocket accept() loop thread

Community
  • 1
  • 1
pulu
  • 495
  • 1
  • 7
  • 16

1 Answers1

2

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

Community
  • 1
  • 1
nms
  • 325
  • 1
  • 14
  • You are right, my program is single threaded. But if, I receive 2 requests at same time, I need to put them in a queue and proccess them one by one. What you suggested did not work when I submit 2 requests at same time from the internet :(. I will try to implement a threadpool and a mutex. Thanks for helping me. – pulu Dec 14 '12 at 11:24
  • @pulu: Just out of curiosity: "*... What you suggested did not work when I submit 2 requests at same time ...*": What kind of error/mal function do you experience in this case? – alk Dec 17 '12 at 12:48
  • 1
    By default when you set up your server socket, you have a queue of income connections set to 50(you can change this number true backlog using this constructor ServerSocket(int port, int backlog)). I see 2 possibilities for you client drops, 1) You set your client timeout for a short amount. 2) You are still resetting you serverSocket. Also can be other think, but try to check this 2 points I mention above. If you do not resolved your problem yet, please update your post with your recent code. – nms Dec 18 '12 at 00:31
  • @nms: I implemented a thread pool, but it did not worked. So I thought that the problem could be my client and not my server. I'm currently working on the client side now, to check what is happening. Thank you! – pulu Dec 18 '12 at 13:22
  • @alk: I think that what happens is that one connection get in the middle of another one, and as result, they both get dropped due to timeout, resulting in a broken pipe error. – pulu Dec 18 '12 at 13:27
  • @pulu: Who gets/detects/logs the broken pipe, the client or the server or both? – alk Dec 18 '12 at 16:35