JAVA ServerSocket如何同步多个传入连接

时间:2012-12-13 19:01:03

标签: java multithreading synchronization thread-safety serversocket

我有一个服务器应用程序,可以同时接收来自多个客户端的传入连接。它用于从互联网接收数据并在本地将其发送到POS打印机。这是代码:

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()处理线程,同步它们。 但是,如果我从2台不同的计算机同时发送2个连接,我的服务器卡在“从主机收到的连接”,并且两个客户端因超时而丢失。

我所需要的就是解决这个小问题......我已经阅读了这些主题,但未能成功解决我的问题

JAVA threads (different stacks) synchronization

Java thread simple queue

Stopping a ServerSocket accept() loop thread

1 个答案:

答案 0 :(得分:2)

首先,你的程序是在单线程中运行。

对于我在您的代码中看到的内容,您的问题是您为每个您参加的请求/客户重置服务器。

你应该使用这样的东西:

        ServerSocket serverSocket = new ServerSocket(port);
        while (true) {
            Socket client = serverSocket.accept();

            //Do your work

            client.close();
        }

在您的情况下,我认为您不需要多线程,但如果您想使用,请将客户端套接字委托给另一个线程,接受另一个请求...

如果您想使用线程,我建议您创建一个线程池,并创建一个互斥锁,以便独占访问您的打印机。 一些有用的链接: thread poolmutex