-1

I have an android app which will create a ServerSocket and accept a socket. I want it can communicate(read/write) with remote device.

My sample code like following:

mListenSocket = new ServerSocket();
mListenSocket.setReuseAddress(true);
mListenSocket.bind(new InetSocketAddress(DOCK_PORT));
mSocket = mListenSocket.accept();

Thread {
    loop {
    outputStream = mSyncSocket.getOutputStream();
    inputStream = mSyncSocket.getInputStream();
    ...
    inputStream.read(data)
    outputStream.write(data);
    ...
    }
}

It can read right inputStream data from client, but the second time read() always return -1 after I write data in outputStream firt time .

I have no idea about this issue. Somebody can give me some tips? Thanks a lot.

======================================================================

I think I need to express my problem more clearly.

There are two devices(A, B), and their workflow as follows:

Type 1 task:
1. A sends command to B
2. B receives command and reply message to A
3. A receives message, Done.

Type 2 task:
1. A sends command to B, Done.

Above tasks are asynchronized.

My old method was that create a ServerSocket on A and B device respectively to handle A->B and B->A communication.
I think maybe one socket can resolve above task, but one socket will encounter read -1 issue.
Someone can give me more advices? Thanks a lot.

Derek Tsai
  • 24
  • 4
  • Take the first two statements of the loop out of the loop. Put them before the loop. You should initiate those variables only once. – greenapps Feb 04 '16 at 13:27
  • sorry for the late reply.'loop' is something like 'while', 'for' ...etc sample code omitted too much. I did initiate those variables before loop. – Derek Tsai Feb 14 '16 at 16:12

2 Answers2

0

read() returns -1 because the peer has closed the connection. You should do likewise, and stop reading, and stop handling the connection completely. It's finished.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

I use something like this:

final DataOutputStream dos = new DataOutputStream(s.getOutputStream());   
final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));

// Send the login data.
final JSONObject login = new JSONObject();

// Send the login message.
dos.write((login.toString() + "\r\n").getBytes());

// Wait until we receive something from the server.
String receivedMessage;
while ((receivedMessage = in.readLine()) != null) {
    Log.i(TAG, "Received data: " + receivedMessage);
    processMessagesFromServer(dos, receivedMessage);
}

I use a normal socket instead of ServerSocket. Please check this thread for more information.

Community
  • 1
  • 1
Blehi
  • 1,990
  • 1
  • 18
  • 20
  • Zero evidence here that the client is sending lines. The mixture of `DataOutputStream` and `BufferedInputStream`is bizarre. – user207421 Feb 06 '16 at 09:26
  • We always use the `\r\n` separator .. so for us it is working properly. Do you have better idea instead of the mixture of `DataOutputStream` and `BufferedInputStream`? Please share it with us. – Blehi Feb 08 '16 at 13:44
  • sorry for the late reply. I'm really appreciate your response. – Derek Tsai Feb 14 '16 at 16:18