1 int SessionBase::ServerSock() 2 { 3 /* 4 * WSADATA是个结构体,在WSAStartup中被填充. 5 * WSAStartup为调用WinSock准备初始化的工作. 6 * 第一个参数指定编程使用的WinSock规范的最高版本号. 7 * WSAStartup成功返回0.失败则程序退出. 8 */ 9 WSADATA wsaData; 10 if((WSAStartup(0x101,&wsaData)) != 0)11 {12 return 0;13 }14 SOCKET clientSock; SOCKET serverSock;15 sockaddr_in local;16 sockaddr_in from;17 int fromlen=sizeof(from);18 19 local.sin_family = AF_INET;20 local.sin_addr.s_addr = inet_addr( m_strPorxyAddr.c_str() );21 local.sin_port = htons( m_ProxyPort );22 23 if((socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)24 {25 return 0;26 }27 28 if(bind(serverSock,(sockaddr*)&local,sizeof(local))!=0)29 {30 return 0;31 }32 33 if(listen(serverSock,10)!=0) /* backlog表示最大的连接数 */34 {35 return 0;36 }37 38 while(TRUE)39 {40 char temp[2048];41 42 clientSock=accept(serverSock,(struct sockaddr*)&from,&fromlen);43 logger.writeLog("SessionBase Server socket accept client IP:[%s],port:[%d]",inet_ntoa(from.sin_addr),ntohs(from.sin_port));44 45 send(clientSock,temp,strlen(temp),0);46 47 closesocket(clientSock);48 }49 closesocket(serverSock);50 51 WSACleanup(); /* 确保进程不再使用WS2_32.DLL */52 return 0;53 }