This article is mainly about the two programs that implement TCP connections. This article assumes that the reader has socket programming ideas. Familiar with C programming.
Server:
# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/socket. h>
# Include <netinet/in. h> // Internet address family
# Include <ARPA/inet. h>
# Include <netdb. h>
# Include <ctype. h> // toupper (converts lowercase letters to uppercase letters)
Int Port = 8000;
/* Server */
Int main (INT argc, char ** argv ){
Struct sockaddr_in sin; // The two struct sockaddr and struct sockaddr_in are used to process network communication addresses.
Struct sockaddr_in pin;
Int sock_descriptor; // set the interface description.
Int temp_sock_descriptor;
Int address_size;
Char Buf [16384]; // buffer size
Int I, Len;
/*
* Int socket (INT domain, int type, int Protocol );
* Pf_inet, af_inet: IPv4 network protocol
* Pf_inet6, af_inet6: ipv6 network protocol.
* The type parameter is used to set the communication protocol type. The possible values are as follows:
Sock_stream: Provides connection-oriented stable data transmission, namely, TCP.
OOB: You must use connect () to establish the connection status before transmitting all data.
Sock_dgram: uses non-continuous and unreliable data packet connection.
Sock_seqpacket: provides continuous and reliable data packet connection.
Sock_raw: Provides original network protocol access.
Sock_rdm: provides reliable data packet connection.
Sock_packet: directly communicates with the network driver.
*/
// Socket function, which requests a communication port from the system
Sock_descriptor = socket (af_inet, sock_stream, 0); // IPv4 TCP protocol
If (sock_descriptor =-1) // application failed
{
Perror ("call to socket ");
Exit (1 );
}
Bzero (& sin, sizeof (SIN); // initialize and set the socket
Sin. sin_family = af_inet; // protocol family, which can only be af_inet (TCP/IP protocol family) in socket programming)
Sin. sin_addr.s_addr = inaddr_any; // sin_addr storage IP address, using the in_addr Data Structure
// S_addr stores IP addresses in byte sequence.
// In_addr32-bit IPv4 address
Sin. sin_port = htons (port); // storage port number
// Link the socket (SIN) to the port (sock_descriptor)
If (BIND (sock_descriptor, (struct sockaddr *) & sin, sizeof (SIN) =-1)
{
Perror ("call to bind ");
Exit (1 );
}
/* Int Pascal far listen (socket S, int backlog );
S: used to identify the description of a bound API.
Backlog: Maximum length of the waiting queue.
* Listen () is only applicable to interfaces that support connection, such as sock_stream.
*/
If (Listen (sock_descriptor, 20) =-1) // listen on the port sock_descriptor
{
Perror ("call to listen ");
Exit (1 );
}
Printf ("accepting connections \ n ");
While (1)
{// Port used for listening sock_descriptor
Temp_sock_descriptor = accept (sock_descriptor, (struct sockaddr *) & pin, & address_size );
If (temp_sock_descriptor =-1)
{
Perror ("call to accept ");
Exit (1 );
}
/* Int Pascal far Recv (socket S, char far * Buf, int Len, int flags );
S: A description that identifies the connected interfaces.
Buf: the buffer for receiving data.
Len: Buffer length.
Flags: Specifies the call method.
*/
If (Recv (temp_sock_descriptor, Buf, 16384,0) =-1)
{
Perror ("call to Recv ");
Exit (1 );
}
Printf ("received from client: % s \ n", Buf );
Len = strlen (BUF );
For (I = 0; I <Len; I ++)
{
Buf [I] = toupper (BUF [I]); // converts lowercase letters to uppercase letters
}
/* Int Pascal far send (socket S, const char far * Buf, int Len, int flags );
S: A description used to identify connected interfaces.
Buf: buffer that contains the data to be sent.
Len: the length of data in the buffer.
Flags: Call execution method. */
/* Send () Send TCP based on links
* Sendto () is based on a non-link UDP
*/
If (send (temp_sock_descriptor, Buf, Len, 0) =-1)
{
Perror ("call to send ");
Exit (1 );
}
Close (temp_sock_descriptor );
}
Return (exit_success );
}
Client:
# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/select. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <netdb. h>
/* Client */
Char * host_name = "127.0.0.1"; // you need to search for the server IP address.
Int Port = 8000;
/* Argc: an integer used to count the number of command line parameters sent to the main function when you run the program.
* Argv: String Array, used to store a pointer array pointing to your string parameters. Each element points to a parameter.
Argv [0] points to the full path name of the program running
Argv [1] points to the first string after the program name is executed in the doscommand line
Argv [2] points to the second string after the execution program name
*/
Int main (INT argc, char ** argv ){
Char Buf [8192];
Char message [256];
Int socket_descriptor;
Struct sockaddr_in pin; // address for processing network communication
/*
* Hostent records host information, including host name, alias, address type, address length, and address list
* Struct hostent {
Char * h_name; formal name of the address
Char ** h_aliases; Null Byte-pointer to the prepared name of the address
Int h_addrtype; Address type; usually af_inet.
Int h_length; the bit length of the address.
Char ** h_addr_list; zero byte-host network address pointer. Network byte sequence.
};
# Define h_addr h_addr_list [0] // The first address in h_addr_list
*/
Struct hostent * server_host_name;
Char * STR = "a default test string ";
If (argc <2) // number of parameters sent to the main function to the command line when running the program
{
Printf ("Usage: Test \" any test string \ "\ n ");
Printf ("we will send a default test string. \ n ");
}
Else
{
STR = argv [1];
}
/*
* Gethostbyname () returns the Host Name and address information corresponding to the given host name.
* Hostent structure pointer. The schema declaration is consistent with that in gethostaddr. */
If (server_host_name = gethostbyname (host_name) = 0)
{
Perror ("error resolving local host \ n ");
Exit (1 );
}
Bzero (& pin, sizeof (PIN ));
Pin. sin_family = af_inet;
// Htonl () converts the unsigned long integer of the host to the network byte sequence
Pin. sin_addr.s_addr = htonl (inaddr_any); // s_addr stores IP addresses in byte order.
// In_addr: the first address in the 32-bit IPv4 address h_addr_list
Pin. sin_addr.s_addr = (struct in_addr *) (server_host_name-> h_addr)-> s_addr; // The name must be h_addr.
Pin. sin_port = htons (port );
/* Apply for a communication port */
If (socket_descriptor = socket (af_inet, sock_stream, 0) =-1)
{
Perror ("error opening socket \ n ");
Exit (1 );
}
// Pin defines the IP port connected to the server
If (connect (socket_descriptor, (void *) & pin, sizeof (PIN) =-1)
{
Perror ("error connecting to socket \ n ");////
Exit (1 );
}
Printf ("Sending Message % s to server \ n", STR );
If (send (socket_descriptor, STR, strlen (STR), 0) =-1)
{
Perror ("error in send \ n ");
Exit (1 );
}
Printf ("... sent message... wait for response... \ n ");
If (Recv (socket_descriptor, Buf, 8192,0) =-1)
{
Perror ("error in processing ing response from server \ n ");
Exit (1 );
}
Printf ("\ n response from server: \ n % s \ n", Buf );
Close (socket_descriptor );
Return (exit_success );
}