C language simulating web post data submission

Source: Internet
Author: User

# Include <pthread. h> <br/> # include <string. h> </P> <p> # include <sys/socket. h> <br/> # include <stdio. h> <br/> # include <errno. h> <br/> # include <stdlib. h> <br/> # include <unistd. h> <br/> # include <sys/types. h> <br/> # include <sys/socket. h> <br/> # include <netinet/in. h> <br/> # include <netdb. h> </P> <p> # define server "www.su.zju.edu.cn" <br/> # Define Port 80 <br/> # define thread_num 5 </P> <p> pthread_mutex_t sock_mu Tex; <br/> pthread_cond_t sock_cond; </P> <p> char * gen_request () <br/>{< br/> char * text = (char *) malloc (sizeof (char) * 2048); </P> <p> memset (text, 0, sizeof (char) * 2048 ); </P> <p>/* Text packet get from Wireshark */<br/> strcat (text, "post/hack/vote. PHP? Type = 2 HTTP/1.1/R/N "); <br/> strcat (text," Host: [url] www.su.zju.edu.cn [/url]/R/N "); <br/> strcat (text, "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; RV: 1.8.1.3) gecko/20061201 Firefox/2.0.0.3 (Ubuntu-Feisty)/R/N "); <br/> strcat (text," accept: text/XML, application/XML, application/XHTML + XML, text/html; q = 0.9, text/plain; q = 0.8, image/PNG, */*; q = 0.5/R/N "); <br/> strcat (text, "Accept-language: ZH-CN, Zh; q = 0.5/R/N "); <br/> strcat (text," Accept-encoding: gzip, deflate/R/N "); <br/> strcat (text, "Accept-charset: gb2312, UTF-8; q = 0.7, *; q = 0.7/R/N "); <br/> strcat (text, "keep-alive: 300/R/N"); <br/> strcat (text, "connection: keep-alive/R/N "); <br/> strcat (text," Referer: [url] http://www.su.zju.edu.cn/#/url#/r/n "); <br/> strcat (text," Cookie: usrtime = 1178023651; lasturl = http % 3A % 2f % 2fwww.su.zju.edu.cn % 2fha CK % 2fvisit. PHP % 3 FTYPE % 3d1; loginurl = http % 3A % 2f % 2fwww.su.zju.edu.cn % 2 fhack % 2fvote. PHP % 3 FTYPE % 3d2; usrsid = random; usripfrom = unknow; usrtime = 1178023650; lasturl = http % 3A % 2f % 2fwww.su.zju.edu.cn % 2f/R/N "); <br/> strcat (text, "Content-Type: Application/X-WWW-form-urlencoded/R/N"); <br/> strcat (text, "Content-Length: 10/R/N"); <br/> strcat (text, "/R/N"); <br/> strcat (text, "voteid = 276 "); </P> <p> return text; <br/>}</P> <p> void * sock_thread (void * Data) <br/>{< br/> while (1) <br/>{< br/>/* wait to start vote */<br/> pthread_mutex_lock (& sock_mutex ); <br/> pthread_cond_wait (& sock_cond, & sock_mutex); <br/> pthread_mutex_unlock (& sock_mutex); </P> <p> struct hostent * hostinfo; <br/> struct sockaddr_in name; </P> <p> name. sin_family = af_inet; <br/> name. sin_port = htons (port); <br/> Hostinfo = gethostbyname (server); </P> <p> If (hostinfo = NULL) <br/> continue; <br/> name. sin_addr = * (struct in_addr *) hostinfo-> h_addr; <br/> int sock = socket (pf_inet, sock_stream, 0); <br/> If (sock <0) <br/> continue; </P> <p>/* connect to the server */<br/> If (connect (sock, (struct sockaddr *) & name, sizeof (name) <0) <br/> continue; <br/> char * text = gen_request (); </P> <p>/* Send it !!! */<Br/> write (sock, text, strlen (text) + 1); <br/> memset (text, 0, sizeof (char) * 2048 ); <br/> free (text); </P> <p>/* well... better receive it */<br/> text = (char *) malloc (sizeof (char) * 1024); <br/> while (1) <br/>{< br/> int nbyte = Recv (sock, text, 1024, 0); <br/> If (nbyte <1024) <br/> break; <br/>}< br/> free (text); </P> <p>/* close the connection */<br/> close (sock ); <br/>}</P> <p> int main (INT argc, char * argv []) <br/> {<br/>/* initialize */<br/> pthread_t * arr_pid = (pthread_t *) malloc (sizeof (pthread_t) * thread_num ); <br/> pthread_mutex_init (& sock_mutex, null); <br/> pthread_cond_init (& sock_cond, null ); </P> <p>/* create all the thread */<br/> int I; <br/> for (I = 0; I <thread_num; I ++) <br/> pthread_create (& arr_pid [I], null, sock_thread, null); <br/> while (1) <br/>{< br/>/* sleep for a while to let one of the thread to go */<br/> usleep (200 ); <br/> pthread_cond_signal (& sock_cond ); <br/>}</P> <p>/* clean up all the threads */<br/> for (I = 0; I <thread_num; I ++) <br/> pthread_join (arr_pid [I], null); </P> <p> return 0; <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.