Question

I had create a new client-server connection using ordinary java socket programming:

private static BufferedReader input;
private static DataOutputStream output;
private static Socket socket;
public void connect() {
    try {
        socket = new Socket(address, port);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And I'm sending and receiving the following way :

to send:

output = new DataOutputStream(socket.getOutputStream());
output.writeBytes(data);

to receive:

    InputStream stream = null;
    try{
        stream = socket.getInputStream();
    }catch(Exception e){
        e.printStackTrace();
    }
    if(stream != null){
        input = new BufferedReader(new InputStreamReader(
                stream));

    // some input processing
    }

The problem is when The connection is interrupted some how I have to relaunch it again in the app run-time, so I made a thread in order to re-execute the connect() method, and It executed efficiently without any Exceptions, but then , the input and output variables fail to send or receive any data.

Était-ce utile?

La solution

It sounds like you are reconnecting your socket without recreating your input and output streams.

Try running recreating your input and output streams after you run connect() like so:

output = new DataOutputStream(socket.getOutputStream());
stream = socket.getInputStream();

Autres conseils

you can try cleaning the input and output stream in finally and also close the socket...

finally { socket.close(); }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top