2
votes

So I'm trying to make a chatroom. I use TCP Sockets to send all information. But when I send a byte array the receiving socket's buffer has the last byte missing. it's 0 cause the byte array is 255 big, so the left over bytes are 0. I've checked through debug mode, at the point when the packet was sent, the array is correct. But at the break point when the sockets finished receiving it's missing that last byte. Any reason why this would be happening? I saw another thread which said you had to stop the thread till the sockets finished receiving, but I'm using AsyncCallBack which to my knowledge is called when the socket has finished receiving.

Also the first byte in each packet is supposed to be the string size (To account for the extra empty bytes).

PacketClass:

public class Packet
{
    public byte[] Buffer;

    public Packet(string message)
    {
        Buffer = new byte[255];

        byte[] messageArray = Encoding.ASCII.GetBytes(message);

        Buffer[0] = (byte)messageArray.GetLength(0);

        for (int i = 0; i < messageArray.GetLength(0); i++)
        {
            Buffer[i+1] = messageArray[i];
        }

    }
    public Packet(byte[] buffer)
    {
        Buffer = buffer;
    }
    public string GetMessage()
    {
        List<byte> messageBuffer = new List<byte>();

        for (int i = 1; i <= Buffer[0]; i++)
        {
            messageBuffer.Add(Buffer[i]);
        }

        return Encoding.ASCII.GetString(messageBuffer.ToArray());
    }

}

SocketClass:

class Client
{
    public static List<Client> connectedClients = new List<Client>();
    public Socket Sock;
    public byte[] Buffer = new byte[255];

    public Client(Socket pSock)
    {
        Sock = pSock;
        Sock.BeginReceive(Buffer, 0, 255, SocketFlags.None, new AsyncCallback(RecieveCallBack), null);
    }

    public void Send(byte[] pBuffer)
    {
        Sock.BeginSend(pBuffer, 0, pBuffer.GetLength(0), SocketFlags.None, new AsyncCallback(SendCallBack), null);
    }

    public void SendCallBack(IAsyncResult AR)
    {
        Console.WriteLine("Sent");
        Sock.EndSend(AR);
    }
    public void RecieveCallBack(IAsyncResult AR)
    {
        Sock.EndReceive(AR);

        Packet recPacket = new Packet(Buffer);
        Console.WriteLine(recPacket.GetMessage());

        Sock.BeginReceive(Buffer, 0, 255, SocketFlags.None, new AsyncCallback(RecieveCallBack), null);
    }
}

Main Server Program `class Client { public static List connectedClients = new List(); public Socket Sock; public byte[] Buffer = new byte[255];

    public Client(Socket pSock)
    {
        Sock = pSock;
        Sock.BeginReceive(Buffer, 0, 255, SocketFlags.None, new AsyncCallback(RecieveCallBack), null);
    }

    public void Send(byte[] pBuffer)
    {
        Sock.BeginSend(pBuffer, 0, pBuffer.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
    }

    public void SendCallBack(IAsyncResult AR)
    {
        Console.WriteLine("Sent");
        Sock.EndSend(AR);
    }
    public void RecieveCallBack(IAsyncResult AR)
    {
        Sock.EndReceive(AR);

        Packet recPacket = new Packet(Buffer);
        Console.WriteLine(recPacket.GetMessage());

        Sock.BeginReceive(Buffer, 0, 255, SocketFlags.None, new AsyncCallback(RecieveCallBack), null);
    }
}`
1
I assume you know that if the passed in message is bigger than 254 characters it will then crash because the array is not big enough? - Phil Wright
I intend to limit strings to a fixed sized. - Spader
What protocol are you using? TCP? Can you show how you instantiate the Sock variable? - Reticulated Spline

1 Answers

0
votes

Solved

The error was in the send method which I didn't include.

sock.BeginSend(sentBuffer[0].Buffer, 0, sentBuffer[0].Buffer[0], SocketFlags.None, new AsyncCallback(SendCallBack), null);

sentBuffer is a list, to be a queue. I changed it to 255 and it workd