Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket programming in C using http post
    text
    copied!<p>Want to do client-server programming using c in windows7, it should send string to server using http POST method. The paramater in POST method should include the ip-address etc:</p> <p>I got this code from <strong><a href="http://souptonuts.sourceforge.net/code/http_post.c.html" rel="nofollow">http://souptonuts.sourceforge.net/code/http_post.c.html</a></strong> and changed it for running it on windows, but still 1 error is coming:</p> <pre><code>#ifdef WIN32 #include &lt;winsock2.h&gt; #include &lt;ws2tcpip.h&gt; #include &lt;windows.h&gt; #else #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;unistd.h&gt; #include &lt;sys/wait.h&gt; #include &lt;netdb.h&gt; #include &lt;assert.h&gt; #endif #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; #include &lt;signal.h&gt; #include &lt;ctype.h&gt; #define SA struct sockaddr #define MAXLINE 4096 #define MAXSUB 200 #define LISTENQ 1024 extern int h_errno; ssize_t process_http(int sockfd, char *host, char *page, char *poststr) { char sendline[MAXLINE + 1], recvline[MAXLINE + 1]; ssize_t n; snprintf(sendline, MAXSUB, "POST %s HTTP/1.0\r\n" "Host: %s\r\n" "Content-type: application/x-www-form-urlencoded\r\n" "Content-length: %d\r\n\r\n" "%s", page, host, strlen(poststr), poststr); write(sockfd, sendline, strlen(sendline)); while ((n = read(sockfd, recvline, MAXLINE)) &gt; 0) { recvline[n] = '\0'; printf("%s", recvline); } return n; } int main(void) { int sockfd; struct sockaddr_in servaddr; char **pptr; //********** You can change. Puy any values here ******* char *hname = "souptonuts.sourceforge.net"; char *page = "/chirico/test.php"; char *poststr = "mode=login&amp;user=test&amp;password=test\r\n"; //******************************************************* char str[50]; struct hostent *hptr; if ((hptr = gethostbyname(hname)) == NULL) { fprintf(stderr, " gethostbyname error for host: %s: %s", hname, hstrerror(h_errno)); exit(1); } printf("hostname: %s\n", hptr-&gt;h_name); if (hptr-&gt;h_addrtype == AF_INET &amp;&amp; (pptr = hptr-&gt;h_addr_list) != NULL) { printf("address: %s\n", inet_ntop(hptr-&gt;h_addrtype, *pptr, str, sizeof(str))); } else { fprintf(stderr, "Error call inet_ntop \n"); } sockfd = socket(AF_INET, SOCK_STREAM, 0); // bzero(&amp;servaddr, sizeof(servaddr)); memset(&amp;servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(80); inet_pton(AF_INET, str, &amp;servaddr.sin_addr); connect(sockfd, (SA *) &amp; servaddr, sizeof(servaddr)); process_http(sockfd, hname, page, poststr); close(sockfd); exit(0); } </code></pre> <p>The error which is coming on MinGW compiler is: </p> <pre><code>httppost.c:33:12: error: conflicting types for 'WSAGetLastError' In file included from httppost.c:5:0: c:\mingw\bin\../lib/gcc/mingw32/4.7.2/../../../../include/winsock2.h:594:32: n e: previous declaration of 'WSAGetLastError' was here </code></pre>
 

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