Linuxsocke programming example: A simple echo server program
Source: Internet
Author: User
Linuxsocke programming example: A simple echo server program-general Linux technology-Linux programming and kernel information. The following is a detailed description. Maybe it was the first time I really used the linux operating system. I suddenly became very interested in the long-discarded C. Recently I want to learn about linux. In the linux World, C is the most exposed, so I feel like writing a linux socket program.
Maybe many of my friends will write too many such programs in Java like me, and even never forget the Socket communication details. Since I have no programming experience in linux, I encountered a lot of things I don't understand when writing a program. After google flipped through, I finally understood some basic things. Well, let's talk less about it. Let's talk about the idea of programming.
In this article, we compile the echo server sample program. When the client data is received, the server sends the data to the customer without processing. TCP connection and port 8080 are used for design. socket communication is mainly involved throughout the process.
First, create a socket. The Code is as follows:
Int socketfd;
Socketfd = socket (AF_INET, SOCK_STREAM, 0 );
The socket function is the first function we encounter when writing a socket program. It creates a socket on the specified Protocol. Its function description is as follows:
# Include
Int socket (int AddressFamily, int Type, int Protocol)
The AddressFamily parameter specifies the network address type to be interpreted in the socket operation. The value is one of the following:
AF_UNIX
Indicates the path of the operating system file.
AF_INET
Internet address
AF_NS
XEROX network address
The Type parameter table describes the communication semantics, that is, the communication connection method. The parameters are as follows:
SOCK_STREAM
Provides stable and reliable connections and provides two-way communication, such as TCP.
SOCK_DGRAM
Provides connectionless datagram communication, such as UDP.
SOCK_RAW
Only root users can use the internal network protocol and network interface.
Returned value: if the request succeeds, the socket descriptor is returned. If the request fails,-1 is returned. You can use the errno code to check the cause of the error.
Again, bind the socket to the local machine. The Code is as follows:
Struct sockaddr_in sa;
Bzero (& sa, sizeof (sa ));
Sa. sin_family = AF_INET;
Sa. sin_port = htons (EHCO_PORT );
Sa. sin_addr.s_addr = htons (INADDR_ANY );
Bzero (& (sa. sin_zero), 8 );
If (bind (socketfd, (struct sockaddr *) & sa, sizeof (sa ))! = 0)
{
Printf ("bind failed ");
Printf ("errno = % d", errno );
Exit (1 );
}
Else
{
Printf ("bind successfully ");
}
In the code above, define a scokaddr_in struct variable sa and fill in the port number and address to be activated by the machine service.
Sa. sin_family = AF_INET;
> Indicates the address type.
Sa. sin_port = htons (EHCO_PORT );
> The port number is 8080.
Sa. sin_addr.s_addr = htons (INADDR_ANY );
> Indicates binding to the Local Machine
Then, use the bind function to bind the socket you just created as a parameter.
After the binding is complete, the server needs to listen to the connection of the client. Therefore, the listener setting process must be completed by the listen function. The Code is as follows:
If (listen (socketfd, MAX_CLIENT_NUM )! = 0)
...{
Printf ("listen error ");
Exit (1 );
}
Else
...{
Printf ("listen successfully ");
}
Listen (socketfd, MAX_CLIENT_NUM) indicates listening on socketfd. the maximum number of customers is MAX_CLIENT_NUM.
After listening, you can connect the customer to the server. The service needs to obtain the customer's request through the accept function. At the same time, a sockaddr_in struct must be used to obtain the customer's information. The Code is as follows:
Here, clientfd is the customer's socket. on the server side, each time a customer connection is received, a customer's socket descriptor is returned, and the Server communicates with the customer according to it. ClientAdd is the structure of the customer address information. It is filled in the accept function to obtain the customer address information.
Wait for the first customer. When the first customer's request comes to the server, this function will return, and clientfd is the customer's socket descriptor.
Then conduct communication
While (n = recv (clientfd, buff, 100,0)> 0)
Wait for the customer's data. After receiving the data, enter the received data information in the standard and send it back to the customer: send (clientfd, buff, n, 0 );
Here, we use simple commands to control communication. quit indicates that the customer wants to end the communication process, and close indicates that the customer requests to close the server. To close the function, you only need to use the close function.
The complete code is as follows:
# Include
# Include
# Include
# Include
# Include
# Define EHCO_PORT 8080
# Define MAX_CLIENT_NUM 10
Int main ()
{
Int socketfd;
Socketfd = socket (AF_INET, SOCK_STREAM, 0 );
If (socketfd =-1)
{
Printf ("errno = % d", errno );
Exit (1 );
}
Else
{
Printf ("socket create successfully ");
}
Struct sockaddr_in sa;
Bzero (& sa, sizeof (sa ));
Sa. sin_family = AF_INET;
Sa. sin_port = htons (EHCO_PORT );
Sa. sin_addr.s_addr = htons (INADDR_ANY );
Bzero (& (sa. sin_zero), 8 );
If (bind (socketfd, (struct sockaddr *) & sa, sizeof (sa ))! = 0)
After cc compilation, you can run it. The program we write here is a server program. If you want to complete communication, you have to write a client program, right ???
Well, let's put down the client program first. Let's test our server program first. Here, we use telnet as the client for testing. telnet is a good client program. Haha:
The local IP address is 192.168.0.69. The entire communication process is as follows:
The connection is made twice. At the first time, it communicates with the server three times. After each message is sent, the same information is received. When you enter quit, the server closes the socket that is idle for communication with the customer, and the communication ends. For the second time, the customer only enters close. After the server responds, the server is immediately closed and the client is also closed. The following is the server display content:
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