/* Read */ int CTcpClient::Read(BYTE *pBuf, int iSize, int *piRet, char *psRet) { int m_iRet; int m_iRet2; char m_sRet[256]; int m_iSize; int m_nErr; m_iRet = 0; m_iRet2 = 0; strcpy(m_sRet,""); /* 1. 如果尚未连接,则返回失败! */ if (m_bIsConnected != true) { m_iRet = 0; m_iRet2= -1; strcpy(m_sRet,"尚未建立连接!"); goto L_RET; } m_iSize = (int)::recv(m_id, pBuf, iSize, 0); if (m_iSize == 0) { m_iRet = 0; m_iRet2 = -2; sprintf(m_sRet,"链接已断开:%d",GET_LAST_SOCK_ERROR()); goto L_RET; } // DumpBinary(pBuf, m_iSize); if (m_iSize == -1) { m_nErr = GET_LAST_SOCK_ERROR(); if (m_nErr == EWOULDBLOCK || m_nErr == 0) { m_iRet = 0; m_iRet2 = 0; goto L_RET; } m_iRet = 0; m_iRet2 = -3; sprintf(m_sRet,"GET_LAST_SOCK_ERROR():%d",m_nErr); goto L_RET; } m_iRet2 = m_iSize; m_iRet = 0; L_RET: if (piRet) *piRet = m_iRet; if (psRet) strcpy(psRet,m_sRet); return m_iRet2; }
int CTcpSocket::GetLastError(char *szMsgBuf, int MsgBLen) { int errcode = GET_LAST_SOCK_ERROR(); if (MsgBLen>0 && szMsgBuf!=NULL) { char msg[256]; switch(errcode) { #ifdef WIN32 case WSAEINTR: sprintf(msg,"%d:Interrupted function call. ",errcode); break; case WSAEACCES: sprintf(msg,"%d:Permission denied. ",errcode); break; case WSAEFAULT: sprintf(msg,"%d:Bad address. ",errcode); break; case WSAEINVAL: sprintf(msg,"%d:Invalid argument. ",errcode); break; case WSAEMFILE: sprintf(msg,"%d:Too many open files. ",errcode); break; case WSAEWOULDBLOCK: sprintf(msg,"%d:Resource temporarily unavailable. ",errcode); break; case WSAEINPROGRESS: sprintf(msg,"%d:Operation now in progress. ",errcode); break; case WSAEALREADY: sprintf(msg,"%d:Operation already in progress. ",errcode); break; case WSAENOTSOCK: sprintf(msg,"%d:Socket operation on nonsocket. ",errcode); break; case WSAEDESTADDRREQ: sprintf(msg,"%d:Destination address required. ",errcode); break; case WSAEMSGSIZE: sprintf(msg,"%d:Message too long. ",errcode); break; case WSAEPROTOTYPE: sprintf(msg,"%d:Protocol wrong type for socket. ",errcode); break; case WSAENOPROTOOPT: sprintf(msg,"%d:Bad protocol option. ",errcode); break; case WSAEPROTONOSUPPORT: sprintf(msg,"%d:Protocol not supported. ",errcode); break; case WSAESOCKTNOSUPPORT: sprintf(msg,"%d:Socket type not supported. ",errcode); break; case WSAEOPNOTSUPP: sprintf(msg,"%d:Operation not supported. ",errcode); break; case WSAEPFNOSUPPORT: sprintf(msg,"%d:Protocol family not supported. ",errcode); break; case WSAEAFNOSUPPORT: sprintf(msg,"%d:Address family not supported by protocol family. ",errcode); break; case WSAEADDRINUSE: sprintf(msg,"%d:Address already in use. ",errcode); break; case WSAEADDRNOTAVAIL: sprintf(msg,"%d:Cannot assign requested address. ",errcode); break; case WSAENETDOWN: sprintf(msg,"%d:Network is down. ",errcode); break; case WSAENETUNREACH: sprintf(msg,"%d:Network is unreachable. ",errcode); break; case WSAENETRESET: sprintf(msg,"%d:Network dropped connection on reset. ",errcode); break; case WSAECONNABORTED: sprintf(msg,"%d:Software caused connection abort. ",errcode); break; case WSAECONNRESET: sprintf(msg,"%d:Connection reset by peer. ",errcode); break; case WSAENOBUFS: sprintf(msg,"%d:No buffer space available. ",errcode); break; case WSAEISCONN: sprintf(msg,"%d:Socket is already connected. ",errcode); break; case WSAENOTCONN: sprintf(msg,"%d:Socket is not connected. ",errcode); break; case WSAESHUTDOWN: sprintf(msg,"%d:Cannot send after socket shutdown. ",errcode); break; case WSAETIMEDOUT: sprintf(msg,"%d:Connection timed out. ",errcode); break; case WSAECONNREFUSED: sprintf(msg,"%d:Connection refused. ",errcode); break; case WSAEHOSTDOWN: sprintf(msg,"%d:Host is down. ",errcode); break; case WSAEHOSTUNREACH: sprintf(msg,"%d:No route to host. ",errcode); break; case WSAEPROCLIM: sprintf(msg,"%d:Too many processes. ",errcode); break; case WSASYSNOTREADY: sprintf(msg,"%d:Network subsystem is unavailable. ",errcode); break; case WSAVERNOTSUPPORTED: sprintf(msg,"%d:Winsock.dll version out of range. ",errcode); break; case WSANOTINITIALISED: sprintf(msg,"%d:Successful WSAStartup not yet performed. ",errcode); break; case WSAEDISCON: sprintf(msg,"%d:Graceful shutdown in progress. ",errcode); break; case WSATYPE_NOT_FOUND: sprintf(msg,"%d:Class type not found. ",errcode); break; case WSAHOST_NOT_FOUND: sprintf(msg,"%d:Host not found. ",errcode); break; case WSATRY_AGAIN: sprintf(msg,"%d:Nonauthoritative host not found. ",errcode); break; case WSANO_RECOVERY: sprintf(msg,"%d:This is a nonrecoverable error. ",errcode); break; case WSANO_DATA: sprintf(msg,"%d:Valid name, no data record of requested type. ",errcode); break; case WSA_INVALID_HANDLE: sprintf(msg,"%d:Specified event object handle is invalid. ",errcode); break; case WSA_INVALID_PARAMETER: sprintf(msg,"%d:One or more parameters are invalid. ",errcode); break; case WSA_IO_INCOMPLETE: sprintf(msg,"%d:Overlapped I/O event object not in signaled state. ",errcode); break; case WSA_IO_PENDING: sprintf(msg,"%d:",errcode); break; case WSA_NOT_ENOUGH_MEMORY: sprintf(msg,"%d:Insufficient memory available. ",errcode); break; case WSA_OPERATION_ABORTED: sprintf(msg,"%d:Overlapped operation aborted. ",errcode); break; #ifdef WSAINVALIDPROCTABLE case WSAINVALIDPROCTABLE: sprintf(msg,"%d:Invalid procedure table from service provider. ",errcode); break; #endif #ifdef WSAINVALIDPROVIDER case WSAINVALIDPROVIDER: sprintf(msg,"%d:Invalid service provider version number. ",errcode); break; #endif #ifdef WSAPROVIDERFAILEDINIT case WSAPROVIDERFAILEDINIT: sprintf(msg,"%d:Unable to initialize a service provider. ",errcode); break; #endif case WSASYSCALLFAILURE: sprintf(msg,"%d:System call failure. ",errcode); break; #else case 1 : sprintf(msg,"%d:Operation not permitted",errcode); break; case 2 : sprintf(msg,"%d:No such file or directory",errcode); break; case 3 : sprintf(msg,"%d:No such process",errcode); break; case 4 : sprintf(msg,"%d:interrupted system call",errcode); break; case 5 : sprintf(msg,"%d:I/O error",errcode); break; case 6 : sprintf(msg,"%d:No such device or address",errcode); break; case 7 : sprintf(msg,"%d:Arg list too long",errcode); break; case 8 : sprintf(msg,"%d:Exec format error",errcode); break; case 9 : sprintf(msg,"%d:Bad file descriptor",errcode); break; case 10 : sprintf(msg,"%d:No child processes",errcode); break; case 11 : sprintf(msg,"%d:Resource temporarily unavailable ",errcode); break; case 12 : sprintf(msg,"%d:Not enough space",errcode); break; case 13 : sprintf(msg,"%d:Permission denied",errcode); break; case 14 : sprintf(msg,"%d:Bad address",errcode); break; case 15 : sprintf(msg,"%d:Block device required",errcode); break; case 16 : sprintf(msg,"%d:Resource busy",errcode); break; case 17 : sprintf(msg,"%d:File exists",errcode); break; case 18 : sprintf(msg,"%d:Improper link",errcode); break; case 19 : sprintf(msg,"%d:No such device",errcode); break; case 20 : sprintf(msg,"%d:Not a directory",errcode); break; case 21 : sprintf(msg,"%d:Is a directory",errcode); break; case 22 : sprintf(msg,"%d:Invalid argument",errcode); break; case 23 : sprintf(msg,"%d:Too many open files in system",errcode); break; case 24 : sprintf(msg,"%d:Too many open files",errcode); break; case 25 : sprintf(msg,"%d:Inappropriate I/O control operation",errcode); break; case 26 : sprintf(msg,"%d:Text file busy",errcode); break; case 27 : sprintf(msg,"%d:File too large",errcode); break; case 28 : sprintf(msg,"%d:No space left on device",errcode); break; case 29 : sprintf(msg,"%d:Invalid seek",errcode); break; case 30 : sprintf(msg,"%d:Read only file system",errcode); break; case 31 : sprintf(msg,"%d:Too many links",errcode); break; case 32 : sprintf(msg,"%d:Broken pipe",errcode); break; case 33 : sprintf(msg,"%d:Domain error within math function",errcode); break; case 34 : sprintf(msg,"%d:Result too large",errcode); break; case 35 : sprintf(msg,"%d:No message of desired type",errcode); break; case 36 : sprintf(msg,"%d:Identifier removed",errcode); break; case 37 : sprintf(msg,"%d:Channel number out of range",errcode); break; case 38 : sprintf(msg,"%d:Level 2 not synchronized",errcode); break; case 39 : sprintf(msg,"%d:Level 3 halted",errcode); break; case 40 : sprintf(msg,"%d:Level 3 reset",errcode); break; case 41 : sprintf(msg,"%d:Link number out of range",errcode); break; case 42 : sprintf(msg,"%d:Protocol driver not attached",errcode); break; case 43 : sprintf(msg,"%d:No CSI structure available",errcode); break; case 44 : sprintf(msg,"%d:Level 2 halted",errcode); break; case 45 : sprintf(msg,"%d:Resource deadlock avoided",errcode); break; case 46 : sprintf(msg,"%d:Device not ready",errcode); break; case 47 : sprintf(msg,"%d:Write-protected media",errcode); break; case 48 : sprintf(msg,"%d:Unformatted media",errcode); break; case 49 : sprintf(msg,"%d:No locks available",errcode); break; case 50 : sprintf(msg,"%d:no connectioncase case",errcode); break; case 52 : sprintf(msg,"%d:no filesystemcase case",errcode); break; case 53 : sprintf(msg,"%d:old, currently unused AIX errno",errcode); break; case 54 : sprintf(msg,"%d:Operation would block",errcode); break; case 55 : sprintf(msg,"%d:Operation now in progress",errcode); break; case 56 : sprintf(msg,"%d:Operation already in progress",errcode); break; case 57 : sprintf(msg,"errno=%d:Socket operation on non-socket",errcode); break; case 58 : sprintf(msg,"errno=%d:Destination address required",errcode); break; case 59 : sprintf(msg,"errno=%d:Message too long",errcode); break; case 60 : sprintf(msg,"errno=%d:Protocol wrong type for socket",errcode); break; case 61 : sprintf(msg,"errno=%d:Protocol not available",errcode); break; case 62 : sprintf(msg,"errno=%d:Protocol not supported",errcode); break; case 63 : sprintf(msg,"errno=%d:Socket type not supported",errcode); break; case 64 : sprintf(msg,"errno=%d:Operation not supported on socket",errcode); break; case 65 : sprintf(msg,"errno=%d:Protocol family not supported",errcode); break; case 66 : sprintf(msg,"errno=%d:Address family not supported by protocol family",errcode); break; case 67 : sprintf(msg,"errno=%d:Address already in use",errcode); break; case 68 : sprintf(msg,"errno=%d:Can't assign requested address",errcode); break; /* operational errors */ case 69 : sprintf(msg,"errno=%d:Network is down",errcode); break; case 70 : sprintf(msg,"errno=%d:Network is unreachable",errcode); break; case 71 : sprintf(msg,"errno=%d:Network dropped connection on reset",errcode); break; case 72 : sprintf(msg,"errno=%d:Software caused connection abort",errcode); break; case 73 : sprintf(msg,"errno=%d:Connection reset by peer",errcode); break; case 74 : sprintf(msg,"errno=%d:No buffer space available",errcode); break; case 75 : sprintf(msg,"errno=%d:Socket is already connected",errcode); break; case 76 : sprintf(msg,"errno=%d:Socket is not connected",errcode); break; case 77 : sprintf(msg,"errno=%d:Can't send after socket shutdown",errcode); break; case 78 : sprintf(msg,"errno=%d:Connection timed out",errcode); break; case 79 : sprintf(msg,"errno=%d:Connection refused",errcode); break; case 80 : sprintf(msg,"errno=%d:Host is down",errcode); break; case 81 : sprintf(msg,"errno=%d:No route to host",errcode); break; case 82 : sprintf(msg,"%d:restart the system call",errcode); break; case 83 : sprintf(msg,"%d:Too many processes",errcode); break; case 84 : sprintf(msg,"%d:Too many users",errcode); break; case 85 : sprintf(msg,"%d:Too many levels of symbolic links",errcode); break; case 86 : sprintf(msg,"%d:File name too long",errcode); break; case 87 : sprintf(msg,"%d:Directory not empty",errcode); break; case 88 : sprintf(msg,"%d:Disc quota exceeded",errcode); break; case 89 : sprintf(msg,"%d:Invalid file system control data ",errcode); break; case 93 : sprintf(msg,"%d:Item is not local to host ",errcode); break; case 109 : sprintf(msg,"%d:Function not implemented POSIX ",errcode); break; case 110 : sprintf(msg,"%d:media surface error ",errcode); break; case 111 : sprintf(msg,"%d:I/O completed, but needs relocation ",errcode); break; case 112 : sprintf(msg,"%d:no attribute found ",errcode); break; case 113 : sprintf(msg,"%d:security authentication denied ",errcode); break; case 114 : sprintf(msg,"%d:not a trusted program ",errcode); break; case 115 : sprintf(msg,"%d:Too many references: can't splice ",errcode); break; case 116 : sprintf(msg,"%d:Invalid wide character ",errcode); break; case 117 : sprintf(msg,"%d:asynchronous i/o cancelled ",errcode); break; case 118 : sprintf(msg,"%d:temp out of streams resources ",errcode); break; case 119 : sprintf(msg,"%d:I_STR ioctl timed out ",errcode); break; case 120 : sprintf(msg,"%d:wrong message type at stream head ",errcode); break; case 121 : sprintf(msg,"%d:STREAMS protocol error ",errcode); break; case 122 : sprintf(msg,"%d:no message ready at stream head ",errcode); break; case 123 : sprintf(msg,"%d:fd is not a stream ",errcode); break; case 124 : sprintf(msg,"%d:POSIX threads unsupported value ",errcode); break; case 125 : sprintf(msg,"%d:multihop is not allowed ",errcode); break; case 126 : sprintf(msg,"%d:the link has been severed ",errcode); break; case 127 : sprintf(msg,"%d:value too large to be stored in data type ",errcode); break; #endif default : sprintf(msg,"errno=%d:No error description",errcode); break; } strncpy(szMsgBuf,msg,MsgBLen-1); szMsgBuf[MsgBLen-1]='\0'; } return(errcode); }
/* Send */ int CTcpClient::Send(BYTE *pBuf, int iSize, int *piRet, char *psRet) { int m_iRet; int m_iRet2; char m_sRet[256]; int m_iSize; int m_nErr; m_iRet = 0; m_iRet2 = 0; strcpy(m_sRet,""); /* 1. 如果尚未连接,则返回失败! */ if (m_bIsConnected != true) { m_iRet = 0; m_iRet2= -1; strcpy(m_sRet,"尚未建立连接!"); goto L_RET; } /* DEBUG */ #if 0 printf("Send:%d bytes\n",iSize); for(i=0;i<iSize;i++) { if (i%8 ==0 && i!=0) printf("\n"); printf("%02x ",(unsigned)pBuf[i]); } printf("\n"); #endif m_iSize = (int)::send(m_id,pBuf,iSize,0); if (m_iSize == 0) { m_iRet = 0; m_iRet2 = -2; strcpy(m_sRet,"链接已断开"); goto L_RET; } if (m_iSize <= 0) { m_nErr = GET_LAST_SOCK_ERROR(); #ifdef AIX if (m_nErr == EWOULDBLOCK || m_nErr == 2 || m_nErr == 0) #elif defined(HP_UX) if (m_nErr == EWOULDBLOCK || m_nErr == 16 || m_nErr == 0 || m_nErr == 2 || m_nErr == 251) #else if (m_nErr==EWOULDBLOCK) #endif { m_iRet = 0; m_iRet2 = 0; goto L_RET; } else { m_iRet = 0; m_iRet2 = -3; sprintf(m_sRet,"GET_LAST_SOCK_ERROR():%d",m_nErr); goto L_RET; } } m_iRet2 = m_iSize; m_iRet = 0; L_RET: if (piRet) *piRet = m_iRet; if (psRet) strcpy(psRet,m_sRet); return m_iRet2; }
bool CTcpClient::Create(int *piRet, char *psRet) { int m_iRet; bool m_bRet; char m_sRet[256]; int on; m_iRet = 0; m_bRet = true; strcpy(m_sRet,""); m_id = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (m_id<0) { m_bRet = false; m_iRet = -1; strcpy(m_sRet,"Can not create socket for tcp client"); goto L_RET; } setsockopt(m_id, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); for (;;) { #ifdef UNIX on=1; if (ioctlsocket(m_id, FIONBIO, (char *)&on) < 0) #endif #ifdef WIN32 unsigned long on_windows=1; if (ioctlsocket(id, FIONBIO, &on_windows) < 0) #endif #ifdef VOS int off=0; if (ioctlsocket(id, FIONBIO, (char *)&off) <0) #endif { // printf("%d\n",GET_LAST_SOCK_ERROR()); if (GET_LAST_SOCK_ERROR() == EINTR) continue; m_iRet = -2; m_bRet = false; strcpy(m_sRet,"Can not set FIONBIO for socket"); closesocket(m_id); goto L_RET; } break; } m_bIsConnected = false; m_bIsConnecting = false; L_RET: if (m_bRet == false) m_id = 0; if (piRet) *piRet = m_iRet; if (psRet) strcpy(psRet,m_sRet); return m_bRet; }
/* 返回2:进入异步 */ int CTcpClient::WaitConnect(int *piRet, char *psRet) { int m_iRet; int m_iRet2; char m_sRet[256]; fd_set writefds; struct timeval timeout; int ret; struct sockaddr name2; #ifdef MAC_OS socklen_t nameLen = sizeof(name2); #else SOCKADDRLEN nameLen=sizeof(name2); #endif m_iRet = 0; m_iRet2 =0; strcpy(m_sRet,""); /* 1. 如果已经连接,则返回成功! */ if (m_bIsConnected == true) { m_iRet = 0; m_iRet2 = 1; strcpy(m_sRet,"已经连接成功!"); goto L_RET; } /* 2. 如果还没有开始连接,则返回失败 */ if (m_bIsConnecting == false) { m_iRet = 0; m_iRet2 = 0; strcpy(m_sRet,"尚未开始连接"); goto L_RET; } /* use select to wait for it */ FD_ZERO(&writefds); FD_SET(m_id,&writefds); timeout.tv_sec=0; timeout.tv_usec=100; ret=select(m_id+1,NULL,&writefds,NULL,&timeout); /* timeout? then return with fail */ if (ret==0) { m_iRet2 =2 ; m_iRet = 0; strcpy(m_sRet,""); goto L_RET; } /* recheck whether connected */ // printf("Getpeername:%d,%d\n",ret,GET_LAST_SOCK_ERROR()); if ((ret>0)&&FD_ISSET(m_id,&writefds)) { nameLen=sizeof(name2); memset(&name2,0,sizeof(name2)); ret =getpeername(m_id,(sockaddr*)&name2,&nameLen); if (ret ==0) { m_iRet = 0; m_iRet2 = 1; m_bIsConnecting = false; m_bIsConnected = true; goto L_RET; } /* 这个很奇怪,连接某些时候就是getpeername会失败,返回EINVAL,可能和服务端有关? */ if (GET_LAST_SOCK_ERROR()==EINVAL) { m_iRet = 0; m_iRet2 = 1; m_bIsConnecting = false; m_bIsConnected = true; goto L_RET; } // printf("Getpeername:%d,%d\n",ret,GET_LAST_SOCK_ERROR()); } m_iRet = -1; m_iRet2 = 0; sprintf(m_sRet,"Can not connect to Server[%d]!",GET_LAST_SOCK_ERROR()); CloseSocket(); L_RET: if (piRet) *piRet = m_iRet; if (psRet) strcpy(psRet,m_sRet); return m_iRet2; }