Network socket data transmission is a special type of I/O. There are two common socket types: Lost socket (sock_stream for TCP Service) and datagram (sock_dgragm for UDP Service)
The following describes some data types related to socket programming.
1, structure type struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
}
Structure Type: struct sockaddr_in {
Short int sin_family;
Unsigned short int sin_port;
Struct in_addr sin_addr;
Unsigned char sin_zero [8]; // set it to 0 with bzero to keep the same size as struct sockaddr
};
2. socket programming-related function prototype
(1) Descriptor
Int socket (INT domain, int type, int Protocol );
(2) Bind
Int BIND (INT sockfd, struct sockaddr * my_addr, int addrlen );
Note: my_addr.sin_port = 0; // The system randomly selects an unused port number;
My_addr.sin_addr.s_addr = inaddr_any; // enter the local IP address;
(3) establish a connection
Int connect (INT sockfd, struct sockaddr * serv_addr, int addrlen );
(4) Listening
Int listen (INT sockfd, int backlog); // The maximum number of requests specified by backlog. The default value is 20.
Therefore, the server usually calls functions in the sequence of socket (), BIND (), listen (), and
(5) connection port service requests
Int accept (INT sockfd, void * ADDR, int * addrlen );
(6) Data Transmission
Int send (INT sockfd, const void * MSG, int Len, int flags );
Int Recv (INT sockfd, void * Buf, int Len, unsigned int flags );
(7) data transmission using Datagram
Int sendto (INT sockfd, const void * MSG, int Len, unsigned int flags, const struct sockaddr * To, int tolen );
/* To indicates the IP address and port number of the target host. tolen is often assigned sizeof (struct sockaddr );*/
Int recvfrom (INT sockfd, void * Buf, int Len, unisgned int lags, struct sockadrr * From, int * fromlen );
(8) end Data Transmission
Int Shutdown (INT sockfd, int how );
/* How: 0. data cannot be accepted.
* 1. data cannot be sent again.
* 2. You are not allowed to send or accept data. If both allow, close () is called ()*/
(9) Domain Name Service Functions
Struct hostent * gethostbyname (const char * Name );
Where: struct hostent {
Char * h_name; // host's official Domain Name
Char ** h_aliases; // an array of host aliases ending with null
Int h_addrtype; // The type of the returned address, AF-INET in the Internet environment
Int h_length; // The length of the address in bytes.
Char ** h_addr_list; // an array ending with 0, containing all the addresses of the host
};
# Define h_addr h_addr_list [0] // The first address in H-ADDR-list
3. Attach the instance and the server prints "Hello, world!" to the connected client !"
Server code 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>
# Define myport 3490
# Define backlog 10
Int
Main ()
{
Int sockfd, new_fd;
Struct sockaddr_in my_addr;
Struct sockaddr_in their_addr;
Int sin_size;
If (sockfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("error: socket ");
Return 0;
}
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (myport );
My_addr.sin_addr.s_addr = inaddr_any;
Bzero (& (my_addr.sin_zero), 8 );
If (BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) =-1 ){
Perror ("error: bind ");
Return 1;
}
If (Listen (sockfd, backlog) =-1 ){
Perror ("error: Listen ");
Return 1;
}
While (1 ){
Sin_size = sizeof (struct sockaddr_in );
If (new_fd = accept (sockfd, (struct sockaddr *) & their_addr, & sin_size) =-1 ){
Perror ("error: accept ");
Continue;
}
Printf ("server: Got connection from % s", inet_ntoa (their_addr.sin_addr ));
If (! Fork ()){
If (send (new_fd, "Hello world! ", 14, 0) =-1)
Perror ("error: Send ");
Close (new_fd );
Return 0;
}
Close (new_fd );
Waitpid (-1, null, wnohang );
}
}
Client code 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>
# Define Port 3490
# Define maxdatasalize 100
Int
Main (INT argc, char * argv [])
{
Int sockfd, numbytes;
Char Buf [maxdatasize];
Struct hostent * He;
Struct sockaddr_in their_addr;
If (argc <2 ){
Fprintf (stderr, "Usage: client hostip/N ");
Return 1;
}
/* If (He = gethostbyname (argv [1]) = NULL ){
Perror ("error: gethostbyname ");
Return-1;
}*/
If (sockfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("error: socket ");
Return-1;
}
Their_addr.sin_family = af_inet;
Their_addr.sin_port = htons (port );
Their_addr.sin_addr.s_addr = inet_addr (argv [1]);
// Their_addr.sin_addr = * (struct in_addr *) He-> h_addr );
Bzero (& (their_addr.sin_zero), 8 );
If (connect (sockfd, (struct sockaddr *) & their_addr, sizeof (struct sockaddr) =-1 ){
Perror ("error: connect ");
Return-1;
}
If (numbytes = Recv (sockfd, Buf, maxdatasize, 0) =-1 ){
Perror ("error: Recv ");
Return-1;
}
Buf [numbytes] = '/0 ';
Printf ("recived: % s", Buf );
Close (sockfd );
Return 0;
}
Makefile content
ALL: Server. O client. o
Server. O: Server. c
Gcc-O server. O server. c
Client. O: client. c
Gcc-O client. O client. c