简体   繁体   中英

Java Client - Server not repeat respond

//Edit 13:25 Client Console

Connect to server<br />
Repsonded: Tue May 22 13:23:28 CEST 2012<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Repsonded: Tue May 22 13:23:38 CEST 2012<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />

Server Console

Server Initialized<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
Error(run): java.net.SocketException: Connection reset by peer: socket write error<br />


Server load data and not return repead "Repsonded: Tue May 22 13:23:38 CEST 2012" or client not ouput print.

The problem lies in your server implementation. You are not looping in the run statement, checking for new incomming messages from that connection. You read the information once and then close the connection in the finally statement:

 @Override
    public void run() {

        try {

            BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
            InputStreamReader isr = new InputStreamReader(is);
            ...
        finally {
            try {
                connection.close();

Sure, your server has a loop checking for incomming connection requests, so this would work if the client tries to connect to the server again after each message is sent, but your client only sends one request (in the beginning of the main statement).

So, what I would advice you to do: Server-side, setup the streams etc in the run statement, and once they are set up enter a infinte loop with while(true) , and in that loop have the server read from the input stream, and if read() does not return null, print the message. This will cause the server check for incomming data from that client, once you get a string that's not null it will print it (and trigger a response if you want).

Same goes for client side, you need a loop that listens for incomming messages from the server

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