Client端接收Server端回应数据问题。。。。。。。。。

yydyqy 2003-06-24 11:38:19
我的程序从Client端不断向Server端发送请求,每发送一个请求,就等待Server端回应,受到回应后发送下一请求。现在的问题是第一次发送能正确受到回应,第二次时recv函数返回10053错误(An established connection was aborted by the software in your host machine, possibly due to a data transmission time-out or protocol error. )如果是protocol error,那第一次发送就收不到正确回应,如果是data transmission time-out,我已经用Select模型判断了可以接受才开始recv,请问以上现象的原因是?该如何解决?下面是一些关键代码,有时间的哥们可以帮忙看看,谢谢

int CTcmSms::SendSM(LPCTSTR lpszSrcMsisdn, LPCTSTR lpszDestMsisdn, LPCTSTR lpszContent)
{
int nRet;
memset(&m_SendMsg, 0, sizeof(m_SendMsg));
m_SendMsg.type = SERV_SEND;
m_SendMsg.coding = 1; // 0x01 -- big5
strcpy(m_SendMsg.pchMessage, lpszContent); //傳送之訊息
strcpy(m_SendMsg.pchMsisdn, lpszSrcMsisdn); //受訊方的手機號碼
memcpy(m_SendMsg.pchMessageID, lpszSrcMsisdn, 9);

//if send nonText message, you con’t get length by following method
m_SendMsg.length = strlen(lpszContent);

if (1) // 加入即时传送的条件
{
m_SendMsg.tran_type = SEND_NOW;
}
else //預約傳送
{
char* pTime = "030510120000";
m_SendMsg.tran_type = SEND_ORDER;
strcpy(m_SendMsg.pchSendTime, pTime);
}

if( SendToSns(m_hSocket, m_SendMsg, m_RecvMsg) )
{
switch(m_RecvMsg.code)
{
case 0: // Send successfully.
nRet = 0;
break;

case 1:
nRet = 1;
break;
}
}

return nRet;
}

bool CTcmSms::SendToSns(SOCKET &sock, SendMsg &sendMsg, RecvMsg &recvMsg)
{
int nRet;
if( !IsConnected() )
{
// Wait 20 seconds to connect server again.
Sleep(20 * 1000);

if( !Connect(m_SvrInfo.szSvrName, m_SvrInfo.nPort) )
return false;
if( Login(m_SvrInfo.szSvrUserId, m_SvrInfo.szSvrPwd) != 0 )
return false;
}

if( SelectWrite() )
{
if(send(sock, (char *)&sendMsg, sizeof(SendMsg), 0) == SOCKET_ERROR)
{
// socket send error
//closesocket(sock);
m_bConnected = false;
cout << "Send error: " << GetLastError() << endl;
return false;
}
}

memset((char *)&recvMsg, 0, sizeof(RecvMsg));
if( SelectRead() )
{
nRet = recv(sock, (char *)&recvMsg, sizeof(RecvMsg), 0);
if((nRet == SOCKET_ERROR) || (nRet <= 0))
{
// receive error or server close connection
//closesocket(sock);
m_bConnected = false;
cout << "Receive error: " << GetLastError() << endl;
return false;
}
}

return true;
}

bool CTcmSms::SelectRead()
{
int nRet = 0;
bool bOk = false;

fd_set fdRead;
FD_ZERO(&fdRead);
FD_SET(m_hSocket, &fdRead);
TIMEVAL tv = {m_nTimeOut, 0};
nRet = select(0, &fdRead, NULL, NULL, &tv);
if( 0 == nRet || SOCKET_ERROR == nRet )
{
cout << "Select(Read) error: " << GetLastError() << endl;
}
else
{
if( FD_ISSET(m_hSocket, &fdRead) )
bOk = true;
}

return bOk;
}


bool CTcmSms::SelectWrite()
{
int nRet = 0;
bool bOk = false;

fd_set fdwrite;
FD_ZERO(&fdwrite);
FD_SET(m_hSocket, &fdwrite);
TIMEVAL tv = {m_nTimeOut, 0};
nRet = select(0, NULL, &fdwrite, NULL, &tv);
if( 0 == nRet || SOCKET_ERROR == nRet )
{
cout << "Select(Write) error: " << GetLastError() << endl;
}
else
{
if( FD_ISSET(m_hSocket, &fdwrite) )
bOk = true;
}

return bOk;
}
...全文
11 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
alanwang_ 2003-06-24
也有可能是server调用了closesocket
回复
yydyqy 2003-06-24
每人遇到过类似问题吗?这个问题挺急的,请大家帮帮忙,谢谢!
回复
yydyqy 2003-06-24
to awnucel(三十功名臣与土※八千里路云和月)(峰宸):

套接字操作遇到了一个已死的网络
出错的是SendToSns函数中的nRet = recv(sock, (char *)&recvMsg, sizeof(RecvMsg), 0);
这一步,为何第一次成功,第二次到这就会遇到问题
回复
catyou 2003-06-24
IsConnected() 函数是如何操作的?如果你判断不正确, 则第二次connect,可能会出错,
回复
曾经的猎狐 2003-06-24
套接字操作遇到了一个已死的网络
回复
alanwang_ 2003-06-24
还有,select(fdRead)返回成功之后,不一定表示有数据可读!对等方关闭socket、网络失败等事件也会设置fdRead,此时就需要recv一下 ---- 如果成功就是有数据过来了,如果失败就是socket异常
回复
发动态
发帖子
网络编程
创建于2007-09-28

1.8w+

社区成员

VC/MFC 网络编程
申请成为版主
社区公告
暂无公告