static int SSLNetOpen(char *remote_addr, short remote_port, short local_port, long flag) { int iErrMsg, iRet; NET_SOCKADDR stSocketAddr; if(s_iSocket>= 0) { DebugOutput("%s--%d, s_iSocket:%d\n", __FILE__, __LINE__, s_iSocket); return s_iSocket; } s_iSocket = NetSocket(NET_AF_INET, NET_SOCK_STREAM, 0); DebugOutput("%s--%d--%s, s_iSocket:%d\n", __FILE__, __LINE__, __FUNCTION__, s_iSocket);//linzhao if (s_iSocket < 0) { return s_iSocket; } SockAddrSet(&stSocketAddr, remote_addr, remote_port); iErrMsg = NetConnect(s_iSocket, &stSocketAddr, sizeof(stSocketAddr)); DebugOutput("%s--%d--%s, iErrMsg:%d\n", __FILE__, __LINE__, __FUNCTION__, iErrMsg);//linzhao if (iErrMsg < 0) { iRet = NetCloseSocket(s_iSocket); DebugOutput("%s--%d, iRet;%d, s_iSocket:%d\n", __FILE__, __LINE__, iRet, s_iSocket); //linzhao //if (0==iRet) // s_iSocket = -1; switch (iErrMsg) { case NET_ERR_TIMEOUT: return -ERR_SSL_TIMEOUT; case NET_ERR_RST: return -ERR_SSL_NET; default: return iErrMsg; } } return s_iSocket; }
int WifiStartServer(void) { int iRet; struct net_sockaddr server_addr; if(server_socket_id >= 0) { NetCloseSocket(server_socket_id); server_socket_id = -1; } iRet = NetSocket(NET_AF_INET, NET_SOCK_STREAM, 0); if(iRet < 0) { return iRet; } server_socket_id = iRet; iRet = SockAddrSet(&server_addr, glSysCtrl.szLocalIp, (short)atoi(glSysCtrl.szLocalPort)); if(iRet < 0) { return iRet; } iRet = NetBind(server_socket_id, &server_addr, sizeof(server_addr)); if(iRet < 0) { return iRet; } iRet = NetListen(server_socket_id, 5); if(glSysCtrl.bDebugOpt) { DebugOutput("listen start = [%d]", iRet); } return iRet; }
NetSocket Net::openListenPort(U16 port) { if(Journal::IsPlaying()) { U32 ret; Journal::Read(&ret); return NetSocket(ret); } NetSocket sock = openSocket(); if (sock == InvalidSocket) { Con::errorf("Unable to open listen socket: %s", strerror(errno)); return InvalidSocket; } if (bind(sock, port) != NoError) { Con::errorf("Unable to bind port %d: %s", port, strerror(errno)); ::closesocket(sock); return InvalidSocket; } if (listen(sock, 4) != NoError) { Con::errorf("Unable to listen on port %d: %s", port, strerror(errno)); ::closesocket(sock); return InvalidSocket; } setBlocking(sock, false); addPolledSocket(sock, Listening); if(Journal::IsRecording()) Journal::Write(U32(sock)); return sock; }
NetSocket Net::openConnectTo(const char *addressString) { if(!dStrnicmp(addressString, "ipx:", 4)) // ipx support deprecated return InvalidSocket; if(!dStrnicmp(addressString, "ip:", 3)) addressString += 3; // eat off the ip: char remoteAddr[256]; dStrcpy(remoteAddr, addressString); char *portString = dStrchr(remoteAddr, ':'); U16 port; if(portString) { *portString++ = 0; port = htons(dAtoi(portString)); } else port = htons(defaultPort); if(!dStricmp(remoteAddr, "broadcast")) return InvalidSocket; if(Journal::IsPlaying()) { U32 ret; Journal::Read(&ret); return NetSocket(ret); } NetSocket sock = openSocket(); setBlocking(sock, false); sockaddr_in ipAddr; dMemset(&ipAddr, 0, sizeof(ipAddr)); ipAddr.sin_addr.s_addr = inet_addr(remoteAddr); if(ipAddr.sin_addr.s_addr != INADDR_NONE) { ipAddr.sin_port = port; ipAddr.sin_family = AF_INET; if(::connect(sock, (struct sockaddr *)&ipAddr, sizeof(ipAddr)) == -1) { S32 err = getLastError(); if(err != Net::WouldBlock) { Con::errorf("Error connecting %s: %s", addressString, strerror(err)); ::closesocket(sock); sock = InvalidSocket; } } if(sock != InvalidSocket) { // add this socket to our list of polled sockets addPolledSocket(sock, ConnectionPending); } } else { // need to do an asynchronous name lookup. first, add the socket // to the polled list addPolledSocket(sock, NameLookupRequired, remoteAddr, port); // queue the lookup gNetAsync.queueLookup(remoteAddr, sock); } if(Journal::IsRecording()) Journal::Write(U32(sock)); return sock; }
int wifiConnect(uchar *inIp, uchar *inPort) { int i = 0; int ret; int trans_port; int event; int ret_num; char sBuff[33]; ST_WIFI_AP WifiAp; struct net_sockaddr server_addr; for (i = 0;i < 1; i++) // change 5 to 1 by wuc 2014.9.2 { ret = WifiCheck(&WifiAp); if (ret < 0) { ret = WifiApLogin(); if (ret != 0 && i != 4) { continue; } else if (ret != 0 && i == 4) { return ret; } else { break; } } else if (ret == 0) { DelayMs(500); continue; } else { break; } } trans_port = atoi(inPort); //如果TCP仍然连接,就不用再连接 if(wnet_socket_id >= 0) { event = Netioctl(wnet_socket_id, CMD_EVENT_GET, 0); if(event&(SOCK_EVENT_CONN|SOCK_EVENT_WRITE|SOCK_EVENT_READ)) { return 0; } } if(wnet_socket_id >= 0) { NetCloseSocket(wnet_socket_id); wnet_socket_id = -1; } ret = NetSocket(NET_AF_INET, NET_SOCK_STREAM, 0); if(ret < 0) { return -1; } wnet_socket_id = ret; /*ret = Netioctl(wnet_socket_id, CMD_TO_SET, 30*1000);//advice from S/W department if (ret != 0) { DebugOutput("ret = [%d]", ret); return ret; }*/ SockAddrSet(&server_addr, (char*)inIp, (short)trans_port); ret_num = NetConnect(wnet_socket_id, &server_addr, sizeof(server_addr)); if(ret_num < 0) { switch (ret_num) { case NET_ERR_ABRT: case NET_ERR_CONN: case NET_ERR_IF://linzhao strcpy(sBuff,_T("CONN FAIL")); break; case NET_ERR_RST: strcpy(sBuff,_T("CONN RESET")); break; case NET_ERR_CLSD: strcpy(sBuff,_T("CONN CLOSE")); break; case NET_ERR_RTE: strcpy(sBuff,_T("ROUTE ERR")); break; case NET_ERR_ISCONN: strcpy(sBuff,_T("CONN EXIST")); break; case NET_ERR_TIMEOUT: strcpy(sBuff,_T("CONN TIMOUT")); break; case NET_ERR_PASSWD: strcpy(sBuff,_T("PASSWORD ERR")); break; case NET_ERR_MODEM: strcpy(sBuff,_T("PPP FAIL")); break; case NET_ERR_LINKDOWN: case NET_ERR_LOGOUT: case NET_ERR_PPP: strcpy(sBuff,_T("RETRY PPP")); break; default: strcpy(sBuff,_T("UNKNOW ERR")); break; } ScrClrBelow(2); PubDispString(sBuff, DISP_LINE_LEFT|2); //Cielo要求去除返回码, linzhao // sprintf(sBuff, _T("code=%d "), ret_num); // PubDispString(sBuff, DISP_LINE_LEFT|3); PubWaitKey(2); NetCloseSocket(wnet_socket_id); wnet_socket_id = -1; return -1; } else return 0; }