Note that there are some explanatory texts on larger screens.

plurals
  1. POC Socket Programming: HTTP request not working continously
    primarykey
    data
    text
    <p>I am a newbie to c socket programming and c itself. 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.</p> <p>following is the code example</p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; #include &lt;netdb.h&gt; //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,&amp;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(&amp;(my_addr.sin_zero),8); bzero(&amp;(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-&gt;h_addr); //bind to a socket if (bind(client_socket_id, (struct sockaddr *)&amp;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 *)&amp;client_addr,&amp;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 *)&amp;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(&amp;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(&amp;recv_data,1024); while((client_bytes_received=read(server_socket_id,recv_data,1024))&gt;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(&amp;recv_data,1024); fflush(stdout); } } close(client_id); close(client_socket_id); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload