C# SOCKET接收问题 小文件可以,大文件无响应

我的接收代码参照网上现有的具体如下:
public static byte[] ReceiveVarData(Socket s) // return array that store the received data.
{
int total = 0;
int recv;
byte[] datasize = new byte[4];
LogUtil.writeLog("log.txt", "start to receive data");
byte[] data = null;
;

        if (!s.Poll(200, SelectMode.SelectRead))
        {
            LogUtil.writeLog("log.txt", "socket can not selectRead");
             data = null;
             return data;
        }

        if (!(s.Available>0))
        {
            LogUtil.writeLog("log.txt", "socket is not Available");
            data = null;
            return data;
        }

        recv = s.Receive(datasize, 0, 4, SocketFlags.None);//receive the size of data array for initialize a array.

        LogUtil.writeLog("log.txt", "socket receive finish step1");
        LogUtil.writeLog("log.txt", "recv====" + recv); 
        int size = BitConverter.ToInt32(datasize, 0);
        LogUtil.writeLog("log.txt", "size====" + size); 
        int dataleft = size;
        data = new byte[size];
        int index = 1;
        while (total < size)
        {

            if (!s.Poll(200, SelectMode.SelectRead))
            {
                LogUtil.writeLog("log.txt", "index = " + index + "socket can not selectRead");
                data = null;
                return data;
            }

            if (!(s.Available > 0))
            {
                LogUtil.writeLog("log.txt", "index = " + index + "socket is not Available");
                data = null;
                return data;
            }

            recv = s.Receive(data, total, dataleft, SocketFlags.None);
            LogUtil.writeLog("log.txt", "total====" + total);
            LogUtil.writeLog("log.txt", "recv====" + recv);
            LogUtil.writeLog("log.txt", "dataleft====" + dataleft);
            if (recv == 0)
            {
                data = null;
                break;
            }
            total += recv;
            dataleft -= recv;
            index++;
        }

        LogUtil.writeLog("log.txt", "socket receive finish step2");   
        return data;

    }

            现在该方法接收1M左右文件可以接收到,大文件就无法接收到。log日志如照片所示,请问我是哪方面写的有问题呢?
        ![图片说明](https://img-ask.csdn.net/upload/201611/04/1478227739_856916.jpg)![图片说明](https://img-ask.csdn.net/upload/201611/04/1478227792_268087.jpg)

1个回答

total += recv;
这个效率太低
应该用StringBuilder去拼接字符串

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问