简体   繁体   English

Android TCP 与 C# 服务器通信 -> 套接字。关闭

[英]Android TCP communication with C# Server -> socket.Close

I have an Android client and a C# server.我有一个 Android 客户端和一个 C# 服务器。 They are communicating over sockets and the C# server handles receiving asynchronously.它们通过 sockets 进行通信,并且 C# 服务器处理异步接收。

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.一旦 OutputStream 关闭,就会发生这种情况。

This is my Android Client Code:这是我的 Android 客户端代码:

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".当我简单地关闭 Android 仿真器时,我得到了正确的服务器消息:“客户端断开连接”。

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'.但是,当我关闭 Outputstream 或客户端 OnDataReceived() 上的整个套接字时,会以“0”的 data.length 快速重复调用。

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.它不是空的“数据包”(请注意 TCP 是stream ) - 从 TCP 套接字读取零字节意味着另一端也关闭了连接。

"client disconnected" here is probably the result of an RST from the emulator when it goes down and cutting all connections.这里的“客户端断开连接”可能是模拟器出现故障并切断所有连接时发出的RST的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM