C Language Learning-Implementation of TCP servers and clients in Linux
The client code is as follows:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define portnumber 3333
Int main (int argc, char * argv [])
{
Int sockfd;
Char buffer [1024];
Struct sockaddr_in server_addr;
Struct hostent * host;
/* Use hostname to query host names */
If (argc! = 2)
{
Fprintf (stderr, "Usage: % s hostname \ a \ n", argv [0]);
Exit (1 );
}
If (host = gethostbyname (argv [1]) = NULL)
{
Fprintf (stderr, "Gethostname error \ n ");
Exit (1 );
}
/* The customer program starts to establish the sockfd descriptor */
If (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1) // AF_INET: Internet; SOCK_STREAM: TCP
{
Fprintf (stderr, "Socket Error: % s \ a \ n", strerror (errno ));
Exit (1 );
}
/* Fill in the information of the client program on the server */
Bzero (& server_addr, sizeof (server_addr); // initialization, set 0
Server_addr.sin_family = AF_INET; // IPV4
Server_addr.sin_port = htons (portnumber); // (convert the short data on the machine to the short data on the network) Port Number
Server_addr.sin_addr = * (struct in_addr *) host-> h_addr); // ip address
/* The client program initiates a connection request */
If (connect (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr) =-1)
{
Fprintf (stderr, "Connect Error: % s \ a \ n", strerror (errno ));
Exit (1 );
}
/* Connection successful */
Printf ("Please input char: \ n ");
/* Send data */
Fgets (buffer, 1024, stdin );
Send (sockfd, buffer, strlen (buffer), 0 );
/* End communication */
Close (sockfd );
Exit (0 );
}
The server code is as follows:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define portnumber 3333 // The server port number must be the same as the client port number
Int main (int argc, char * argv [])
{
Int sockfd, new_fd;
Struct sockaddr_in server_addr;
Struct sockaddr_in client_addr;
Int sin_size;
Int nbytes;
Char buffer [1024];
/* The server starts to establish the sockfd descriptor */
If (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1) // AF_INET: IPV4; SOCK_STREAM: TCP
{
Fprintf (stderr, "Socket error: % s \ n \ a", strerror (errno ));
Exit (1 );
}
/* Fill in the sockaddr structure on the server */
Bzero (& server_addr, sizeof (struct sockaddr_in); // initialization, set 0
Server_addr.sin_family = AF_INET; // Internet
Server_addr.sin_addr.s_addr = htonl (INADDR_ANY); // (converts long data on the machine to long data on the network). Communication with any host // INADDR_ANY indicates that data from any IP address can be received, bind to all IP addresses
Server_addr.sin_port = htons (portnumber); // (convert the short data on the machine to the short data on the network) Port Number
/* Bind the sockfd descriptor to the IP address */
If (bind (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr) =-1)
{
Fprintf (stderr, "Bind error: % s \ n \ a", strerror (errno ));
Exit (1 );
}
/* Set the maximum number of clients allowed to connect */
If (listen (sockfd, 5) =-1)
{
Fprintf (stderr, "Listen error: % s \ n \ a", strerror (errno ));
Exit (1 );
}
While (1)
{
/* Server blocking until the Client Program establishes a connection */
Sin_size = sizeof (struct sockaddr_in );
If (new_fd = accept (sockfd, (struct sockaddr *) (& client_addr), & sin_size) =-1)
{
Fprintf (stderr, "Accept error: % s \ n \ a", strerror (errno ));
Exit (1 );
}
Fprintf (stderr, "Server get connection from % s \ n", inet_ntoa (client_addr.sin_addr); // converts a network address to a. String
// If (nbytes = read (new_fd, buffer, 1024) =-1) // you can write it as if (nbytes = recv (new_fd, buffer,, 0) =-1)
If (nbytes = recv (new_fd, buffer, 1024,0) =-1)
{
Fprintf (stderr, "Read Error: % s \ n", strerror (errno ));
Exit (1 );
}
Buffer [nbytes] = '\ 0 ';
Printf ("Server received % s \ n", buffer );
/* The communication has ended */
Close (new_fd );
/* Loop next */
}
/* End communication */
Close (sockfd );
Exit (0 );
}
Zookeeper