Java network client sends only one message

时间:2017-12-17 08:17:36

标签: java networking

I am learning Java and I'm writing an example client - server application. The sokcket connection is fine, everything works well until the second message from the client app. It does not reach the server. If I start another client it also succeed at the first message, and fails at the second.

Anyone has an idea? Thanks in advance!

Server code:

package networking;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private static Socket socket;

    public static void main(String[] args) {
        try {

            int port = 25000;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop
            while (true) {
                //Reading the message from the client
                socket = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String incomingMessage = br.readLine();
                System.out.println("((( " + incomingMessage);

                String returnMessage = incomingMessage;

                //Sending the response back to the client.
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
                bw.write(returnMessage + "\n");
                bw.flush();
                System.out.println("))) " + returnMessage);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (Exception e) {
            }
        }
    }
}

And the client

package networking;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Client {

    private static Socket socket;

    public static void main(String args[]) {
        try {
            String host = "localhost";
            int port = 25000;
            InetAddress address = InetAddress.getByName(host);
            socket = new Socket(address, port);

            //Send the message to the server
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String message = "";
            /*
            while(!message.equals("q")) {

                System.out.print("Message: ");
                message = console.readLine();
                bw.write(message + "\n");
                bw.flush();
                System.out.println("))) " + message);
                //Get the return message from the server
                String incomingMessage = br.readLine();
                System.out.println("((( " + incomingMessage);
            }
             */
            for (int i = 0; i < 10; i++) {
                bw.write(i + "\n");
                bw.flush();
                System.out.println("))) " + i);
                String incomingMessage = br.readLine();
                System.out.println("((( " + incomingMessage);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            //Closing the socket
            try {
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

2 个答案:

答案 0 :(得分:2)

Your server is not set up to handle this. You are reading one line, then discarding the connection for the GC, without closing it. The server, reading one line, then ignores all other lines and starts listening for the next connection.

Also, consider using threads.

答案 1 :(得分:2)

您的function getInfopeople(user_id) { // https://graph.facebook.com/{version}/{user_id}?fields=picture.width(150).height(150) FB.api('/' + user_id, 'GET', { fields:'picture.width(150).height(150)' }, function(response) { document.getElementById('user_status').innerHTML = "<img src='" + response.picture.data.url + "'>"; }); } 在您的服务器代码中放错了位置,实际上您需要2 while个循环:

  • 一个接受新客户
  • 一个用于通过客户端管理多条消息

在伪代码中,它为您提供:

while

如果你想使用线程,那么它就是你必须委托给新线程的内部while (true): accept new client while (client connected): read message from client write back message to client close client socket 循环任务。

注意:while阻止,直到新客户端到来。这就是为什么你只能通过客户端发送一条消息。