Question: Hello World
Requirement: CaseProgramBased on the TCP protocol, after the client program starts, it sends "Hello World" to the server program. The server program displays the Client IP address, port, and sent information. The server sends the received string to the client, and the client displays the verification code.
Usage: compile in Linux
$ Gcc-O client. c
$ Gcc-O server. c
Run the server program $./server first
Run the client program $./client XXX (you want to access the server name --- non-IP address)
Program:
/* Client. C */
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
Int main (INT argc, char * argv []) {
Int sockfd, numbytes;
Char Buf [100];
Char * MSG = "Hello World ";
Struct hostent * He;
Struct sockaddr_in their_addr;
Int I = 0;
// Convert the basic Name and address. You must enter the server name as the parameter.
If (argc <2 ){
Printf ("You shoshould input IP or name of the server! \ N ");
Exit (1 );
}
He = gethostbyname (argv [1]);
// Create a TCP Interface
If (sockfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
// Initialize the struct and connect to port 2323 of the server
Their_addr.sin_family = af_inet;
Their_addr.sin_port = htons (2323 );
Their_addr.sin_addr = * (struct in_addr *) He-> h_addr );
Bzero (& (their_addr.sin_zero), 8 );
// Establish a connection with the server. If the connection fails, an error is reported.
If (connect (sockfd, (struct sockaddr *) & their_addr,
Sizeof (struct sockaddr) =-1 ){
Perror ("Connect ");
Exit (1 );
}
// Send the string MSG to the server
If (send (sockfd, MSG, strlen (MSG), 0) =-1 ){
Perror ("send ");
Exit (1 );
}
// Accept the information returned by the slave server
If (numbytes = Recv (sockfd, Buf, 100,0) =-1 ){
Perror ("Recv ");
Exit (1 );
}
Buf [numbytes] = '\ 0 ';
Printf ("Result: % s", Buf );
Close (sockfd );
Return 0;
}
/* Server. C */
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <sys/Wait. H>
Void showclientinf (struct sockaddr_in client_addr ){
Printf ("\ nthe IP of client is: % s", inet_ntoa (client_addr.sin_addr ));
Printf ("\ nthe port of client is: % d \ n", ntohs (client_addr.sin_port ));
}
Int main (){
Int sockfd, new_fd;
Struct sockaddr_in my_addr;
Struct sockaddr_in their_addr;
Int sin_size;
Char buff [100];
Int numbytes;
// Establish a TCP Interface
If (sockfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
// Initialize the struct and bind it to port 2323
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (2323 );
My_addr.sin_addr.s_addr = inaddr_any;
Bzero (& (my_addr.sin_zero), 8 );
// Bind a set of Apis
If (BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) =-1)
{
Perror ("bind ");
Exit (1 );
}
// Create a listener set Interface
If (Listen (sockfd, 10) =-1 ){
Perror ("listen ");
Exit (1 );
}
Printf ("server is run... \ n ");
// Wait for connection
While (1 ){
Sin_size = sizeof (struct sockaddr_in );
// If a connection is established, a new socket is generated. their_fd stores the sender's information.
// A socket maintains a control connection with the client, and the new socket transmits and accepts information with the client.
If (new_fd = accept (sockfd, (struct sockaddr *)
& Their_addr, & sin_size) =-1)
{
Perror ("accept ");
Exit (1 );
}
// Display client information
Showclientinf (their_addr );
// Generate a sub-process to complete the session with the client. The parent process continues to listen
If (! Fork ()){
// Read the information sent from the client
// You can only use sizeof to get the buff size. Because the buff has not been initialized, it is easy to use strlen to hit '\ 0'
If (numbytes = Recv (new_fd, buff, sizeof (buff), 0) =-1)
{
Perror ("Recv ");
Exit (1 );
}
Buff [numbytes] = '\ 0 ';
Printf ("Recieved % d bytes. \ n", numbytes );
Printf ("The message is: % s \ n", buff );
// Send the information received from the client back to the client
If (send (new_fd, buff, strlen (buff), 0) =-1)
Perror ("send ");
Close (new_fd );
Exit (0 );
}
Close (new_fd );
}
Close (sockfd );
}
Package download:
Http://files.cnblogs.com/lzcarl/tcp.rar