Guest User

Untitled

a guest
Oct 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. // Standard C++ headers
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. // Socket headers
  8. #include <sys/socket.h>
  9. #include <netinet/ip.h>
  10. #include <arpa/inet.h>
  11.  
  12. // UNIX system headers
  13. #include <errno.h>
  14.  
  15. using namespace std;
  16.  
  17. const int SERVER_PORT=0;
  18. const int MAX_PENDING=5;
  19. const int MAX_LINE=256;
  20.  
  21. void handle_error(int eno, char const *msg)
  22. {
  23. if(eno == 0)
  24. cerr << msg << endl;
  25. else
  26. cerr << msg << ": "<< strerror(eno) << endl;
  27. exit(errno);
  28. }
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32. int nread;
  33. socklen_t len;
  34. sockaddr_in sin;
  35. sockaddr *sin_p = (sockaddr *)&sin;
  36. sin.sin_family = AF_INET; // IP version 4
  37. sin.sin_port = htons(SERVER_PORT);
  38. sin.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all IP addresses
  39. int rstat;
  40.  
  41. cout << ntohs(sin.sin_port) << endl;
  42.  
  43. // Create socket
  44. int s = socket(AF_INET, SOCK_STREAM, 0);
  45. if(s == -1)
  46. handle_error(errno, "simplex_server - socket");
  47.  
  48. // Bind socket to local address
  49. rstat = bind(s, sin_p, sizeof(sin));
  50. if(rstat == -1)
  51. handle_error(errno, "simplex_server - bind");
  52.  
  53. rstat = listen(s, MAX_PENDING);
  54. if(rstat == -1)
  55. handle_error(errno, "simplex_server - listen");
  56.  
  57. // Allocate a memory buffer for received messages
  58. char *buf = new char[MAX_LINE];
  59.  
  60. // Wait for connections
  61. while(true) {
  62. // Upon return the sin structure will contain the address of the
  63. // connecting socket. Note that a new socket is returned.
  64. len = sizeof(sin);
  65. int new_s = accept(s, sin_p, &len);
  66. if(new_s == -1)
  67. handle_error(errno, "simplex_server - accept");
  68.  
  69. // Display IP addr and Port
  70. len = sizeof(sin);
  71. rstat = getpeername(new_s, sin_p, &len);
  72. if (rstat == -1)
  73. handle_error(errno, "simplex_server - address and port");
  74. // h_addr contains the memory address of the IP address (?!?)
  75. // The IP addresses are in network byte order
  76. char *dst = new char[INET_ADDRSTRLEN];
  77. const char *rmem = inet_ntop(AF_INET, &sin.sin_addr, dst, INET_ADDRSTRLEN);
  78. if(rmem == NULL)
  79. handle_error(errno, "simplex_client - inet_ntop");
  80. cout << "Client IP Address: " << dst << endl;
  81. cout << "Client IP Port: " << ntohs(sin.sin_port) << endl;
  82.  
  83. // Receive and print messages as long as client is connected.
  84. while(true) {
  85. nread = recv(new_s, buf, MAX_LINE, 0);
  86. if(nread == -1)
  87. handle_error(0, "simplex_server - recv");
  88. if (nread == 0) break; // client has disconnected
  89. cout << buf << flush;
  90. }
  91. close(new_s);
  92. }
  93. close(s);
  94. delete [] buf;
  95.  
  96. return 0;
  97. }
Add Comment
Please, Sign In to add comment