通过TCP套接字发送二进制文件c

时间:2011-10-08 14:45:21

标签: c# c++ c sockets tcp

我做了一个客户&通过套接字建立TCP连接的服务器,我试图通过套接字发送二进制数据,但我只能发送txt或pdf文件,我没有运气文件的运气,我使用fread和fseek来读取文件并将其拆分进入缓冲区。当我读取整个exe文件时,它会成功发送,但是当我拆分它时,它会被发送损坏!

我读了一些关于套接字的书,但我仍然不太了解,我有一些问题。

可以将整个文件发送到一个send()吗?或者我应该继续以小块发送它?

另外,为什么exe文件在我发送块时会被破坏?

谢谢!

客户代码(c中):

int bufSize = 10000;
int sentBytes = 0;


  FILE * pFile;
  long remainingBytes;
  char * buffer;
  size_t result;

  pFile = fopen ( "D:\\file.exe" , "rb" );
  fseek (pFile , 0 , SEEK_END);
  remainingBytes = ftell (pFile);
  rewind (pFile);

int bufferSize = remainingBytes > bufSize ? bufSize : remainingBytes;
buffer = (char*) malloc (sizeof(char)*bufferSize);
send(Socket, (char*)&remainingBytes, 4, 0);

while(remainingBytes > 0)
{
    fseek (pFile , sentBytes , SEEK_SET);
    result = fread(buffer,1,bufferSize,pFile);
    if(bufferSize < remainingBytes)
    {
        send(Socket, buffer, bufferSize, 0);
    }
    else
    {
        send(Socket, buffer, remainingBytes, 0);
        bufferSize = remainingBytes;
    }
    remainingBytes -= bufferSize;
    sentBytes += bufferSize;
}

服务器代码(在c#中)

        try
        {
            int bufferSize = 200;
            int len = 0;
            int receivedBytes = 0;
            int remainingBytes = len;
            byte[] length = new byte[4];
            //byte[] imgBuf = new byte[bufferSize];

            int current = 0;
            List<byte[]> coming = new List<byte[]>();

            sockets[number].Receive(length,4,0);

            len = BitConverter.ToInt32(length, 0);
            remainingBytes = len;

            bufferSize = len < bufferSize ? len : bufferSize;

            while(receivedBytes < len)
            {
                if (remainingBytes > bufferSize)
                {
                    coming.Add(new byte[bufferSize]);
                    //imgBuf = new byte[bufferSize];
                    sockets[number].Receive(coming[current], bufferSize, 0);
                }
                else
                {
                    coming.Add(new byte[remainingBytes]);
                    //imgBuf = new byte[remainingBytes];
                    sockets[number].Receive(coming[current], remainingBytes, 0);
                    bufferSize = remainingBytes;
                }
                remainingBytes -= bufferSize;
                receivedBytes += bufferSize;
                current++;
                //Array.Clear(imgBuf, 0, imgBuf.Length);
            }

            using (var stream = new FileStream(@"C:\receivedFile.exe",FileMode.Create))
            {
                using (var binaryWriter = new BinaryWriter(stream))
                {
                    foreach (byte[] buffer in coming)
                    {
                        binaryWriter.Write(buffer);
                    }
                }

            }
        }
        catch (Exception ex)
        { this.setText(ex.Message, textBox2); }

编辑:感谢您的帮助,我得到了它的工作:)

        try
        {
            int bufferSize = 1024 * 100;
            int len = 0;
            int receivedBytes = 0;
            int remainingBytes = len;
            int reached = 0;

            byte[] length = new byte[4];
            byte[] imgBuf = new byte[bufferSize];

            int current = 0;
            sockets[number].Receive(length,4,0);

            len = BitConverter.ToInt32(length, 0);
            remainingBytes = len;

            bufferSize = len < bufferSize ? len : bufferSize;
            imgBuf = new byte[len];

            while (reached < len)
            {
                reached += sockets[number].Receive(imgBuf, receivedBytes, remainingBytes, 0);
                remainingBytes = len - reached;
                receivedBytes = reached;
                current++;
                //Array.Clear(imgBuf, 0, imgBuf.Length);
            }

            using (var stream = new FileStream(@"C:\putty.exe",FileMode.Create))
            {
                using (var binaryWriter = new BinaryWriter(stream))
                {
                        binaryWriter.Write(imgBuf);
                }
            }
            Array.Clear(imgBuf, 0, imgBuf.Length);
        }
        catch (Exception ex)
        { this.setText(ex.Message, textBox2); }

2 个答案:

答案 0 :(得分:4)

您不会检查sockets[number].Receive(coming[current], bufferSize, 0);中实际收到的字节数。它不必等于您声明的缓冲区大小。 另外,正如评论中所说,将整个文件保存在内存中并不是一个好主意。

答案 1 :(得分:2)

除了检查接收的字节数之外,还需要检查每个发送调用发送的字节数 - 如果TCP传输窗口填满,发送调用可能发送的数据少于您请求的数据,在这种情况下你需要重新发送未发送的数据。

通常,您总是需要检查系统调用的返回值,以检查可能发生的所有各种奇数情况。阅读send(2)和recv(2)的手册页,获取可能发生的所有事情的完整列表。