简体   繁体   中英

TCP/IP Client sending multiple messages to Server

I'm using this code to make a communication between client and server, now what I need and I'm struggling with is how to make that the server accept multiple messages from client; and that the communication between client and server is not 1 client message then 1 server message instead they can write in every order.

How can I do this?

Thanks

Here is my code :

public class GossipServer {

    public static void main(String[] args) throws Exception{
        //create a server socket 
        ServerSocket sersock = new ServerSocket(3000);
        System.out.println("Server  ready for chatting");
        Socket sock = sersock.accept();        
        System.out.println("Client accepted information"+sock.getInetAddress()+sock.getPort());

        // reading from keyboard (keyRead object)
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(""+keyRead.toString());

        // sending to client (pwrite object)
        OutputStream ostream = sock.getOutputStream(); 
        PrintWriter pwrite = new PrintWriter(ostream, true);
        System.out.println("output stream "+ostream.toString());

        // receiving from client ( receiveRead  object)
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
        System.out.println("input stream "+istream.toString());

        //receiving message.    send message  
        String receiveMessage, sendMessage;               
        while(true) {
            if((receiveMessage = receiveRead.readLine()) != null ) {
                System.out.println(receiveMessage); }     

                sendMessage = keyRead.readLine(); 
                pwrite.println(sendMessage);             
                pwrite.flush();

            }
        }                                      
    }
}

client :

public class GossipClient {

    public static void main(String[] args) throws Exception {
        Socket sock = new Socket("127.0.0.1", 3000);
        // reading from keyboard (keyRead object)
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        // sending to client (pwrite object)
        OutputStream ostream = sock.getOutputStream(); 
        PrintWriter pwrite = new PrintWriter(ostream, true);

        // receiving from server ( receiveRead  object)
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

        System.out.println("Start the chitchat, type and press Enter key");

        String receiveMessage, sendMessage;               
        while(true) {
            sendMessage = keyRead.readLine();  // keyboard reading
            pwrite.println(sendMessage);       // sending to server
            pwrite.flush();                    // flush the data
            if((receiveMessage = receiveRead.readLine()) != null) //receive from server {
                System.out.println(receiveMessage); // displaying at DOS prompt
            }         
        }               
    }                  
}

First of all this answer will not be a guide on how to fix your issue but more of a detailed explanation what you have to fix including some suggestions and ideas.

Now let's talk about blocking operations. Reading from an InputStream using the BufferedReader::readLine method for exampleis such a blocking operation. This means it will block the entire thread until it has returned. Think of it like like a while loop.

Now in order to bypass this restriction you could create a new thread which will only listen to the stream and print out whatever it receives. I personally went with this in some small scale projects and tests as it will always need two threads per connection on the server side.

If you just want a small peer-to-peer chat application I follow this approach since it's easier to comprehend (in my opinion ) and is sophisticated enough to deal with your issue.


However if you want a large scale chatroom with thousands of users chatting at the same time I would follow the channel approach introduced in the java.nio package.

However as I have not gotten myself too deep into these areas of the core API I cannot provide more concrete information about it.

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