简体   繁体   English

C 套接字编程:HTTP 请求不连续工作

[英]C Socket Programming: HTTP request not working continously

I am a newbie to c socket programming and c itself.我是 c 套接字编程和 c 本身的新手。 I have written a small piece of code that reads raw input from another internet socket and post the data to a webserver.我编写了一小段代码,它从另一个互联网套接字读取原始输入并将数据发布到网络服务器。 the received data is always numeric.接收到的数据总是数字的。 however the problem seems that the http post request happens only once instead of running in a loop and the program terminates.但是问题似乎 http 发布请求仅发生一次,而不是循环运行并且程序终止。

following is the code example以下是代码示例

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>

//define server parameters
#define WEBIP       "172.16.100.2"






int main()
{
        //declare variables
        struct sockaddr_in my_addr,client_addr,server_addr;
        struct hostent *server_host;
        int true=1;
        int client_socket_id,server_socket_id;
        int client_id;int sin_size;
        int client_bytes_received;
        char send_data [1024],recv_data[1024],post_data[1024];       

        server_host=gethostbyname(WEBIP2);

        //create a socket to listen to client
        if ((client_socket_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("Error Creating Socket");
            exit(1);
        }
        if (setsockopt(client_socket_id,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
            perror("Setsockopt");
            exit(1);
        }
        //create socket to connect to webserver
        if ((server_socket_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("Error Creating Webserver Socket");
            exit(1);
            }

        my_addr.sin_family = AF_INET;         
        my_addr.sin_port = htons(7070);     
        my_addr.sin_addr.s_addr = INADDR_ANY; 
        //bzero(&(my_addr.sin_zero),8); 
        bzero(&(server_addr.sin_zero),8); 
        server_addr.sin_family = AF_INET;         
        server_addr.sin_port = htons(WEBPORT);     
        server_addr.sin_addr = *((struct in_addr *)server_host->h_addr);





        //bind to a socket
        if (bind(client_socket_id, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) {
            perror("Unable to bind");
            exit(1);
        }

        //listen to socket
        if (listen(client_socket_id, 5) == -1) {
            perror("Error Listening to Socket");
            exit(1);
        }

        printf("\n\r Waiting for client on port 7070");
        fflush(stdout);


        while(1)
        {  

            sin_size = sizeof(struct sockaddr_in);
            client_id = accept(client_socket_id, (struct sockaddr *)&client_addr,&sin_size);
            printf("\n I got a connection from (%s , %d)",
                   inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

            //connect to remote server
            if (connect(server_socket_id, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1) 
                {
                    perror("Error Connecting to Web Server");
                    exit(1);
                }


            while(1){

            //send some data to client
            send(client_id,"Hello, World!",13, 0); 
            //receive some data from client
            client_bytes_received=recv(client_id,recv_data,1024,0);
            recv_data[client_bytes_received] = '\0';
            //print received_data
            int c_length=strlen(recv_data)+11;
            printf("\n\rRecieved data (%d bytes %d words)= %s " , client_bytes_received,c_length,recv_data);
            //post dta to webserver
            fflush(stdout);
            bzero(&post_data,1024);
            sprintf(post_data,"POST /environment.php HTTP/1.1\r\n"
                            "Host: 172.16.100.2\r\n"
                            "User-Agent: C Example Client\r\n"
                            "Content-Type: application/x-www-form-urlencoded\r\n"
                            "Content-Length: %d\r\n\r\n"
                            "track_data=%s",c_length,recv_data);
            write(server_socket_id,post_data,strlen(post_data)+1); 
            bzero(&recv_data,1024);


            while((client_bytes_received=read(server_socket_id,recv_data,1024))>0){
            recv_data[client_bytes_received] = '\0';
                if (fputs(recv_data,stdout)==EOF)
                    perror("web server read_error");
                }
            //print received_data
            printf("\n\rRecieved data from webserver (%d)= %s " , client_bytes_received,recv_data);
            //
            bzero(&recv_data,1024);
            fflush(stdout);



         }
        }  
        close(client_id);

    close(client_socket_id);
    return 0;
} 

I have not done socket programming for years, so please bear with me.我已经好几年没有做过socket编程了,所以请多多包涵。 Do you need to connect, process, and then disconnect?您需要连接、处理然后断开连接吗? That's the first thing that came to mind reading your code.这是阅读您的代码时想到的第一件事。

I am surprised this program works.我很惊讶这个程序有效。 You have created blocking sockets, unless you are working on a non-POSIX compliant OS.您已经创建了阻塞 sockets,除非您正在使用不符合 POSIX 的操作系统。 The accept call should have never returned.接受调用应该永远不会返回。 If accept is returning it means that your server socket is not able to go into the wait mode.如果接受返回,则意味着您的服务器套接字无法 go 进入等待模式。 Hence whatever you are seeing is most likely because of an error.因此,无论您看到什么,很可能是因为错误。

SO_NONBLOCK is the socket option you can use for creating non blocking sockets. SO_NONBLOCK 是可用于创建非阻塞 sockets 的套接字选项。

Since you are using the same routine for both client and server you should use select in the socket loop.由于您对客户端和服务器使用相同的例程,因此您应该在套接字循环中使用 select 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM