收到的数据与通过c sharp socket发送的数据不同

时间:2011-08-12 11:04:19

标签: c# sockets byte send

我正在尝试将文件从客户端发送到服务器,因此我将文件加载到客户端的字节数组中,并通过send()方法将其发送到服务器,但接收到的数组是与发送的数组不同且更大,我想知道它是否是协议问题(但我使用tcp协议确保错误检测):

客户代码:

IPAddress ipAddress = new IPAddress(ip);
IPEndPoint ipEnd = new IPEndPoint(ipAddress, 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

FileStream fl = File.Open("pos.xls",FileMode.Open);
byte[] fileData = ReadFully(fl);
fl.Close();  

byte[] clientData = new byte[ fileData.Length];
fileData.CopyTo(clientData,0);

curMsg = "Connection to server ...";
clientSock.Connect(ipEnd);

curMsg = "File sending...";
clientSock.Send(clientData);

curMsg = "Disconnecting...";
clientSock.Close();
curMsg = "File transferred."; 

服务器代码:

curMsg = "Starting...";
sock.Listen(100);

curMsg = "Running and waiting to receive file.";
byte[] clientData = new byte[1024 * 5000];
while (true)
{
    Socket clientSock = sock.Accept();

    clientData = new byte[1024 * 5000];

    int receivedBytesLen = clientSock.Receive(clientData);
    curMsg = "Receiving data...";

    FileStream fz = writeFully(clientData);
    fz.Close();
    curMsg = "Saving file...";

1 个答案:

答案 0 :(得分:1)

已定义 clientData = new byte[1024 * 5000]; - 然后您不使用receivedBytesLen。我不记得Receive重载是否会在EOF 之前尽可能多地读取,或者只是“some或EOF”(后者是Stream.Read行为),但您必须验证 并使用 receivedBytesLen

IMO,固定缓冲区的方法存在固有缺陷,因为它也不能很好地应对超大输入。我个人会在这里使用NetworkStream;然后你的整个代码变成:

using(var fz = File.Create(path)) {
    networkStream.CopyTo(fz);
}

此处的另一种常见方法是将期望的大小作为前缀发送到数据;这样您就可以验证您是否拥有所需的数据。我个人不会使用这些信息在内存中创建一个正确大小的缓冲区,因为它仍然不允许使用史诗大小的文件(Stream,但是)