Questions about using udp to transmit data in Windows and linux!

Source: Internet
Author: User
Questions about using udp to transmit data in Windows and linux! -- Linux general technology-Linux technology and application information. For details, refer to the following section. I am running a udp client program in linux and then sending data. Could you tell me something wrong with the function? Please advise. If I try to use the udp test tool in windows, I cannot receive it, thank you!
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define PORT 2000/* listener PORT */
# Define MAXDATASIZE 100/* buffer size */

Int main (int argc, char * argv [])
{
Int fd, numbytes;
/* Store received messages */
Char buf [MAXDATASIZE];
/* Store remote server information */
Struct hostent * he;
/* Server address information */
Struct sockaddr_in server, reply;

If (argc! = 3)
{/* Usage */
Printf ("Usage: % s \ N ", argv [0]);
Exit (1 );
}

If (he = gethostbyname (argv [1]) = NULL)
{
Printf ("gethostbyname () error \ n ");
Exit (1 );
}

If (fd = socket (AF_INET, SOCK_DGRAM, 0) =-1)
{
Printf ("socket () error \ n ");
Exit (1 );
}

Bzero (& server, sizeof (server ));
Server. sin_family = AF_INET;
Server. sin_port = htons (PORT );
Server. sin_addr = * (struct in_addr *) he-> h_addr );
/* Send a message to the server */
Sendto (fd, argv [2], strlen (argv [2]), 0 ,\
(Struct sockaddr *) & server, sizeof (struct sockaddr ));
While (1)
{
Int len;
/* Receive messages from the server */
If (numbytes = recvfrom (fd, buf, MAXDATASIZE, 0 ,\
(Struct sockaddr *) & reply, & len) =-1)
{
Printf ("recvfrom () error \ n ");
Exit (1 );
}

If (len! = Sizeof (struct sockaddr) | \
Memcmp (const void *) & server, (const void *) & reply, len )! = 0)
{
Printf ("Receive message from other server. \ n ");
Buf [numbytes] = '\ 0 ';
Printf ("Server Message: % s \ n", buf );
Break;
}
}
/* Close the socket */
Close (fd );
Return 0;
}
Related Article

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.