Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/socket.h> /* for socket(), bind(), and connect() */
  3. #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. #define MAXPENDING 5
  11.  
  12. void DieWithError(char *errorMessage);
  13. void HandleTCPClient(int clntSocket);
  14.  
  15. /* Maximum outstanding connection requests */
  16.  
  17. int main() {
  18. printf("fadyyyy");
  19. /* TCP client handling function */
  20. int servSock; /* Socket descriptor for server */
  21. int clntSock; /* Socket descriptor for client */
  22. struct sockaddr_in echoServAddr; /* Local address */
  23. struct sockaddr_in echoClntAddr; /* Client address */
  24. unsigned short echoServPort; /* Server port */
  25. unsigned int clntLen; /* Length of client address data structure */
  26.  
  27. echoServPort = 5000; /* First arg: local port */
  28. /* Create socket for incoming connections */
  29. if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  30. DieWithError("socket () failed");
  31. /* Construct local address structure */
  32. memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
  33. echoServAddr.sin_family = AF_INET; /* Internet address family */
  34. echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
  35. echoServAddr.sin_port = htons(echoServPort); /* Local port */
  36.  
  37. cout << echoServAddr.sin_addr.s_addr << endl;
  38.  
  39. /* Bind to the local address */
  40. if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0){
  41. DieWithError("bind () failed");
  42. }
  43. /* Mark the socket so it will listen for incoming connections */
  44. if (listen(servSock, MAXPENDING) < 0)
  45. DieWithError("listen() failed");
  46. while (1) /* Run forever */
  47. {
  48. cout << "TOMIA" << endl;
  49. /* Set the size of the in-out parameter */
  50. clntLen = sizeof(echoClntAddr);
  51. cout << "TOMIA++" << endl;
  52.  
  53. /* Wait for a client to connect */
  54. // if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
  55. // DieWithError("accept() failed");
  56.  
  57. if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
  58. DieWithError("accept() failed");
  59. cout << "TIA" << endl;
  60.  
  61. /* clntSock is connected to a client! */
  62. printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
  63. HandleTCPClient(clntSock);
  64. }
  65. /* NOT REACHED */
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement