Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:12:46+00:00 2026-05-24T11:12:46+00:00

I have an Android client and a C# server. They are communicating over sockets

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:12:46+00:00Added an answer on May 24, 2026 at 11:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple client-server app on android. the android service communicates with the
I am making a Client Server application for my Android phone. I have created
I am newbie to android. I have client server based application. Server keeps on
I have created a simple Twisted based TCP server and Android client. I have
I have an client server application on Android.And I have to send data from
I have to create an application server client in android(both the client an server
I have to create an application a kind of client-server thing,both on android!!! My
I have a year's experience writing client code but none with server stuff. I
I'm working on a client-server Android application and trying to figure out how to
I have recently written a socket server in PHP that will be handling communication

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.