fork(1) download
  1. /* A simple server in the internet domain using TCP
  2.   The port number is passed as an argument */
  3. /*proxy.c*/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <netdb.h>
  12.  
  13. #define BUFFER_SIZE 256
  14.  
  15. void error(const char *msg)
  16. {
  17. perror(msg);
  18. exit(1);
  19. }
  20.  
  21. void sendMessage(const char* msg, const char* address, const char* port){
  22. int sockfd, portno, n;
  23. struct sockaddr_in serv_addr;
  24. struct hostent *server;
  25.  
  26. fprintf(stderr, "sendMessage()1: %s\n", msg);
  27.  
  28. portno = atoi(port);
  29. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  30. fprintf(stderr, "sendMessage()2: %s\n", msg);
  31. if(sockfd < 0) error("ERRPR opening socket");
  32.  
  33. server = gethostbyname(address);
  34. if(server == NULL){
  35. fprintf(stderr, "ERROR, no such host\n");
  36. exit(0);
  37. }
  38. fprintf(stderr, "sendMessage()3: %s\n", msg);
  39.  
  40. bzero((char *)&serv_addr, sizeof(serv_addr));
  41. serv_addr.sin_family = AF_INET;
  42. bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  43. serv_addr.sin_port = htons(portno);
  44. fprintf(stderr, "sendMessage()4: %s port:%d\n", msg, portno);
  45.  
  46. if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  47. error("ERROR connecting");
  48.  
  49. n = write(sockfd, msg, strlen(msg));
  50. if(n < 0) error("ERROR writing to socket");
  51. close(sockfd);
  52.  
  53. fprintf(stderr, "sendMessage()5: %s\n", msg);
  54.  
  55. return;
  56. }
  57.  
  58.  
  59.  
  60. void receiveMessage(const char* port, char* buffer){
  61. int sockfd, newsockfd, portno;
  62. socklen_t clilen;
  63. struct sockaddr_in serv_addr, cli_addr;
  64. int n;
  65. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  66. if (sockfd < 0)
  67. error("ERROR opening socket");
  68. bzero((char *) &serv_addr, sizeof(serv_addr));
  69. portno = atoi(port);
  70. serv_addr.sin_family = AF_INET;
  71. serv_addr.sin_addr.s_addr = INADDR_ANY;
  72. serv_addr.sin_port = htons(portno);
  73. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  74. error("ERROR on binding");
  75.  
  76. listen(sockfd, 5);
  77. clilen = sizeof(cli_addr);
  78.  
  79. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  80. if (newsockfd < 0)
  81. error("ERROR on accept");
  82. bzero(buffer,BUFFER_SIZE);
  83. n = read(newsockfd,buffer,BUFFER_SIZE - 1);
  84.  
  85. if (n < 0) error("ERROR reading from socket");
  86. printf("Here is the message: %s\n",buffer);
  87.  
  88. n = write(newsockfd, "I got your message", 18);
  89. if(n < 0) error("ERROR writing to socket");
  90. close(newsockfd);
  91. close(sockfd);
  92. }
  93.  
  94. int main(int argc, char *argv[])
  95. {
  96. char buffer[BUFFER_SIZE];
  97. if (argc < 3) {
  98. fprintf(stderr,"usage %s portToListenOn addressToForwardTo\n", argv[0]);
  99. exit(0);
  100. }
  101. while(1){
  102. receiveMessage(argv[1], buffer);
  103. sendMessage(buffer, argv[3], argv[2]);
  104. }
  105. return 0;
  106. }
  107.  
Success #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
usage ./prog portToListenOn addressToForwardTo