MFC Socket通信 Receive返回值求助!
就是我在进行文件传输的时候。客户端接收文件。但是调试时Receive函数的返回值为-1,令dw=m_sConnectSocket.Receive(pBuf,pBufSiz...
就是我在进行文件传输的时候。客户端接收文件。但是调试时Receive函数的返回值为-1,令dw=m_sConnectSocket.Receive(pBuf,pBufSize).查看dw的值是一个465438925,特别大的字,这是怎么回事啊。我的服务器端是发送成功了的。。就是因为dw的值不对,后面的文件也写不了。我查资料。人说是连接断开了。但是我是TCP,创建的是流式套接字啊。。为什么在我没有启动断开时断开呢。。
展开
2个回答
展开全部
Receive返回值为-1,就是SOCKET_ERROR 。这时候应该调用GetLastError()来继续判断是什么错误类型。具体的可以查看MSDN的CAsyncSocket::Receive的说明。
另外,既然dw=m_sConnectSocket.Receive(pBuf,pBufSize),而Receive的返回值又是-1,那么dw不应该是一个大数字,就应该是-1。
调用时应当是如下代码:
dw=m_sConnectSocket.Receive(pBuf,pBufSize).
if(dw==SOCKET_ERROR)
{
int iErr;
iErr = GetLastError();
switch(iErr)
{
//进行错误处理,case语句族
}
}
else
{
//正常处理收到的数据。
}
MSDN相关内容:
virtual int Receive( void* lpBuf, int nBufLen, int nFlags = 0 );
Return Value
If no error occurs, Receive returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:
WSANOTINITIALISED A successful AfxSocketInit must occur before using this API.
WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed.
WSAENOTCONN The socket is not connected.
WSAEINPROGRESS A blocking Windows Sockets operation is in progress.
WSAENOTSOCK The descriptor is not a socket.
WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not of type SOCK_STREAM.
WSAESHUTDOWN The socket has been shut down; it is not possible to call Receive on a socket after ShutDown has been invoked with nHow set to 0 or 2.
WSAEWOULDBLOCK The socket is marked as nonblocking and the Receive operation would block.
WSAEMSGSIZE The datagram was too large to fit into the specified buffer and was truncated.
WSAEINVAL The socket has not been bound with Bind.
WSAECONNABORTED The virtual circuit was aborted due to timeout or other failure.
WSAECONNRESET The virtual circuit was reset by the remote side.
另外,既然dw=m_sConnectSocket.Receive(pBuf,pBufSize),而Receive的返回值又是-1,那么dw不应该是一个大数字,就应该是-1。
调用时应当是如下代码:
dw=m_sConnectSocket.Receive(pBuf,pBufSize).
if(dw==SOCKET_ERROR)
{
int iErr;
iErr = GetLastError();
switch(iErr)
{
//进行错误处理,case语句族
}
}
else
{
//正常处理收到的数据。
}
MSDN相关内容:
virtual int Receive( void* lpBuf, int nBufLen, int nFlags = 0 );
Return Value
If no error occurs, Receive returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:
WSANOTINITIALISED A successful AfxSocketInit must occur before using this API.
WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed.
WSAENOTCONN The socket is not connected.
WSAEINPROGRESS A blocking Windows Sockets operation is in progress.
WSAENOTSOCK The descriptor is not a socket.
WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not of type SOCK_STREAM.
WSAESHUTDOWN The socket has been shut down; it is not possible to call Receive on a socket after ShutDown has been invoked with nHow set to 0 or 2.
WSAEWOULDBLOCK The socket is marked as nonblocking and the Receive operation would block.
WSAEMSGSIZE The datagram was too large to fit into the specified buffer and was truncated.
WSAEINVAL The socket has not been bound with Bind.
WSAECONNABORTED The virtual circuit was aborted due to timeout or other failure.
WSAECONNRESET The virtual circuit was reset by the remote side.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,我也遇到了和你一样的问题 就是调用Receive()的时候返回一个很大的数 收到的根本不是真实的数据,不知当时你后面是怎么解决的呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询