Network is to use the physical link to connect the isolated workstations or hosts together to form a data link , so as to achieve the purpose of resource sharing and communication. Communication is the exchange and transmission of information between people through some kind of media. Network communication is through the network to connect the various isolated devices, through the exchange of information to achieve human and human, human and computer, computer and computer communication.
The most important thing in network communication is Network communication protocol . Today there are many network protocols , the most commonly used in LAN is three network protocols: Microsoft NetBEUI, Novell's ipx/spx and TCP/IP protocol. The appropriate network protocol should be selected as needed.
Network protocol is the bridge between the network communication, communication, only the same network protocol computer to communicate and communicate information. This is like the communication between people use the same language, only use the same language in order to normal, Shun network communication to communicate. From the professional point of view, network protocol is the computer to implement communication in the network must abide by the Convention, That is communication protocol . It is mainly about the rate of information transmission, the transmission code , code structure, Transmission Control steps , Error control, etc., and develop a .
The background program of C/E + + requires network communication, which can be implemented in two ways: using the system's underlying socket or using an existing encapsulated network library (heavyweight ACE, lightweight with Libevent ,Libev,libcurl, and Boost ASIO. ).
I. Socket programming at the bottom of the system:
There are 6 main steps:
(1). Socket () function
(2). Bind () function
(3). Listen (), connect () function
(4). Accept () function
(5). Read (), write () function, etc.
(6). Close () function
The following is a direct reference to the code description in the article:
Server side:
<span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include < SYS types.h= "" > #include <sys socket.h= "" > #include <netinet in.h= "" > #define MAXLINE 8192 int main (int argc , char** argv) {int listenfd, CONNFD; struct sockaddr_in servaddr; Char buff[8192]; int n; if ((LISTENFD = socket (af_inet, sock_stream, 0)) = =-1) {printf ("Create Socket Error:%s (errno:%d) \ n", Strerror (errno) , errno); Exit (0); } memset (&servaddr, 0, sizeof (SERVADDR)); servaddr.sin_family = af_inet; SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any); Servaddr.sin_port = htons (6666); if (Bind (LISTENFD, (struct sockaddr*) &servaddr, sizeof (servaddr)) = =-1) {printf ("Bind socket Error:%s (errno:%d) \ N ", Strerror (errno), errno); Exit (0); } if (Listen (LISTENFD, ten) = =-1) {printf ("Listen socket error:%s (errno:%d) \ n ", Strerror (errno), errno); Exit (0); } printf ("======waiting for client ' s request======\n"); while (1) {if (CONNFD = Accept (LISTENFD, (struct sockaddr*) null, null)) = =-1) {printf ("Accept socket Error:%s ( errno:%d) ", Strerror (errno), errno); Continue } n = recv (CONNFD, Buff, MAXLINE, 0); Buff[n] = ' + '; printf ("Recv msg from client:%s\n", buff); Close (CONNFD); } close (LISTENFD);} </span></span>
Client side:
<span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include < SYS types.h= "" > #include <sys socket.h= "" > #include <netinet in.h= "" > #define MAXLINE 8192 int main (int argc , char** argv) {int sockfd, n; Char recvline[8192], sendline[8192]; struct sockaddr_in servaddr; if (argc! = 2) {printf ("Usage:./client <ipaddress>\n"); Exit (0); } if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {printf ("Create Socket Error:%s (errno:%d) \ n", Strerror (E Rrno), errno); Exit (0); } memset (&servaddr, 0, sizeof (SERVADDR)); servaddr.sin_family = af_inet; Servaddr.sin_port = htons (6666); if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <= 0) {printf ("Inet_pton error for%s\n", argv[1]); Exit (0); if (Connect (sockfd, struct sockaddr*) &servaddr, sizeof (ServaddR)) < 0) {printf ("Connect Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit (0); } printf ("Send msg to server: \ n"); Fgets (Sendline, 8192, stdin); if (Send (SOCKFD, Sendline, strlen (Sendline), 0) < 0) {printf ("Send msg Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit (0); } close (SOCKFD); Exit (0);} </span></span>
Two. Common open source Socket library:
1. ACE
ACE is a large-scale middleware product, the code is about 200,000 lines, too ambitious, a bunch of design patterns, the structure of a layer after layer, when using, depending on the situation, see you from that layer to use. Support cross-platform. (Detailed information can be viewed : http://www.cs.wustl.edu/~schmidt/ACE.html)
2. Boost ASIO
The boost ASIO is an asynchronous IO Library that encapsulates Common operations on sockets and simplifies The development of socket-based programs. It is open source, free, and supports cross-platform. (Detailed information can be viewed:http://think-async.com/)
3. Libevent
Libevent is a C-language network library, the official main support is the class Linux operating system, the latest version added to the Windows IOCP support. Since IOCP is asynchronous IO, with the poll model under Linux, the Epoll model, and the kqueue of FreeBSD, these synchronization models are completely inconsistent in usage, so the use of the method is not the same as the reactor and proactor patterns in Ace, Use needs to change the way of thinking. If there is no specific requirement for performance, then using the Select model in libevent for cross-platform operations, the Select model can span systems such as Windows, Linux, Unix,solaris, and so on. (Detailed information can be viewed : http://libevent.org/)
4. Libev
libev is written in C, c written in language, only supports linux system library, previously only encapsulated epoll model libevent linux (Detailed information can be viewed: http://software.schmorp.de/pkg/libev.html
5. Linux Socket Programming in C + +
Detailed information can be viewed:http://tldp.org/LDP/LG/issue74/tougher.html)
6. C + + Sockets Library
It is a cross-platform sockets library that implements TCP, UDP, ICMP, and SCTP protocols. Implemented application protocols include SMTP, HTTP (S), AJP. With SOCKS client implementation and anonymous DNS, support for HTTP get/post/put and webserver framework. (See more resources for: http://www.alhem.net/Sockets/index.html)
7. Simple Socket
This class library lets you write based on Socket of Customers / server programs are much easier. (Detailed information can be viewed:http://home.kpn.nl/lcbokkers/simsock.htm)
8. POCO
POCO C + + Librariesprovide a set ofC + +class libraries for developing Web-based portable applications that involve threading, thread synchronization, file system access, streaming operations, shared libraries and class loading, sockets, and network protocols including:HTTP,FTP,SMTPIn itself, it also contains aHTTPserver, providingXMLthe parsing andSQLthe access interface for the database. POCOthe modular, efficient design and implementation of the library makesPOCOideal for embedded development. In the field of embedded development,C + +suitable for both the bottom (deviceI/O, interrupt processing, etc.) and high-level object-oriented development are becoming more and more popular. (Detailed information can be viewed:http://pocoproject.org/)
9. Libcurl
free Lightweight Client network library with support for DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, Imaps,ldap, LDAPS,POP3, Pop3s, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet, TFTP. Support SSL, Httppost,httpput, FTP Upload , HTTP form upload, agent, Cookies, user name and password authentication. (Detailed information can be viewed:http://curl.haxx.se/libcurl/)
Ten. Libiop
aCCross-platform Network for language developmentIOLibrary. Functional Features:C/c++api,Bottom SupportEpoll,select,pollwaitiomodel, asynchronous event model, task pool model, cross-platform threading interface, cross-platform(linux/windows); Log service; stable, support7x24hours uninterrupted operation, automatic handling of abnormal state, high concurrency and rapid response;APIsimplicity, low learning costs. (Detailed information can be viewed: http://sourceforge.net/projects/libiop/)
A summary of C + + network programming