Java中的多线程客户机 - 服务器套接字编程

时间:2017-03-25 20:34:05

标签: java multithreading sockets server client

我正在处理我正在处理的消息应用程序的问题,希望有人可以指出我正确的方向。我坚持让客户端和服务器正常通信。

我的客户端向5个进程组播消息,这些进程在服务器端进行更改并将其发送回客户端。这一切都运行正常,但是当我第二次尝试多播时,服务器卡住阅读并最终给我错误:W / System.err(没有其他信息)在我在代码中指出的行。

客户代码:

private class ClientTask extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... msgs) {

        try{

            String msg = msgs[0];
            int ID = Integer.parseInt(msgs[1]);
            ObjectOutputStream out;
            clock++;
            Message m1 = new Message(clock, msg, ID);

            for (int i = 0; i < 5; ++i) {

                String remotePort = REMOTE_PORT[i];
                Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
                        Integer.parseInt(remotePort));

                out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(m1);
                out.flush();

                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                Message m2 = (Message)in.readObject();

                proposed_seq_from_q = m2.seq_number;
                all_proposed.add(proposed_seq_from_q);

                socket.close();
            }

            agreed_deliver = Collections.max(all_proposed);

            m1.set_seq_number(agreed_deliver);

            for (int i = 0; i < 5; ++i) {

                String remotePort = REMOTE_PORT[i];
                Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
                        Integer.parseInt(remotePort));

                out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(m1);
                out.flush();
                socket.close();
            }

        } catch(ClassNotFoundException e){
            Log.e(TAG, "ClientTask: ClassNotFoundException");
            e.printStackTrace();
        } catch (UnknownHostException e) {
            Log.e(TAG, "ClientTask: UnknownHostException");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "ClientTask: IOException");
            e.printStackTrace();
        }

        return null;
    }
}

服务器代码:

    private class ServerTask extends AsyncTask<ServerSocket, String, Void> {

    @Override
    protected Void doInBackground(ServerSocket... sockets) {

        ServerSocket serverSocket = sockets[0];
        boolean listening = true;

        try{
            while(listening) {
                Socket clientSocket = serverSocket.accept();
                ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
                Message m = (Message)in.readObject();

                new_proposed = Math.max(new_proposed + 1, clock);
                m.set_seq_number(new_proposed);

                ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
                out.writeObject(m);

                holdback_queue.add(m);

// ******************************************************
//       THIS IS WHERE IT IS CRASHING
// ******************************************************
                m = (Message)in.readObject();

                publishProgress(m.get_message());
            }

        }catch(Exception e){
            Log.e(TAG, "FAIL");
            e.printStackTrace();
        }

        return null;
    }

我已经尝试了所有我能想到的但是我无法让服务器读取第二条消息。一切都在起作用,并且在没有问题的情况下通过,直到确切的一点。谁能告诉我我做错了什么?

0 个答案:

没有答案