Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool CEsEFSproxy :: OnCompileCommand ( const char * sCommandLine )
- {
- if ( ! strncmp ( sCommandLine, ".efsstart", 9 )) {
- _beginthread(serverStart, 0, NULL);
- }
- return true ;
- }
- void serverStart( void * arg)
- {
- // Initialize Winsock -----------
- WSADATA wsaData;
- iResult = WSAStartup(MAKEWORD(2,2), &wsaData); // Initialise WSA
- if(iResult != 0)
- {
- printf("WSAStartup failed: %d\n", iResult);
- return;
- }
- // Create Socket for server ------
- struct addrinfo *result = NULL, *ptr = NULL, hints;
- ZeroMemory(&hints, sizeof (hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
- if(iResult != 0)
- {
- printf("getaddrinfo failed %d\n", iResult);
- WSACleanup();
- return;
- }
- SOCKET ListenSocket = INVALID_SOCKET;
- ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if(ListenSocket == INVALID_SOCKET)
- {
- printf("Error at socket(): %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return;
- }
- // Bind/setup TCP listening socket ---------
- iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
- if(iResult == SOCKET_ERROR)
- {
- printf("bind failed with error: %d\n", WSAGetLastError());
- freeaddrinfo(result);
- closesocket(ListenSocket);
- WSACleanup();
- return;
- }
- // Start listening for connection
- if(listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
- {
- printf( "Listen failed with error: %ld\n", WSAGetLastError() );
- closesocket(ListenSocket);
- WSACleanup();
- return;
- }
- // Accept connection -------------
- ClientSocket = accept(ListenSocket, NULL, NULL);
- if(ClientSocket == INVALID_SOCKET)
- {
- printf("accept failed: %d\n", WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return;
- }
- // Receive message -------------
- char recvbuf[DEFAULT_BUFLEN];
- int iSendResult;
- int recvbuflen = DEFAULT_BUFLEN;
- do {
- iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- {
- on_receive(recvbuf);
- }
- else if(iResult == 0)
- printf("Connection closing...\n");
- else {
- printf("recv failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return;
- }
- } while (iResult > 0);
- iResult = shutdown(ClientSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return;
- }
- closesocket(ClientSocket);
- WSACleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement