/////////////////////////////////////////////////////////////////////// // multiServer.cpp - demonstrate operation of socket server // // // // Jim Fawcett, CSE691 - SW Modeling & Analysis, Fall 2000 // /////////////////////////////////////////////////////////////////////// #include #include #include #include #include using namespace std; CRITICAL_SECTION cs; //----< thread processing function >--------------------------------- unsigned int __stdcall threadProc(void *pArgs) { SOCKET *pSock = (SOCKET*)pArgs; string save; const int bufSize = 1024; char buffer[bufSize]; int bytes, count=0; do { bytes = recv(*pSock,buffer,bufSize-1,0); buffer[bytes] = '\0'; save = buffer; if(bytes > 0) { EnterCriticalSection(&cs); cout << "\n block " << ++count << ": " << save.c_str(); LeaveCriticalSection(&cs); } } while(bytes != 0 && bytes != SOCKET_ERROR); if(bytes == SOCKET_ERROR) { int errType = WSAGetLastError(); EnterCriticalSection(&cs); cout << "\n receive error type: " << errType << "\n\n"; LeaveCriticalSection(&cs); return errType; } EnterCriticalSection(&cs); cout << "\n"; LeaveCriticalSection(&cs); closesocket(*pSock); return 0; } // //----< demo main >-------------------------------------------------- int main() { cout << "\n MultiSocket Server Demonstration " << "\n ==================================\n"; InitializeCriticalSection(&cs); ///////////////////////////////////////////////////////////////////// // load version 1.1 of winsock dll WORD wVersionRequested = MAKEWORD(1,1); // requesting version 1.1 WSAData wData; // startup data filled // by WSAStartup int err = WSAStartup(wVersionRequested, &wData); if(err == SOCKET_ERROR) { int errType = WSAGetLastError(); cout << "\n initialization error type: " << errType << "\n\n"; return errType; } /////////////////////////////////////////////////////////////////// // create TCP/IP socket object to listen for connection requests SOCKET listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /////////////////////////////////////////////////////////////////// // bind listner socket to specific port and network interface int port = 2048; SOCKADDR_IN tcpAddr; tcpAddr.sin_family = AF_INET; // TCP/IP tcpAddr.sin_port = htons(port); // listening port tcpAddr.sin_addr.s_addr = INADDR_ANY; // listen over every network interface err = bind(listener, (SOCKADDR*)&tcpAddr, sizeof(tcpAddr)); if(err == SOCKET_ERROR) { int errType = WSAGetLastError(); cout << "\n initialization error type: " << errType << "\n\n"; return errType; } // /////////////////////////////////////////////////////////////////// // listen for incoming connection requests int backLog = 10; err = listen(listener, backLog); if(err == SOCKET_ERROR) { int errType = WSAGetLastError(); cout << "\n initialization error type: " << errType << "\n\n"; return errType; } cout << "\n waiting for incoming request"; /////////////////////////////////////////////////////////////////// // block until a connection is established int size = sizeof(tcpAddr); while(1) { SOCKET toClient = accept(listener, (SOCKADDR*)&tcpAddr, &size); if(toClient == INVALID_SOCKET) break; EnterCriticalSection(&cs); cout << "\n established connection with: " << inet_ntoa(tcpAddr.sin_addr) << ":" << ntohs(tcpAddr.sin_port) << endl; LeaveCriticalSection(&cs); ///////////////////////////////////////////////////////////////// // got connection so spawn thread to read transferred data unsigned int threadID; HANDLE hThread = (HANDLE)_beginthreadex( NULL, 0, threadProc, (void*)toClient, 0, &threadID ); CloseHandle(hThread); } /////////////////////////////////////////////////////////////////// // close down gracefully closesocket(listener); WSACleanup(); DeleteCriticalSection(&cs); return 0; }