First, curriculum design objectives and tasks
Objective: To become familiar with socket, the basic method of network programming linux, master writing method to achieve client / server program;
Task: write a simple program that can be implemented based on a simple protocol TCP client / server approach.
Second, the main idea of the theory and algorithms involved
TCP protocol, socket, network programming
Third, the specific design and development of ideas
Write two programs, a client, a server, establish a socket connection of the two programs, to transmit data to each other.
Fourth, the code
Client:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define port 8888 int main() { //一、创建套接字socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror("socket failed"); Exit ( - . 1 ); } // Second, prepare a communication address struct the sockaddr_in addr; addr.sin_family = AF_INET; // specified protocol family, and socket () parameters consistent addr.sin_port = the htons (Port); // Set network port number used to communicate the inet_aton ( " 192.168.1.129 " , & addr.sin_addr); // store communication network ip address // Third, the connection server int CON = connect (sockfd, ( struct the sockaddr *) & addr, the sizeof (addr )); IF (CON == - . 1 ) { perror ( " Connect failed " ); Exit ( - . 1 ); } the printf ( " connection success \ n-! " ); // four, exchange data with the server char buf [ 100 ] = { 0 }; char * STR = " Hello server. " ; Send (sockfd, STR, strlen (STR), 0 ); the recv (sockfd, buf, the sizeof (buf), 0 ); the printf ( " receive information from the server:% S \ n- " , buf) ; // five, close the connection Close (sockfd); return 0 ; }
Server:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define port 8888 int main() { //一、创建套接字 int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror("socket failed"); Exit ( - . 1 ); } // Second, prepare a communication address struct the sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = the htons (Port); the inet_aton ( " 192.168.1.129 " , & addr.sin_addr); // three , socket bindings and communication addresses int bin = the bind (sockfd, ( struct the sockaddr *) & addr, the sizeof (addr)); IF (bin == - . 1 ) { perror ( " the bind failed " ); Exit ( - . 1 ); } // four listening port bin = the listen (sockfd, 100 ); IF (bin == - . 1 ) { perror ( " the listen failed " ); Exit ( - . 1 ); } the printf ( " Start listening port% d ... \ n- " , port); // five, the connection request processing struct the sockaddr_in fromaddr; the socklen_t len = the sizeof (fromaddr); intclientfd = Accept (sockfd, ( struct the sockaddr *) & fromaddr, & len); IF (clientfd == - . 1 ) { perror ( " Accept failed " ); Exit ( - . 1 ); } the printf ( " % S is connected \ n- " , inet_ntoa (fromaddr.sin_addr)); // six, client data processing char buf [ 100 ] = { 0 }; the recv (clientfd, buf, the sizeof (buf), 0 ); the printf ( "Client:% S \ n- " , buf); char * STR = " Hello Client. " ; Send (clientfd, STR, strlen (STR), 0 ); // seven, close the connection Close (clientfd); Close (sockfd ); return 0 ; }
Fifth, the program operating results show screenshots
VI Technology Summary
1. TCP-based network programming:
Based on the connection, during the interaction, the server and client to remain connected, not disconnected. Retransmit all erroneous data, data validation, data to ensure the accuracy, integrity and sequentiality,
The disadvantage is that resource consumption is relatively large.
2. Programming steps:
server:
① Create a socket (socket) socket ()
② prepare mailing address
③ the socket and create mailing address bindings bind ()
④ listening port listen ()
⑤ waiting for clients to connect accpet ()
⑥ communication parties receive data read () / write ()
send()/recv()
⑦ close the socket
Client:
① Create a socket (socket) socket ()
② prepare mailing address
③ connection server connect ()
④ receive data read () / write ()
send()/recv()
⑤ close the socket
3. API Detailed
(1) socket () function
int socket(domain, type, protocol)
domain:
AF_UNIX / AF_LOCAL / AF_FILE: local communication
AF_INET: network communication ipv4
AF_INET6: network communication ipv6
Note: If the AF into effect as PF
type, select the type of communication, including:
SOCK_STREAM: TCP
SOCK_DGRAM : UDP
Protocol, which should specify the communication protocol, but is now largely abandoned, because the protocols have been completed in front of the two specified parameters, to 0 to
(2) bind () function
int bind(int sockfd, struct sockaddr *addr, size)
sockfd: To bind a socket descriptor
size: the second parameter memory space occupied by the
addr: relates to three data structures struct sockaddr, sockaddr_un, sockaddr_in
sockaddr, mainly for function parameters, is not responsible for data storage
sockaddr_un, when the presence of local communication, using the address for local communication (sys / un.h)
the sockaddr_in, when the presence of network communications, network communications is responsible for storing address data
struct sockaddr_in
{
the sin_family; // parameter specifies the protocol suite, and the socket () consistent
sin_port; // port number used for the communication network
sin_addr; ip address stored network communication //
}
(3) listen () function
int listen(int sockfd, int backlog)
sockfd: socket sockfd parameters identified passive mode, so that it can accept the connection request
backlog: represents the pending request queue connected to the maximum length, i.e. up to the number of allowed pending connection request is present. If the pending connection request has reached the value of the server, the client by connect () operation of connecting the server will return -1 as error ECONNREFUSED
(4) accpet () function
int accpet(sockfd, struct sockaddr* addr, socklen_t *addrlen)
Sockfd parameter identified from the socket descriptor corresponding to a connection request queue of pending connection requests removed, while creating a new socket for the communication connection, return socket
Addrlen addr and address information for outputting a connection request originator
Returns: the newly created and a socket descriptor for client communication failure -1, error
(5) recv () function
int recv(int sockfd, buf, len, flags)
flags, usually is 0: obstruction charge data
O_NONBLOCK: not blocked, the error message if no data is received, return
return value:
> 0, the number of data bytes actually received
-1 error, error
0 the other end, the communication shutdown
(6) send () function
int send(int sockfd, buf, len, flags)
flags: usually is 0, blocking transmission
O_NONBLOCK: not blocked, the error message if no data is received, return
(7) connect () function
int connect(int sockfd, addr, addr_len)
Reference parameter bind ()