1 #include <iostream> 2 using namespace std; 3 4 #include <WinSock2.h> 5 #pragma comment(lib, "ws2_32.lib") 6 7 #include "incl/pthread.h" 8 #pragma comment(lib, "pthreadVCE2.lib") 9 10 class SockIp 11 { 12 public: 13 SockIp(SOCKET sock, SOCKADDR_IN addr) 14 { 15 mSock = sock; 16 mAddr = addr; 17 } 18 19 SOCKET getSock() 20 { 21 return mSock; 22 } 23 24 SOCKADDR_IN getAddr() 25 { 26 return mAddr; 27 } 28 29 private: 30 SOCKET mSock; 31 SOCKADDR_IN mAddr; 32 }; 33 34 void* RecvThread(void* p) 35 { 36 //SOCKET skClient = *(SOCKET*)p; 37 SockIp *si = (SockIp*)p; 38 SOCKET skClient = si->getSock(); 39 SOCKADDR_IN addr = si->getAddr(); 40 cout<<"conn IP:"<<inet_ntoa(addr.sin_addr) 41 <<",PORT:"<<ntohs(addr.sin_port)<<endl; 42 43 44 45 char buf[1024]; 46 while (true) 47 { 48 int ret = recv(skClient, buf, 1024, 0); 49 if (ret == -1) 50 { 51 break; 52 } 53 buf[ret] = ‘\0‘; 54 cout<<ret<<","<<buf<<endl; 55 } 56 closesocket(skClient); 57 cout<<"disconn IP:"<<inet_ntoa(addr.sin_addr) 58 <<",PORT:"<<ntohs(addr.sin_port)<<endl; 59 60 return NULL; 61 }; 62 63 64 int main() 65 { 66 //1.init 67 WSADATA wsa; 68 if (WSAStartup(MAKEWORD(2,2), &wsa)) 69 { 70 cout<<"1"<<endl; 71 } 72 73 //2.socket 74 SOCKET skListen = socket(AF_INET, SOCK_STREAM, 0); 75 76 //3.bind 77 SOCKADDR_IN addr; 78 addr.sin_family = AF_INET; 79 addr.sin_port = htons(8888); 80 addr.sin_addr.s_addr = htonl(INADDR_ANY); 81 bind(skListen, (SOCKADDR*)&addr, sizeof(addr)); 82 83 //4.listen 84 listen(skListen, 5); 85 86 87 //a.select+thread 88 /* 89 fd_set fdset; 90 while(true) 91 { 92 FD_ZERO(&fdset); 93 FD_SET(skListen, &fdset); 94 95 int num = select(0, &fdset, 0, 0, 0); 96 if (num > 0) 97 { 98 //read 1.accept 2.recv 99 if(FD_ISSET(skListen, &fdset)) 100 { 101 //accept 102 SOCKADDR_IN accptAddr; 103 int len = sizeof(accptAddr); 104 SOCKET skClient = accept(skListen, (SOCKADDR*)&accptAddr, &len); 105 106 SockIp *si = new SockIp(skClient, accptAddr); 107 pthread_t trd; 108 pthread_create(&trd, 0, RecvThread, si); 109 } 110 } 111 } 112 */ 113 114 //b.select 115 116 fd_set fdset; 117 FD_ZERO(&fdset); 118 FD_SET(skListen, &fdset); 119 while(true) 120 { 121 122 fd_set fdRead = fdset; 123 int num = select(0, &fdRead, 0, 0, 0); 124 if (num > 0) 125 { 126 for (unsigned int i=0; i<fdset.fd_count; i++) 127 { 128 if (FD_ISSET(fdset.fd_array[i], &fdRead)) 129 { 130 SOCKADDR_IN acceptAddr; 131 int len = sizeof(acceptAddr); 132 //accept 133 if(fdset.fd_array[i] == skListen) 134 { 135 if (fdset.fd_count < FD_SETSIZE) 136 { 137 SOCKET skClient = accept(skListen, (SOCKADDR*)&acceptAddr, &len); 138 cout<<"conn IP:"<<inet_ntoa(acceptAddr.sin_addr) 139 <<",PORT:"<<ntohs(acceptAddr.sin_port)<<endl; 140 141 FD_SET(skClient, &fdset); 142 } 143 } 144 else//recv 145 { 146 char buf[1024]; 147 SOCKET skCur = fdset.fd_array[i]; 148 int ir = recv(skCur, buf, 1024, 0); 149 if (ir == -1) 150 { 151 getpeername(skCur, (SOCKADDR*)&acceptAddr, &len); 152 cout<<"diss conn IP:"<<inet_ntoa(acceptAddr.sin_addr) 153 <<",PORT:"<<ntohs(acceptAddr.sin_port)<<endl; 154 155 closesocket(skCur); 156 FD_CLR(skCur, &fdset); 157 } 158 else if (ir == 0) 159 { 160 cout<<"0:disconnect"<<endl; 161 162 closesocket(skCur); 163 FD_CLR(skCur, &fdset); 164 } 165 else 166 { 167 buf[ir]=‘\0‘; 168 cout<<buf<<endl; 169 } 170 171 } 172 } 173 } 174 } 175 } 176 177 system("pause"); 178 return 0; 179 }
时间: 2024-10-14 16:11:10