简体   繁体   English

通过TCP套接字丢失一个字节

[英]Loosing one Byte over TCP Socket

So I'm trying to make a chatroom. 所以我想做一个聊天室。 I use TCP Sockets to send all information. 我使用TCP套接字发送所有信息。 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. 它为0,因为字节数组为255大,所以剩余的字节为0。我已经通过调试模式进行检查,在发送数据包时,该数组是正确的。 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. 我看到另一个线程说您必须停止线程直到套接字完成接收,但是我使用的是AsyncCallBack,据我所知,当套接字完成接收时会被调用。

Also the first byte in each packet is supposed to be the string size (To account for the extra empty bytes). 此外,每个数据包中的第一个字节也应假定为字符串大小(以解决额外的空字节)。

PacketClass: 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: 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(); 主服务器程序类Client {public static List connectedClients = new List(); public Socket Sock; 公共套接字袜子; public byte[] Buffer = new byte[255]; 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);
    }
}`

Solved 解决了

The error was in the send method which I didn't include. 错误出在我未包括的send方法中。

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

sentBuffer is a list, to be a queue. sentBuffer是一个列表,将成为一个队列。 I changed it to 255 and it workd 我将其更改为255,它可以正常工作

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

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