C++ 套接字应用程序无法正常工作

C++ 套接字应用程序无法正常工作,c++,c,sockets,winsock,winsock2,C++,C,Sockets,Winsock,Winsock2,我试图写一个小代码在Windows上通过网络发送文件,但它似乎不能正常工作。 这是我的代码: char *arrFile = readFile("test.exe"); int fileSize = getFileSize("test.exe"); int sentSize = 0; int justSent; while(sentSize < fileSize) { justSent = send(sock, arrFile + sentSize, fileSize - sen

我试图写一个小代码在Windows上通过网络发送文件,但它似乎不能正常工作。 这是我的代码:

char *arrFile = readFile("test.exe");
int fileSize = getFileSize("test.exe");
int sentSize = 0;
int justSent;

while(sentSize < fileSize) {
    justSent = send(sock, arrFile + sentSize, fileSize - sentSize, 0);
    sentSize += justSent;
}
char*arrFile=readFile(“test.exe”);
int fileSize=getFileSize(“test.exe”);
int-sentSize=0;
int刚刚发送;
while(sentSize
经过几次循环后,它在发送函数中出错,我不知道为什么,有人能告诉我原因(以及原因的解决方案):D

更新

我正在使用非阻塞套接字并收到“WSAEWOULDBLOCK”错误,但在客户端返回某些内容之前,它仍然不会在错误后发送任何内容:-(


以上代码为C,C++也为:d< /p> < p>看起来函数<代码>发送<代码>不正确工作。它返回一个-1,然后添加到<代码> JavaStest。经过几次迭代,<代码> JuestEnter < /C> >足够负,导致分割错误。

要解决此问题,您应该添加代码来处理错误条件(当
send
返回值<0时)

像这样的事情将是一个良好的开端:

while(sentSize < fileSize) 
{
    justSent = send(sock, arrFile + sentSize, fileSize - sentSize, 0);
    if(justSent < 0)
    {
        printf("Error!\n");
        break;
    }
    sentSize += justSent;
}
while(sentSize
什么错误?如果您需要特定的帮助,您必须更加具体。您应该检查send()返回代码。如果是SOCKET\u错误,您应该调用WSAGetLastError()并停止循环(而不是将SOCKET\u错误计数为sentSize并停留在循环中)。很抱歉我的错误描述,我已更新:-(