Talk C Chestnut Bar (159th time: C Language Instance--datagram socket communication based on af_inet domain)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the example of a flow socket communication based on the af_inet domain, the example we're talking about is the datagram socket communication based on the af_inet domain . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we made our third dish together in the last one: flow socket communication based on af_inet domain. Today, I will make a fourth dish with you: datagram socket communication based on the af_inet domain.

  • recipe for making a fourth dish: the datagram socket process.
  • produce the ingredients for the fourth Dish : The interface of the datagram socket, socket properties, socket address information.

Crossing, the above content, we in the previous chapter back in detail, if you forget, you can refer to the previous content, the focus is the 148th back content, because this is our recipe. The most troublesome in the ingredients is the socket address information, you can refer to the example in 155. More useless, we demonstrate it through specific code.

The server-side communication process and its code
//1. Setting properties for server-side sockets: Domain, type, and protocol;    Char* Host ="localhost";//using localhost IP address:127.0.0.1    Char* Server =" the";//using Port, it must is more then 1024x768    structAddrinfo hints;structAddrinfo *result;structSockaddr_in client_address;memset(&hints,0,sizeof(structAddrinfo));    result = NULL;    Hints.ai_flags = Ai_numericserv;    hints.ai_family = af_inet; Hints.ai_socktype = Sock_dgram;//get Address info, it has all attribute of socketsres = getaddrinfo (host,server,&hints,&result);//2. Creating a server-side socketSERVER_FD = socket (result->ai_family,result->ai_socktype,result->ai_protocol);//3. Bind the server-side socket to the server's addressres = bind (Server_fd,result->ai_addr,result->ai_addrlen);//4. Read data through sockets, where the Recvfrom function is used;Addr_len =0; res = Recvfrom (server_fd,buf,buf_size,0,(structSOCKADDR *) &client_address,&addr_len);printf("[Server] receiving data (%s) from client by socket\n", buf);//5. Release socket address information. The socket is not released here because the client has freed and closed the communication    if(result->ai_next) Freeaddrinfo (result);
The client's communication process and its code
//1. Setting properties for client sockets: domain, type, and protocol;    Char* Host ="localhost";//using localhost IP address:127.0.0.1    Char* Server =" the";//using Port, it must is more then 1024x768    structAddrinfo hints;structAddrinfo *result;memset(&hints,0,sizeof(structAddrinfo));    result = NULL;    Hints.ai_flags = Ai_numericserv;    hints.ai_family = af_inet; Hints.ai_socktype = Sock_dgram;//get Address info, it has all attribute of socketsres = getaddrinfo (host,server,&hints,&result);//2. Creating a client socket;CLIENT_FD = socket (result->ai_family,result->ai_socktype,result->ai_protocol);//3. Send data through sockets, here we use the SendTo function;    printf("[Client] sending data (%s) to server by socket \ n", buf); res = SendTo (client_fd,buf,sizeof(BUF),0, Result->ai_addr,result->ai_addrlen);//4. Releasing sockets, disconnecting client from server-side communication;    if(result->ai_next) Freeaddrinfo (result); res = close (CLIENT_FD);

Crossing, the above is the core code, the complete code put in my resources, you can click here to download the use.

For the above code, I'll do a little brief explanation. We first get the information for the socket address before we create the socket. This is the same as the previous step, the basis of the same reason is the same, so no detailed introduction. Here, we use the result of the Getaddrinfo function, which can improve the portability of the code. This method is also used in the BIND function and the SendTo function. The rest of the code is similar to the one in the previous chapter, so it is not described in detail.

We need to compile the server-side code and the client code separately and compile it into different executable files:

client  //编译客户端server  //编译服务器端

Crossing, delicious food is done, we have to taste the next, tasting method is to run the program, the following is the operation of the program results, please refer to:

$./server  & //run server  [1 ] 5689  $ ./client  //run client  Pan class= "Hljs-number" >8  chars for  using. Dgram[client ] sending data (Dgram) to server  Span class= "Hljs-keyword" >by  socket [server ] receiving data (Dgram) from Span class= "Hljs-keyword" >client  by  socket[1 ]+ done./server  //Server Run End   

Crossing, as can be seen from the results above, the client sends data to the server through a socket, and the server receives the data from the client through the socket, so that the client and the server can communicate through the socket. In addition, we have a communication, the number of communications can be controlled by themselves, or even written in unlimited times is also possible. Each communication can be considered as a complete communication process, the data we send during the communication is the string "Dgram", and you can send other strings as well.

You crossing, the example of datagram socket communication based on af_inet domain we're going to talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (159th time: C Language Instance--datagram socket communication based on af_inet domain)

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.