简体   繁体   中英

Java network client sends only one message

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();
            }
        }
    }

}

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.

Your while is misplaced in your server code, and in fact you need 2 while loops:

  • one for accepting new clients
  • one for manage several messages by client

In pseudo code it gives you:

while (true):
    accept new client
    while (client connected):
        read message from client
        write back message to client
    close client socket

If you want to use threads, then it's the inner while loop task which you have to delegate to a new thread.

Note: accept is blocking until a new client comes. That why you could send only one message by client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM