【问题标题】:How to let a server receive more than a single message from clients?如何让服务器从客户端接收多条消息?
【发布时间】:2018-04-04 02:07:21
【问题描述】:

我希望有一个正在运行的服务器并从客户端(例如另一个 Java 应用程序)接收消息。我通过带有 InputStream 的 BufferedReader 执行此操作,只要我执行一次它就可以按预期工作。该消息由该方法处理并将接收到的消息的测试消息写入屏幕上,但如果我让它在一个while循环中运行它会说 -

java.net.SocketException: Connection reset

所以一旦服务器收到一条消息,我不知道如何获得第二条或任何后续消息。

我的主要源码是:

public static void main (String[] args)  {

    int port = 13337;
    BufferedReader msgFromClient = null;
    PrintWriter msgToClient = null;

    timeDate td = new timeDate(); //Class i did for myself to get time/date

    ServerSocket s_socket = null;
    try {
        s_socket = new ServerSocket(port);
        System.out.println("Server startet at "+td.getCurrDate()+" "+td.getCurrTime());
    }
    catch (IOException ioe) {
        System.out.println("Server on Port "+port+" couldnt be created. \nException: "+ioe.getMessage());
    }

    Socket socket = null;
    try {
        socket = s_socket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        msgFromClient = utils.createInputStream(socket);
    } catch (IOException ioe) {
        System.out.println("Creation of an Input Stream failed.\n Exception - "+ioe);
    }
    try {
        msgToClient   = utils.createOutputStream(socket);
    } catch (IOException ioe) {
        System.out.println("Creation of an Output Stream failed.\n Exception - "+ioe);
    } 

    String input = null;
    while (true) {
            try {
                input = msgFromClient.readLine();
            } catch (IOException ioe) {
                System.out.println(ioe);
            }
            if(input!=null) {
                System.out.println("Jumping out of loop: "+input);
                utils.processCode(input);
            }
    } 

创建流的两个类如下所示:

public static BufferedReader createInputStream (Socket socket) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    return br;
}

public static PrintWriter createOutputStream (Socket socket) throws IOException {

    PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);

    return pw;
}   

“processCode”类就是一个开关。

【问题讨论】:

    标签: java sockets server bufferedreader serversocket


    【解决方案1】:
    Socket socket = null;
    try {
        socket = s_socket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    您只接受一个连接,然后您正在处理。您需要为每个连接打开一个新线程

    ExecutorService threadLimit = Executors.newFixedThreadPool(10);
        while(true)  {
            Socket s = serverSocket.accept();
            treadLimit.submit(new HandleThread(s));
        }
    

    【讨论】:

    • 我添加了一个 ExecutorService,因为它设置了最大打开线程的限制:如果您打开了 5 个线程/会话,那么它将不会打开任何其他线程。如果您关闭一个线程(在处理完一个线程后),您将能够再启动一个线程。
    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多