Skintkingle
Skintkingle

Reputation: 1579

Socket sending long strings of null

Hi guys i have an issue currently when using a Socket to send data.

I am currently getting a very strange issue when a Client sends to the server, for example "HEART", as a heart beat. afer sending the first time the server starts receiving a whole lot of \0, not always the same amount before the HEART. I am using queues to queue up sending so on slow connections it waits for the current send to be done before it sends the next but for tiny lengths like that i'm a bit confused.

    public void Send(string data)
    {
        if (Connected)
        {
            SendQueue.Enqueue(data);
            if (t.ThreadState == ThreadState.Stopped)
            {
                t = new Thread(new ThreadStart(SendData));
                t.Start();
            }
            else if (t.ThreadState == ThreadState.Unstarted)
                t.Start();
        }
    }

and the SendData function

    private void SendData()
    {
        if (sending)
            return;
        sending = true;
        while (SendQueue.Count > 0)
        {
            if (ClientSocket.Connected)
            {
                byte[] data = Networking.StringToByte((string)SendQueue.Dequeue());
                ClientSocket.Send(data);
            }
        }
        sending = false;
    }

i don't think it's the sending function because i've debugged it and the byte array always holds the correct info.

the receiving end is even simpler.

    public string Receive()
    {
        string msg = "";
        if (Connected)
        {
            byte[] data = new byte[1024];
            while (ClientSocket.Avaliable > 0)
            {
                ClientSocket.Receive(data);
                msg += Networking.ByteToString(data).Trim();
            }
        }
        return msg;
    }

If anyone could point out where i'm going wrong or if i've gone at this the entirely wrong way that would be great. Thanks guys.

I will remind people that it's seemingly random lengths of \0 each 2 seconds (in this example for the HEART message heartbeat)

Upvotes: 1

Views: 1031

Answers (1)

nos
nos

Reputation: 229342

This piece of code can't be correct:

    byte[] data = new byte[1024];
    while (ClientSocket.Avaliable > 0)
    {
        ClientSocket.Receive(data);
        msg += Networking.ByteToString(data).Trim();
    }

It seems you do not take into account how much data you actually receive. You have to look at the return value from ClientSocket.Receive I don't know what your Networking.ByteToString does but when your code runs Networking.ByteToString , that function cannot know how much data you actually received. It'll probably convert the entire buffer - all 1024 bytes. And that's likely where all your zeroes comes from. It could be that somewhere you're doing a similar thing on the sending side.

You also might need to keep in mind that TCP is stream oriented, not packet oriented. If you do 1 Send call, that can take several Receive calls to read, or 1 Receive call might read the data from many Send calls.

Upvotes: 3

Related Questions