SHARE
TWEET

wsTCPServer.c

a guest Jul 2nd, 2017 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef WIN32_LEAN_AND_MEAN
  2. #define WIN32_LEAN_AND_MEAN
  3. #endif
  4.  
  5. #include <windows.h>
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #include <iphlpapi.h>
  9. #include <stdio.h>
  10.  
  11.  
  12. #pragma comment(lib, "Ws2_32.lib")
  13.  
  14. /* Prototype */
  15. void ShowError(char * msg)  {
  16.   printf("%s\n", msg);
  17.   WSACleanup();
  18.   exit(1);
  19. }
  20.  
  21.  
  22. int main(int argc, char *argv[])  {
  23.   WSADATA wsaData;
  24.  
  25.   char recvmsg[1024];
  26.   int iResult, PORT;
  27.   struct addrinfo *result = NULL, *ptr = NULL, ServerAddr;
  28.  
  29.  
  30. /* Passing argument */
  31.   if(argc == 1) {
  32.     PORT = argv[1];
  33.   }
  34.   else  {
  35.     printf("Usage: %s [PORT]", argv[0]);
  36.   }
  37.  
  38.   /* Passing data to address struct */
  39.   ZeroMemory(&ServerAddr, sizeof(ServerAddr));
  40.   ServerAddr.ai_family = AF_INET;
  41.   ServerAddr.ai_socktype = SOCK_STREAM;
  42.   ServerAddr.ai_protocol = IPPROTO_TCP;
  43.   ServerAddr.ai_flags = AI_PASSIVE;
  44.  
  45.   /* Get address information */
  46.   if((iResult getaddrinfo(NULL, PORT, &ServerAddr, &result)) != 0)  {
  47.     ShowError("getaddrinfo() failed:");
  48.   }
  49.  
  50.   /* Initialize Winsock */
  51.   if((iResult = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)  {
  52.     ShowError("WSAStartup failed!");
  53.   }
  54.  
  55.   /* Create SOCKET */
  56.   SOCKET ListenSocket = INVALID_SOCKET;
  57.   SOCKET  ClientSock = INVALID_SOCKET;
  58.  
  59.   if((ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol)) == INVALID_SOCKET)  {
  60.     ShowError("socket() failed");
  61.   }
  62.  
  63.   /* Bind socket and port */
  64.   if((iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen)) == SOCKET_ERROR)  {
  65.     freeaddrinfo(result);
  66.     closesocket(ListenSocket);
  67.     ShowError("bind() failed");
  68.   }
  69.  
  70.   if(listen(ListenSocket, 5) == SOCKET_ERROR) {
  71.     freeaddrinfo(result);
  72.     closesocket(ListenSocket);
  73.     ShowError("Listen() failed");
  74.   }
  75.  
  76.   /* Accept a client socket */
  77.   if((ClientSock = accept(ListenSocket, NULL, NULL)) == INVALID_SOCKET) {
  78.     closesocket(ListenSocket);
  79.     ShowError("accept() failed");
  80.   }
  81.  
  82.  
  83. /* Recv and send */
  84.   do {
  85.     iResult = recv(ClientSock, recvmsg, 1024, 0);
  86.     if(iResult > 0) {
  87.       printf("Handling client: %d\n", ClientSock);
  88.  
  89.       /* Echo received packet */
  90.       if(send(ClientSock, recvmsg, iResult, 0) == SOCKET_ERROR) {
  91.         printf("send failed %d\n", WSAGetLastError());
  92.         closesocket(ClientSock);
  93.         WSACleanup();
  94.         exit(1);
  95.       }
  96.     }
  97.     else if(iResult == 0) {
  98.       printf("Conneciont closing...\n");
  99.     }
  100.     else {
  101.       printf("recv() failed");
  102.       closesocket(ClientSock);
  103.       WSACleanup();
  104.       exit(1);
  105.     }
  106.   }  while(iResult > 0);
  107.  
  108.   /* Disconnection Server */
  109.  
  110.  
  111.   return 0;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top