OpenSSL VPN Serveurs de messagerie |
OpenSSL/ServeurTCPserveur TCP // serveur.cpp application console // #include "stdafx.h" // insérer Ws2_32.lib dans Link>General>Library Modules #define CHK_NULL(x) if ((x)==NULL) exit (1) #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); } #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); } int main(int argc, char* argv[]) { WSADATA wsaData; SOCKET sd; struct sockaddr_in sa; hostent* remoteHost; int err; char recvbuf[32]; int bytesRecv; //----------------------------------------------- // Initialize Winsock WSAStartup(MAKEWORD(2,2), &wsaData); sd = socket(AF_INET, SOCK_STREAM, 0); CHK_ERR(sd, "socket"); memset (&sa, '\0', sizeof(sa)); sa.sin_family = AF_INET; //sa.sin_addr.s_addr = inet_addr ("172.31.198.15"); /* Server IP */ remoteHost = gethostbyname("sec030dev146"); sa.sin_addr.s_addr = *((u_long*)remoteHost->h_addr_list[0]); sa.sin_port = htons (400); /* Server Port number */ bool bOptVal = TRUE; setsockopt(sd,SOL_SOCKET,SO_REUSEADDR, (char*)&bOptVal,sizeof(bOptVal)); if(bind(sd,(struct sockaddr *)&sa, sizeof(sa))== SOCKET_ERROR){ printf(WSAGetLastErrorMessage("client")); goto cleanUp; } if(listen(sd,SOMAXCONN)== SOCKET_ERROR){ printf(WSAGetLastErrorMessage("client")); goto cleanUp; } SOCKET AcceptSocket; while(1) { AcceptSocket = SOCKET_ERROR; while( AcceptSocket == SOCKET_ERROR ) { AcceptSocket = accept( sd, NULL, NULL ); } printf("Client connected.\n"); bytesRecv = SOCKET_ERROR; while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( AcceptSocket, recvbuf, 32, 0 ); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { printf( "Connection Closed.\n"); break; } recvbuf[bytesRecv]='\0'; printf( "Bytes Recv: %ld ***%s***\n", bytesRecv,recvbuf ); } } cleanUp: WSACleanup(); return 0; } |