Android TCP communication with C # & # 8594; socket.Close

I have an Android client and a C # server. They exchange sockets, and C # servers process asynchronously.

Communication itself works without problems. I can authenticate the client on the server and send messages to each other. But if I try to close the socket on the client with

socket.close(); 

the server receives "spam" with empty packets. This happens as soon as the OutputStream closes.

This is my Android client code:

 public void run() { try { InetAddress serverAddr = InetAddress.getByName(pServerIp); Socket socket = new Socket(serverAddr, pServerPort); try { OutputStream socketoutstr = socket.getOutputStream(); OutputStreamWriter osr = new OutputStreamWriter( socketoutstr ); bw = new BufferedWriter( osr ); InputStream socketinstr = socket.getInputStream(); InputStreamReader isr = new InputStreamReader( socketinstr ); br = new BufferedReader( isr ); User tmp = Login("SESAM", "PASSWORD"); if(tmp != null) { Log.e("TCP", "Login succeeded!"); user = tmp; } else { Log.e("TCP", "Login failed!"); socket.close(); } } catch(Exception e) { Log.e("TCP", "S: Error", e); socket.close(); } finally { } } } 

This is part of my server code that processes incoming data:

 private void WaitForData() { try { WorkerCallback = new AsyncCallback(OnDataReceived); UndefinedPacket packet = new UndefinedPacket(); socket.BeginReceive(packet.DataBuffer, 0, packet.DataBuffer.Length, SocketFlags.None, WorkerCallback, packet); } catch (SocketException se) { Console.WriteLine(se.Message); } } private void OnDataReceived(IAsyncResult asyn) { UndefinedPacket socketData = (UndefinedPacket)asyn.AsyncState; try { int CharCount = socket.EndReceive(asyn); char[] chars = new char[CharCount]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); String text = System.Text.Encoding.UTF8.GetString(socketData.DataBuffer); int charLen = d.GetChars(socketData.DataBuffer, 0, CharCount, chars, 0); String data = new String(chars); if (data.Length > 0) { IClientPacket packet = PacketFactory.GetInstance(data); server.PacketManager.AddIncomingPacket(packet, this); } Console.WriteLine("Received Data!"); WaitForData(); } catch (ObjectDisposedException) { Console.WriteLine("OnDataReceived: Socket has been closed"); server.RemoveWorkerSocket(this); } catch (SocketException se) { if (se.ErrorCode == 10054) // Connection reset by peer { string msg = "Client Disconnected"; Console.WriteLine(msg); server.RemoveWorkerSocket(this); } else { Console.WriteLine(se.Message); server.RemoveWorkerSocket(this); } } catch (Exception e) { Console.WriteLine(e.Message); server.RemoveWorkerSocket(this); } } 

When I just close the Android emulator, I get the correct server message: "The client is disconnected."

But when I close Outputstream or the entire socket on the client side, OnDataReceived () is called many times very quickly with data.length '0'.

Thank you for your help!

+4
source share
1 answer

These are not empty “packets” (note that TCP is a stream) - reading zero bytes from a TCP socket means that the other end has closed the connection, so you should also close your end.

"client disconnected" is probably the result of RST from the emulator when it goes down and cuts all connections.

+5
source

All Articles