fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <unistd.h>
  9. #include <memory.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <sys/errno.h>
  13.  
  14.  
  15. const int MAXSTR=9024; // Maximum String length
  16. const long PORTNUM=8070; // The "Well-known" port number
  17.  
  18. void handleRequest(int); // Function to handle clients' request(s)
  19. using namespace::std;
  20. main( int argc, char *argv[])
  21. {
  22. int portNumber;
  23. //char executableName[MAXSTR];
  24. //struct sigaction action;
  25.  
  26. // Validate and read from the command line
  27. if ( (argc != 3) && (argc != 1) )
  28. {
  29. cerr << "Invalid command line.\n";
  30. exit(1);
  31. }
  32. if ( argc == 1 )
  33. {
  34. portNumber = PORTNUM;
  35. }
  36. else
  37. {
  38. if (!strcmp(argv[1],"-p"))
  39. {
  40. portNumber = atoi(argv[2]);
  41. }
  42. else
  43. {
  44. cerr << "Invalid command line.\n";
  45. exit(1);
  46. }
  47. }
  48.  
  49.  
  50. int sockfd,newsockfd,clilen,childpid;
  51. struct sockaddr_in serv_addr,cli_addr;
  52.  
  53. // Create a new TCP socket...
  54. if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
  55. {
  56. cerr << "Cant open stream socket.\n";
  57. exit(0);
  58. }
  59.  
  60. bzero((char *)&serv_addr,sizeof(serv_addr)) ;
  61.  
  62. serv_addr.sin_family = AF_INET;
  63. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  64. serv_addr.sin_port = htons(portNumber);
  65. serv_addr.sin_port = htons(portNumber);
  66.  
  67. // Bind the socket to the server's ( this process ) address.
  68. if (bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
  69. {
  70. cerr << "Cant bind local address.\n";
  71. exit(1);
  72. }
  73.  
  74. // Listen for connections in this socket
  75. listen(sockfd,5);
  76. int status;
  77. cerr << " Connected to port : " << portNumber << "\n";
  78.  
  79. char buf[MAXSTR],ack[MAXSTR];
  80.  
  81. // repeat forever..
  82. while (1)
  83. {
  84. clilen = sizeof(cli_addr);
  85. // Accept a client's request, and get the client's address info into the local variable cli_addr.
  86. newsockfd = accept(sockfd,(struct sockaddr *)&cli_addr,(socklen_t *)&clilen);
  87. // After accepting the connection, all transaction with this client would happen with the new socket descriptor - newsockfd.
  88.  
  89. if ((newsockfd < 0) && (errno != EINTR))
  90. cerr << " server : accept error.\n";
  91. else if (newsockfd > 0)
  92. {
  93. handleRequest(newsockfd);
  94. close(newsockfd);
  95. }
  96. newsockfd = NULL;
  97. }
  98.  
  99. }
  100. void handleRequest(int newsockfd)
  101. {
  102. char buf[MAXSTR],execName[MAXSTR],ack[MAXSTR];
  103. char data[MAXSTR];
  104. static int count = 1;
  105. int check_read = 0;
  106.  
  107. // Read from the socket, for the password and the requested command.
  108. check_read = read(newsockfd,buf,strlen(buf));
  109. sscanf(buf,"%s",execName);
  110. write(newsockfd,buf,strlen(buf));
  111.  
  112. cout << "====================================================" << endl;
  113. cout << "\e[1;36mcounts:\e[31m " << count++ << "\e[m" << endl;
  114. cout << "\e[1;36mCheck Read:\e[31m " << check_read << "\e[m" << endl << endl;
  115. cout << "\e[1;36mstrlen(execName):\e[31m " << strlen(execName) << "\e[m" << endl;
  116. cout << "\e[1;36mexecName:\e[31m " << execName << "\e[m" << endl;
  117. cout << "\e[1;36mstrlen(buf):\e[31m " << strlen(buf) << "\e[m" << endl;
  118. cout << "\e[1;36mbuf:\e[31m " << buf << "\e[m" << endl;
  119. cout << "====================================================" << endl;
  120.  
  121. }
Time limit exceeded #stdin #stdout 5s 2724KB
stdin
Standard input is empty
stdout
Standard output is empty