C socket UDP way to send data

Source: Internet
Author: User
Tags set socket

#defineHost_server_ip "192.168.3.35"#defineHost_port 9501#defineSleep_time 1#defineLoop_time 3#defineData_size 80intjudge_socket_exist () {intLoop_time =0;    Fd_set Send_data_flag; Static intSOCKET_FD =-1;  while(1)    {        if(SOCKET_FD <=0) {socket_fd= Socket (Af_inet, SOCK_DGRAM,0); Loop_time++; }        if(Socket_fd >0) {Fd_zero (&Send_data_flag); Fd_set (SOCKET_FD,&Send_data_flag); Fd_set (0, &Send_data_flag); //Check socket is effective            if(Select(socket_fd+1, NULL, &send_data_flag, NULL,0) >0)            {                 Break; }            Else{socket_fd= Socket (Af_inet, SOCK_DGRAM,0); Loop_time++; }        }        if(Loop_time >loop_time) {            return-1;    } sleep (Sleep_time); }    returnsocket_fd;}intGsh_post_client (Const Char* SN,Const Char*IP) {    if(NULL = = SN | | NULL = =IP) {        return-1; }    intLoop_time =0;//loop time of fail execute    intSend_data_length =0;//length of sending data    intSelect_err =-1;//Flag of Select function back value    intSOCKET_FD = Judge_socket_exist ();//set socket flag, socket not close    structTimeval select_wait = {1,0};//Select function Wait time    CharText[bufsiz]; CharSend_data_buffer[data_size];//buffer of sending data    structSockaddr_in socket_addr;//socket address of server IP and portFd_set Send_data_flag;//Select function Send data flag    if(SOCKET_FD <=0) {fprintf (stderr,"Establish socket fail"); return-1; }    //Initialize socket dataBzero (&AMP;SOCKET_ADDR,sizeof(SOCKET_ADDR)); Socket_addr.sin_family=af_inet; Socket_addr.sin_port=htons (Host_port); Inet_aton (HOST_SERVER_IP,&socket_addr.sin_addr); //Initialize request datamemset (&send_data_buffer,0,sizeof(Send_data_buffer)); strcat (Send_data_buffer,"sn=");    strcat (Send_data_buffer, SN); strcat (Send_data_buffer,"&ip=");    strcat (Send_data_buffer, IP);  while(1)//For no-blocking    {        if(Loop_time >loop_time) {fprintf (stderr,"Send data fail\n"); return-1; } Fd_zero (&Send_data_flag); Fd_set (SOCKET_FD,&Send_data_flag); Fd_set (0, &Send_data_flag); Select_err=Select(socket_fd+1, NULL, &send_data_flag, NULL, &select_wait); if(Select_err <=0) {Loop_time++; }        Else        {            if(Fd_isset (SOCKET_FD, &Send_data_flag)) {Loop_time=0; FD_CLR (SOCKET_FD,&Send_data_flag);  while(Send_data_length <strlen (Send_data_buffer)) {Send_data_length= SendTo (SOCKET_FD, Send_data_buffer, strlen (Send_data_buffer),0, (structSOCKADDR *) &socket_addr,sizeof(SOCKET_ADDR)); if(Send_data_length <=0) {fprintf (stderr,"Sending data fail\n"); return-1; }                    if(Loop_time >loop_time) {fprintf (stderr,"length of sending data shorten than data\n"); return-1; }                    if(Send_data_length <strlen (Send_data_buffer)) {Loop_time++;                    Sleep (Sleep_time); }                }
Recvfrom (Socket_fd,text,sizeof(text),0, NULL, NULL); printf ("%s", text);
Break; } Else{loop_time++; }} sleep (Sleep_time); } return 0;}

C socket UDP way to send data

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.