0

我正在尝试构建客户端-> 代理服务器-> 服务器应用程序。

我需要代理服务器的帮助,我如何让它现在发送数据到server它只能与客户端通信。

这是我的 codef 或proxy server,我从一个示例中对其进行了修改。我是新来的。

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, pid;
     socklen_t clilen;
     struct sockaddr_in serv_addr, cli_addr;

     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     while (1) {
         newsockfd = accept(sockfd, 
               (struct sockaddr *) &cli_addr, &clilen);
         if (newsockfd < 0) 
             error("ERROR on accept");
         pid = fork();
         if (pid < 0)
             error("ERROR on fork");
         if (pid == 0)  {
             close(sockfd);
             dostuff(newsockfd);
             exit(0);
         }
         else close(newsockfd);
     } /* end of while */
     close(sockfd);
     return 0; /* we never get here */
}

/******** DOSTUFF() *********************
 There is a separate instance of this function 
 for each connection.  It handles all communication
 once a connnection has been established.
 *****************************************/
void dostuff (int sock)
{
   int n, p;
   char buffer[256];
   char request;
   FILE *file; 
   file = fopen("process.log","a+");

   do
   {
       //here the proxy server receives data from the client
   bzero(buffer,256);
   p = read(sock,buffer,255);
   if (n < 0) error("ERROR reading from socket");

   printf("num: %s\n",buffer);

       //here the proxy servers replies to the client.
   n = write(sock,buffer,sizeof(buffer));

       //here the process should send data to the server
       //...codes i need help with...           

   if (n < 0) error("ERROR writing to socket");
   fprintf(file,"%s\n",buffer); /*writes*/ 

   }while(p != 0); //this runs the process +1 more than it should. wonder why?

   fclose(file);
}

DoStuff() .... 你也可以查看我添加的评论。另外,如何在客户端连接到服务器后立即向客户端发送消息,例如欢迎客户端消息?:)

注意:代理服务器向 HTTP 1.1 服务器发送 GET 请求。

我真的很感激。谢谢 :)

4

1 回答 1

1

要连接到服务器,您将不得不调用socket然后connect将该套接字从代理连接到您的真实服务器。请参阅此处的第 5 节:

http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html

此外,检查p而不是n4 行进入你的 do-while 循环DoStuff()

于 2012-11-25T02:31:16.827 回答