C# Don';无法在客户端上接收所有数据
可能的重复项:C# Don';无法在客户端上接收所有数据,c#,sockets,asynchronous,C#,Sockets,Asynchronous,可能的重复项: 我在套接字(TCP)上编写了异步客户端和服务器应用程序。例如,当我从服务器发送300000字节时,我在客户机上只收到其中的一部分。然后我再次调用Receive方法并接收其余的数据。虽然Receive方法必须接收所有数据。有什么问题 下面是客户端接收方法的代码: public class ConnectionInfo { public Socket _socketReply; public Socket _SocketCommand;
我在套接字(TCP)上编写了异步客户端和服务器应用程序。例如,当我从服务器发送300000字节时,我在客户机上只收到其中的一部分。然后我再次调用Receive方法并接收其余的数据。虽然Receive方法必须接收所有数据。有什么问题 下面是客户端接收方法的代码:
public class ConnectionInfo
{
public Socket _socketReply;
public Socket _SocketCommand;
public byte[] _bufferSendReply;
public byte[] _bufferReceive;
public byte[] _bufferSendCommand;
public int _numberBytesSentReply = 0;
public int _numberBytesSentCommand = 0;
public List<byte> _fullBufferReceive;
public uint _id = 0;
public ConnectionInfo()
{
_fullBufferReceive = new List<byte>();
}
}
private void RecieveCallback(IAsyncResult asyncResult)
{
ConnectionInfo connection = (ConnectionInfo)asyncResult.AsyncState;
try
{
int bytesRead = connection.Socket.EndReceive(asyncResult);
if (bytesRead > 0)
{
for (int i = 0; i < bytesRead; i++)
connection.FullBufferReceive.Add(connection.BufferReceive[i]);
if (bytesRead == connection.BufferReceive.Length)
{
connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
new AsyncCallback(RecieveCallback), connection);
Console.WriteLine("Bytes recieved -- " + bytesRead + " by " + connection.Id);
}
else
{
Console.WriteLine("All -- Bytes -- recieved " + bytesRead + " by " + connection.Id);
ConnectionInfo NewCI = new ConnectionInfo(connection);
Recieve(connection);
_serverController.SpreadReceivedData(NewCI);
}
}
else
CloseConnection(connection);
}
catch (ObjectDisposedException ode)
{
CloseConnection(connection);
Console.WriteLine("ObjectDisposedException: " + ode.Message);
}
catch (SocketException exc)
{
CloseConnection(connection);
Console.WriteLine("Socket exception: " + exc.SocketErrorCode);
}
catch (Exception e)
{
CloseConnection(connection);
Console.WriteLine(e.ToString());
}
}
public void Recieve(ConnectionInfo connection)
{
try
{
connection.BufferReceive = new byte[Settings.bufferLength];
connection.FullBufferReceive.Clear();
connection.NumberBytesSentReply = 0;
connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
new AsyncCallback(RecieveCallback), connection);
}
catch (SocketException exc)
{
CloseConnection(connection);
Console.WriteLine("Socket exception: " + exc.SocketErrorCode);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
公共类连接信息
{
公共插座_socketReply;
公共套接字SocketCommand;
公共字节[]_bufferSendReply;
公共字节[]_bufferReceive;
公共字节[]_buffersend命令;
public int _numberbytesentreply=0;
public int _numberbytsentcommand=0;
公共列表_fullBufferReceive;
公共uint_id=0;
公共连接信息()
{
_fullBufferReceive=新列表();
}
}
私有void ReceiveCallback(IAsyncResult asyncResult)
{
ConnectionInfo连接=(ConnectionInfo)asyncResult.AsyncState;
尝试
{
int bytesRead=connection.Socket.EndReceive(异步结果);
如果(字节读取>0)
{
for(int i=0;i
Aw man,别再这样了。这是经过设计和记录的。嗯,那个傻瓜在哪里…我明白了。我从服务器调用BeginSend,但其参数是Int32类型。所以,要发送所有数据,我需要在循环中调用BeginSend,同时发送的数据将不等于0。噢,伙计,又不是这个。这是经过设计和记录的。嗯,那个傻瓜在哪里…我明白了。我从服务器调用BeginSend,但其参数是Int32类型。因此,要发送所有数据,我需要在循环中调用BeginSend,而发送数据将不等于0。