I have an Android client and a C# server. They are communicating over sockets and the C# server handles receiving asynchronously.
The communication itself works with no 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 via
socket.close();
the server gets “spammed” with empty packets. This happens as soon as the OutputStream gets closed.
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 the part of my server code which handles 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 simply close the Android emulator I get the correct server message: “Client Disconnected”.
But when I close the Outputstream or the whole socket on client-side OnDataReceived() gets called repeatedly very fast with a data.length of ‘0’.
Thank you for your help!
It’s not empty “packets” (mind you TCP is a stream) – reading zero bytes from TCP socket means the other end closed the connection, so you should close your end too.
“client disconnected” here is probably the result of an RST from the emulator when it goes down and cutting all connections.