Talk C Chestnut Bar (157th time: C Language Instance--datagram socket communication based on Af_unix 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_unix domain, the example we're talking about is the datagram socket communication based on the Af_unix domain . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we made our first dish together in the last time: flow socket communication based on Af_unix domain. Today, I'm going to make a second dish with you: datagram socket communication based on the Af_unix domain.

  • a recipe for making a second dish: the datagram socket process.
  • produce the ingredients for the second 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. More unhelpful, we demonstrate by specific code:

The server-side communication process and its code
//1. Creating a server-side socketSERVER_FD = socket (Af_unix,sock_dgram,0);//2. Setting properties for server-side sockets: Domain, type, and protocol;    memset(&server_address,0,sizeof(structSockaddr_un)); server_address.sun_family = Af_unix;strncpy(Server_address.sun_path,socket_path,sizeof(Server_address.sun_path)-1);//3. Bind the server-side socket to the server's addressAddr_len =sizeof(structSockaddr_un); res = bind (SERVER_FD, (structSOCKADDR *) &server_address,addr_len);//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. Releasing sockets, turning off communicationres = close (CLIENT_FD);
The client's communication process and its code
//1. Creating a client socket;CLIENT_FD = socket (Af_unix,sock_dgram,0);//2. Setting properties for client sockets: domain, type, and protocol;    memset(&server_address,0,sizeof(structSockaddr_un)); server_address.sun_family = Af_unix;strncpy(Server_address.sun_path,socket_path,sizeof(Server_address.sun_path)-1);//3. Send data through sockets, here we use the SendTo function;    printf("[Client] sending data (%s) to server by socket \ n", buf); Addr_len =sizeof(structSockaddr_un); res = SendTo (client_fd,buf,sizeof(BUF),0,(structSOCKADDR *) &server_address,addr_len);//4. Releasing sockets, disconnecting client from server-side communication;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.

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  & //running the server in the back  [1 ] 6733  $./client  //run client  please input less then 8  chars for  using. Hello[client ] sending data (hello) to server  Span class= "Hljs-keyword" >by  socket [server ] receiving data (hello) 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 only one 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, and the data we send during the communication process is a string " h Span style= "Font-family:mathjax_math; Font-style:italic, "id=" mathjax-span-5 "class=" Mi ">e l l o " , you can send other strings as well.

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

Talk C Chestnut Bar (157th time: C Language Instance--datagram socket communication based on Af_unix 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.