套接字不会在列表中保存值

时间:2015-06-18 09:00:36

标签: java multithreading sockets arraylist

我是Java的网络开发新手,我想创建一个简单的Socket服务器,它从客户端获取值并在ArrayList中收集所有这些值。我写了一个示例代码,但在服务器端它没有收集字符串。这是我的服务器端:

服务器

public class ServerSideSocket extends Thread{
    private ServerSocket serverSocket;
    private Socket socket;
    private ArrayList<String> list = new ArrayList<String>();
    DataInputStream inData;
    DataOutputStream outData;

    public ServerSideSocket(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }

    public void run() {
        while(true) {
            try{
                System.out.println("Waiting for connection...");
                socket = serverSocket.accept();
                System.out.println("Connected!" );
                inData = new DataInputStream(socket.getInputStream());
                outData = new DataOutputStream(socket.getOutputStream());

                System.out.println(inData.readUTF());

                list.add(inData.readUTF());

                System.out.println("------------ VALUES ---------");
                for (String value: list) {
                    System.out.println(value);
                }
                System.out.println("------------ END VALUES ---------");

                outData.writeUTF("Message saved!");
                outData.flush();
            } catch (SocketException ex) {
                ex.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    inData.close();
                    outData.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        int port = 9999;
        try {
            Thread t = new ServerSideSocket(port);
            t.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

客户:

public class ClientSideSocket {
    public static void main(String[] args) {
        String serverName = "localhost";
        int port = 9999;
        String line = "";

        try {
            System.out.println("Connecting to " + serverName + " on port " + port);
            Socket client = new Socket(serverName, port);
            System.out.println("Just connected to " + client.getRemoteSocketAddress());
            OutputStream out = client.getOutputStream();
            DataOutputStream outData = new DataOutputStream(out);
            InputStream in = client.getInputStream();
            DataInputStream inData = new DataInputStream(in);

            outData.writeUTF("Simple text");
            outData.flush();
            System.out.println("Response from server: " + inData.readUTF());
            System.out.println("You can write more messages!");
            System.out.println();

            client.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

这是因为您尝试通过调用inData.readUTF()方法从数据流中读取两次。第一次调用成功从流中读取数据,但不是保存结果,而是尝试执行下面的另外两行读取。 readUTF()是阻塞方法,因此它等待从不来自同一客户端的另一部分数据。这就是你的服务器永远挂起的原因

您要做的是阅读一次并将结果存储到本地变量中:

String res = inData.readUTF();
list.add(res);

答案 1 :(得分:1)

您正在将数据写入&#34;简单文本&#34;你只能阅读一次。

在您的代码中,您首先阅读它

 System.out.println(inData.readUTF());
 list.add(inData.readUTF());

而不是这样,你应该首先将它存储在一个字符串中然后使用它。

 String message = inData.readUTF();
 System.out.println(message);
 list.add(message);