[ad_1]
你好,
我正在尝试与套接字编程相关,但我的事情是长时间连接,这是断线连接。 但每隔 1 分钟,连接就会由于接收失败而重置连接,错误编号:22 错误消息:无效参数。 我尝试了最大程度的分析,但我无法找出我的错误。 请帮助我进行进一步的处理。
下面的代码只是一个示例,但我在我的项目中使用相同的套接字编程重要功能。
#include<iostream> using namesapce std; int g_SocFd = -1; bool Connect(char *p_IpAddress, int p_Port) { if (0 > (g_SocFd = socket(AF_INET, SOCK_STREAM, 0))) { g_Logger.log(LOG_LEVEL_NORMAL, "%s <Line:%d> Failed to Create client Socket. [ErrNo:%d,ErrMsg:%s]", __FUNCTION__, __LINE__, errno, strerror(errno)); g_SocFd = -1; return false; } g_Logger.log(LOG_LEVEL_DEBUG, "%s <Line:%d> IP_ADDRESS:%s PORT_NUMBER: %d", __FUNCTION__, __LINE__, p_IpAddress, p_Port); struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(p_IpAddress); serv_addr.sin_port = htons(p_Port); if (0 > connect(g_SocFd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) { g_Logger.log(LOG_LEVEL_CRITICAL, "%s <Line:%d> Failed to Connect [SockFd:%d,ErrNo:%d,ErrMsg:%s]", __FUNCTION__, __LINE__, g_SocFd, errno, strerror(errno)); close(g_SocFd); g_SocFd = -1; return false; } g_ConnectionFlag = true; return true; } int RecvAll(int &s, TEXT *buf, UINT32 len) { unsigned long total = 0; int bytesleft = len; int n; int l_Count = 0; while(total < len) { if(0>s) return -1; n = recv(s, buf+total, bytesleft,0); int l_errno = errno; if (n <= 0) { if(EINTR == errno) { if((++l_Count) == 5) { close(s); return 0; } if(true == g_CloseConnection) { cout<<"failing recv for Graceful exit.\n"; return 0; } continue; } close(s); return n; } total += n; bytesleft -= n; } len = total; // return number actually sent here return len; } bool ReadMessage() { while(1) { fd_set l_FdSet; int l_MaxFd = -1; struct timeval tv; FD_ZERO(&l_FdSet); if((true == g_ConnectionFlag)) { FD_SET(g_SocFd, &l_FdSet); if(g_SocFd > l_MaxFd) l_MaxFd = g_SocFd; } if(-1 == l_MaxFd) return false; tv.tv_sec = 5; tv.tv_usec = 0; if (-1 == select(l_MaxFd+1, &l_FdSet, 0, 0, &tv)) { g_Logger.log(LOG_LEVEL_CRITICAL, "%s <Ln:%d> Error in select [ErrMsg:%s]", __FUNCTION__, __LINE__, strerror(errno)); return false; } if(true == g-ConnectionFlag) { if(FD_ISSET(g_SocFd, &l_FdSet)) { int num_charread; char l_LenStr[10]; memset(l_LenStr, '\0', sizeof(l_LenStr)); num_charread = RecvAll(g_SocFd, l_LenStr, 9); if (num_charread < 0) { close(g_SocFd); g_ConnectionFlag = false; return false; } else if (0 == num_charread) { close(g_SocFd); g_ConnectionFlag = false; return false; } else { cout<<"String : "<<l_LenStr<<endl; } } } }} string g_ReqXml = ""; void SendMessage() { while(1) { if(g_ReqXml.length() > 0) { if((g_ConnectionFlag == true) && (g_SocFd > 2)) { if (-1 == send(g_SocFd, g_ReqXml.c_str(), g_ReqXml.length(), 0)) { g_Logger.log(LOG_LEVEL_WARNING, "%s <Ln:%d> Again pushed in Queue, due to sending failed.", __FUNCTION__, __LINE__); usleep(2000000); return false; } } }}} int main() { Connect("123.123.123.123", 8970); thread_t t1(SendMessage); thread_t t2(ReadMessage); sleep(2); g_ReqXml = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; sleep(2); g_ReqXml = "FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; sleep(2); g_ReqXml = "EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; sleep(2); g_ReqXml = "DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; sleep(2); g_ReqXml = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; sleep(2); g_ReqXml = "BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; sleep(2); getchar(); }
我尝试过的:
尝试过,但仅从网络获取无效参数作为错误代码:22。
解决方案1
首先:记录所有 close() 调用。 我想有一个你不想被处决的人。
但通俗地说:你需要调试它。
因为您写道这是一些计时问题,所以您需要在调试日志中添加时间戳。
断开连接也可能由您通信的服务器触发。 检查是否有一些断开连接计时器或不活动激活。
[ad_2]
コメント