質問

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.

役に立ちましたか?

解決

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

他のヒント

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

finally { socket.close(); }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top