简体   繁体   中英

How to: Java Server Socket response and Client reading

Imagine the next case:

Client - server connection

Client sends a request to the server Server answers the Client Client reads the answer

Class Client:

public class Client extends Service{

    private  String IP_ADDRESS;
    private  int PORT;

    public void start(){
        l.info("Starting client for server at: "+IP_ADDRESS+":"+PORT);
        //Initialization of the client
        try {
            cs=new Socket(IP_ADDRESS,PORT);
        } catch (UnknownHostException e) {
            l.error("Unkown host at the specified address");
            e.printStackTrace();
        } catch (IOException e) {


        l.error("I/O error starting the client socket");
                e.printStackTrace();
            }
        }

        //Sends the specified text by param
        public void sendText(String text){
            //Initializa the output client with the client socket data
            try {
                //DataOutputStream to send data to the server
                toServer=new DataOutputStream(cs.getOutputStream());

                l.info("Sending message to the server");

                PrintWriter writer= new PrintWriter(toServer);
                writer.println(text);
                writer.flush();

            } catch (IOException e) {
                l.error("Bat initialization of the output client stream");
                e.printStackTrace();
            }
        //Should show the answers from the server, i run this as a thread
        public void showServerOutput(){
            String message;

            while(true){
                //If there are new messages
                try {
                    BufferedReader br= new BufferedReader(new InputStreamReader((cs.getInputStream())));
                    if((message=br.readLine())!=null){
                        //Show them
                            System.out.println(message);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

   }

showServerOutput() is the method that returns any answer sent by the server

Then my server class have the following code

public class Server extends Service{
    public void startListenner(){
        l.info("Listening at port "+PORT);
        while(true){
            // Waits for a client connection
            try {
                cs=ss.accept();
                l.info("Connection received: "+cs.getInetAddress()+":"+cs.getPort());

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                toClient= new DataOutputStream(cs.getOutputStream());
                PrintWriter cWriter= new PrintWriter(toClient);

                //Send a confirmation message
                cWriter.println("Message received");
                //Catch the information sent by the client
                csInput=new BufferedReader(new InputStreamReader(cs.getInputStream()));

                printData();
                toClient.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

As you can see im sending a message to the client with the words: "Message received" but its never shown in the client console. Whats wrong?

EDIT

The printData() method prints the message received from the client in console

public void printData(){
    l.info("Printing message received");
    try {
        System.out.println(csInput.readLine());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Not sure what your printData() method is doing, but aren't you missing a cWriter.flush() on the server side, once you printed "Message received" ? As I understand it, you write your message but never send it to your 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