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

时间:2015-02-11 00:28:48

标签: c# sockets tcp server

所以我想做一个聊天室。我使用TCP套接字发送所有信息。但是当我发送一个字节数组时,接收套接字的缓冲区中缺少最后一个字节。它是0因为字节数组是255大,所以左边的字节是0.我已经通过调试模式检查,在发送数据包的时候,数组是正确的。但是在断点处,当套接字完成接收它时,它丢失了最后一个字节。有什么理由会发生这种情况吗?我看到另一个线程说你必须停止线程直到套接字完成接收,但我正在使用AsyncCallBack,据我所知,当套接字完成接收时会调用它。

此外,每个数据包中的第一个字节应该是字符串大小(要考虑额外的空字节)。

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);
    }
}

主服务器程序 `类客户     {         public static List connectedClients = new List();         公共插座袜;         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 个答案:

答案 0 :(得分:0)

解决

错误发生在我没有包含的发送方法中。

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

sentBuffer是一个列表,是一个队列。我将它改为255并且工作了