Linux Network Programming-User Datagram Transmission

Source: Internet
Author: User
Tags socket error htons
Linux Network Programming-User Datagram transmission-Linux general technology-Linux programming and kernel information. The following is a detailed description. 5.1 two common functions
Int recvfrom (int sockfd, void * buf, int len, unsigned int flags, struct sockaddr * from int * fromlen)
Int sendto (int sockfd, const void * msg, int len, unsigned int flags, struct sockaddr * to int tolen)
The meaning of sockfd, buf, len is the same as that of read and write, indicating the socket descriptor, the buffer and size for sending or receiving respectively. recvfrom is responsible for receiving data from sockfd. If the from is not NULL, the information source is stored in the from. If you are not interested in the information source, you can set from and fromlen to NULL. sendto is responsible for sending messages. at this time, the detailed information of the recipient is stored in.
5.2 one instance
/* Server program server. c */
# Include
# Include
# Include
# Include
# Include
# Define SERVER_PORT 8888
# Define MAX_MSG_SIZE 1024
Void udps_respon (int sockfd)
{
Struct sockaddr_in addr;
Int addrlen, n;
Char msg [MAX_MSG_SIZE];
While (1)
{/* Write to the network from the network level */
N = recvfrom (sockfd, msg, MAX_MSG_SIZE, 0,
(Struct sockaddr *) & addr, & addrlen );
Msg [n] = 0;
/* Display that the server has received the message */
Fprintf (stdout, "I have received % s", msg );
Sendto (sockfd, msg, n, 0, (struct sockaddr *) & addr, addrlen );
}
}
Int main (void)
{
Int sockfd;
Struct sockaddr_in addr;
Sockfd = socket (AF_INET, SOCK_DGRAM, 0 );
If (sockfd <0)
{
Fprintf (stderr, "Socket Error: % s \ n", strerror (errno ));
Exit (1 );
}
Bzero (& addr, sizeof (struct sockaddr_in ));
Addr. sin_family = AF_INET;
Addr. sin_addr.s_addr = htonl (INADDR_ANY );
Addr. sin_port = htons (SERVER_PORT );
If (bind (sockfd, (struct sockaddr *) & ddr, sizeof (struct sockaddr_in) <0)
{
Fprintf (stderr, "Bind Error: % s \ n", strerror (errno ));
Exit (1 );
}
Udps_respon (sockfd );
Close (sockfd );
}
/* Client Program */
# Include
# Include
# Include
# Include
# Include
# Include
# Define MAX_BUF_SIZE 1024
Void udpc_requ (int sockfd, const struct sockaddr_in * addr, int len)
{
Char buffer [MAX_BUF_SIZE];
Int n;
While (1)
{/* Read from the keyboard and write it to the server */
Fgets (buffer, MAX_BUF_SIZE, stdin );
Sendto (sockfd, buffer, strlen (buffer), 0, addr, len );
Bzero (buffer, MAX_BUF_SIZE );
/* Read from the network and write it to the screen */
N = recvfrom (sockfd, buffer, MAX_BUF_SIZE, 0, NULL, NULL );
Buffer [n] = 0;
Fputs (buffer, stdout );
}
}
Int main (int argc, char ** argv)
{
Int sockfd, port;
Struct sockaddr_in addr;
If (argc! = 3)
{
Fprintf (stderr, "Usage: % s server_ip server_port \ n", argv [0]);
Exit (1 );
}
If (port = atoi (argv [2]) <0)
{
Fprintf (stderr, "Usage: % s server_ip server_port \ n", argv [0]);
Exit (1 );
}
Sockfd = socket (AF_INET, SOCK_DGRAM, 0 );
If (sockfd <0)
{
Fprintf (stderr, "Socket Error: % s \ n", strerror (errno ));
Exit (1 );
}
/* Fill in the data on the server */
Bzero (& addr, sizeof (struct sockaddr_in ));
Addr. sin_family = AF_INET;
Addr. sin_port = htons (port );
If (inet_aton (argv [1], & addr. sin_addr) <0)

{
Fprintf (stderr, "Ip error: % s \ n", strerror (errno ));
Exit (1 );
}
Udpc_requ (sockfd, & addr, sizeof (struct sockaddr_in ));
Close (sockfd );
}
########## Compile the Makefile ##########
All: server client
Server: server. c
Gcc-o server. c
Client: client. c
Gcc-o client. c
Clean:
Rm-f server
Rm-f client
Rm-f core
If you compile and run the above example, you will find a small problem. on my machine, I run the server first and then the client. enter the information on the client and send it to the server. The server displays the received information, but the client does not reflect it. run another client and send a message to the server to obtain the response. I think the first client has been blocked. if anyone knows how to solve the problem, please let me know. Thank you. because the UDP protocol does not guarantee reliable data reception, when we send information, the system cannot ensure that the information we send is accurate to the destination. generally, we use TCP protocol when writing network programs.
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.