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

Source: Internet
Author: User
Tags unix domain socket

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

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

  • recipe for making a third dish: the flow socket process.
  • the ingredients for making a third dish : the interface of the stream 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 147th 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 the properties of the server-side sockets: Domain, type, and protocol, we used the getaddrinfo function;    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_stream;//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. Create a socket queue, prepare for communication, and start listening for communication connection requests from clientsres = Listen (SERVER_FD,2);//5. Accept the connection request from the client and obtain a socket that matches the clientCLIENT_FD = Accept (SERVER_FD,NULL,NULL);//6. Reading data through socketsres = read (client_fd,buf,buf_size); printf"[Server] receiving data (%s) from client by socket\n", buf);//7. Releasing sockets, turning off communication    if(result->ai_next) Freeaddrinfo (result);    res = close (CLIENT_FD); res = close (SERVER_FD);
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_stream;//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. Connect using the client socket and the server;res = connect (Client_fd,result->ai_addr,result->ai_addrlen);//4. sending data via sockets;    printf("[Client] sending data (%s) to server by socket \ n", buf); res = write (Client_fd,buf,sizeof(BUF));//5. 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 do some additional instructions:

    • We first get the socket address information before we create the socket, although it is not consistent with the order in the recipes, but this is reasonable, because we used to create the socket directly in the function to write the socket properties. 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.
    • We used the localhost in the communication and specified a port number for it: 1080. When specifying a port number, its value needs to be greater than 1024. Because the Linux system binds a dedicated service to a port that is less than 1024.
    • At the same time we release the socket, we have a linked list to release the socket address information.
    • Compared with the example of using UNIX domain socket communication, the main thing is to set the socket properties and address differently, the way the data is read and received in the communication process is the same.

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 the server in the back[1]4643$ ./Client              //Run the clientPlease input8Chars forUsing. hello[Client] Sending data (hello) toServer  bySocket [Server] Receiving data (hello) fromClient  bysocket$./Client               //Run the client againPlease input8Chars forUsing. stream[Client] sending data (stream) toServer  bySocket [Server] receiving data (stream) fromClient  bysocket[1]+ done.Server          //end-of-server run

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 two of communications, the number of communications can be controlled, or even written unlimited time is also possible. Each communication can be regarded as a complete communication process, the data we send during the communication is the string "Hello" and "stream", and you can send other strings as well.

You crossing, the example of flow socket communication based on af_inet Domain Let's just talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (158th time: C Language Instance--flow 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.