Advertisement
Guest User

Untitled

a guest
Nov 30th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. // Initialize Winshock 2.2
  2. WSAStartup(MAKEWORD(2,2),&wsaData);
  3.  
  4. // Create a new socket to make a client connection.
  5. ConnectSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  6.  
  7. // Initialize Servers SOCKADDR_IN
  8. ServerAddr.sin_family = AF_INET;
  9. ServerAddr.sin_port = htons(port);
  10. ServerAddr.sin_addr.s_addr = inet_addr(server_address);
  11.  
  12. // Clear the fd sets
  13. FD_ZERO(&fdread);
  14. FD_ZERO(&BackUpfdread);
  15. FD_ZERO(&fdwrite);
  16. FD_ZERO(&BackUpfdwrite);
  17.  
  18. // Asign the ListeningSocket with fdread and fdwrite.
  19. FD_SET(ConnectSocket,&fdread);
  20. FD_SET(ConnectSocket,&fdwrite);
  21.  
  22. // Set as maxDescriptor ListeningSocket.
  23. maxDescriptor = ConnectSocket;
  24.  
  25. // Setup timer.
  26. timer.tv_sec = 2;
  27. timer.tv_usec = 5000;
  28.  
  29. // Make a connection to the server with socket s.
  30. connect(ConnectSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
  31.  
  32. // Send the username to server.
  33. send(ConnectSocket,username,strlen(username),0);
  34.  
  35. // Receive Welcome Message From Server (If any..)
  36. RecvBytes = recv(ConnectSocket,RecvBuff,sizeof(RecvBuff),0);
  37.  
  38.   if(RecvBytes > 0)
  39.  {
  40.    printf("%s",RecvBuff);  
  41.   // Cleaning the Receive Buffer
  42.   memset(RecvBuff,0,sizeof(RecvBuff));
  43.  } 
  44.  
  45. // Main loop starts here
  46. for(; ;)
  47. {
  48.   memset(SentBuff, 0, sizeof(SentBuff));
  49.   printf("Write: ");
  50.   gets_s(SentBuff, sizeof(SentBuff));
  51.  
  52.   // Copy the fdread into BackUpfdread and fdwrite to BackUpfdwrite.
  53.   BackUpfdread = fdread;
  54.   BackUpfdwrite = fdwrite;
  55.  
  56.   SelectResults = select(maxDescriptor+1,&BackUpfdread,&BackUpfdwrite,NULL,&timer);
  57.                    
  58.   if(SelectResults == -1)
  59.   {
  60.     perror("Client-select() error!\n");
  61.     exit(1);
  62.   }
  63.  
  64.  //printf("Client-select is OK\n");
  65.  
  66.  //Check if we have some data to read.
  67.  for(i=0;i<=maxDescriptor;i++)
  68.  { // #1
  69.   if(FD_ISSET(i,&BackUpfdread))
  70.   { // #2
  71.     if(i == ConnectSocket)
  72.     { // #3
  73.  
  74.     RecvBytes = recv(i, RecvBuff, sizeof(RecvBuff), 0);
  75.     if(RecvBytes > 0)               {                                                             printf("%s\n",RecvBuff);  
  76.         // Cleaning the Receive Buffer
  77.     memset(RecvBuff,0,sizeof(RecvBuff));
  78.     }  
  79.  
  80.     } // #3 end here
  81.  
  82.   } // #2 end here
  83.  
  84.   if(FD_ISSET(i,&BackUpfdwrite))
  85.   {
  86.      if(i == ConnectSocket)
  87.      {
  88.     SentBytes = send(i, SentBuff,sizeof(SentBuff),0);
  89.     // Cleaning the Sent Buffer
  90.     memset(SentBuff,0,sizeof(SentBuff));
  91.                                
  92.      }
  93.   }
  94.  
  95. } // #1 end here
  96.  
  97. } // Main loop ends here
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement