SHARE
TWEET

Untitled

a guest Dec 16th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////  Server.c ////////////////
  2.  
  3. #include<io.h>
  4. #include<stdio.h>
  5. #include<winsock2.h>
  6.  
  7. #define MY_PORT     8989
  8. #define MAXBUF      256
  9.  
  10. int main(int argc , char *argv[])
  11. {
  12.     WSADATA wsa;
  13.     SOCKET sockfd , clientfd;
  14.     struct sockaddr_in self;
  15.     char buffer[MAXBUF];
  16.  
  17.     printf("\nInitialising Winsock...");
  18.     if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  19.     {
  20.         printf("Failed. Error Code : %d",WSAGetLastError());
  21.         return 1;
  22.     }
  23.      
  24.     printf("Initialised.\n");
  25.  
  26.     /*---create streaming socket---*/
  27.     if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  28.     {
  29.         perror("Socket");
  30.         exit(errno);
  31.     }
  32.  
  33.         printf("Socket created.\n");
  34.  
  35.     /*---initialize address/port structure---*/
  36.     /* bzero(&self, sizeof(self));*/
  37.     self.sin_family = AF_INET;
  38.     self.sin_port = htons(MY_PORT);   // Host to Network Short (16-bit)
  39.     self.sin_addr.s_addr = INADDR_ANY;  
  40.  
  41.     /*---assign a port number to the socket---*/
  42.     if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
  43.     {
  44.         perror("socket--bind");
  45.         exit(errno);
  46.     }
  47.  
  48.         puts("Bind done");
  49.  
  50.     /*---make it a "listening socket"---*/
  51.     if ( listen(sockfd, 20) != 0 )
  52.     {
  53.         perror("socket--listen");
  54.         exit(errno);
  55.     }
  56.        
  57.         puts("Waiting for incoming connections...");
  58.  
  59.     /*---forever... ---*/
  60.     while (1)
  61.     {   struct sockaddr_in client_addr;
  62.         int addrlen=sizeof(client_addr);
  63.  
  64.         /*---accept a connection (creating a data pipe)---*/
  65.         clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  66.        
  67.         /*---To show the client's IP address and port number---*/
  68.         printf("The IP address is : %s\n",inet_ntoa(client_addr.sin_addr));
  69.         printf("The port number is : %u\n",htons(self.sin_port));
  70.        
  71.         /*---Loop for client's multiple requests*/
  72.         while(1)
  73.         {  
  74.         /*---memset is used to fill a block of memory or lse there will be garbage value---*/
  75.         memset (buffer, 0, MAXBUF);
  76.        
  77.         /*---Receive data on socket---*/
  78.         recv(clientfd, buffer, MAXBUF, 0);
  79.        
  80.         /*---Change string to reverse---*/
  81.         strrev(buffer);
  82.        
  83.         /*---Change string to uppercase---*/
  84.         strupr(buffer);
  85.        
  86.         /*---Send string to server---*/
  87.         send (clientfd, buffer, sizeof(buffer), 0);
  88.         //send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);
  89.        
  90.         /*---Find the legth of string---*/
  91.         strlen(buffer);
  92.         printf("number of bytes sent : %ld \n",strlen(buffer));
  93.        
  94.             while(1)
  95.             {
  96.                 if (strlen(buffer) == 0)
  97.                 printf("client terminated\n");
  98.                 break;
  99.             }
  100.                
  101.         }
  102.            
  103.         /*---close connection---*/
  104.         close(clientfd);
  105.        
  106.     }
  107.  
  108.     /*---clean up (should never get here!)---*/
  109.     close(sockfd);
  110.     WSACleanup();
  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