The previous implementation of C + + local communication, of course, this way of data transmission can only be limited to personal host, if you want to achieve two different local area network host process communication, that is, the LAN two computers between the data transmission, then can not through the pipeline or named pipe way to achieve, The local area network communication is implemented by means of a method called socket sockets. First you want to data transfer with another computer inside the same LAN, you need to know its IP address, because IP is the only identity assigned to a personal host after the network is connected, so find the address of the host that you want to connect to. Then we both agree on a similar pipe to the only access (that is, port number port), where the port number is generally divided into recognized port number and registration port number, the agreed port number of transmission when both sides need to comply with the Common Network Protocol (TCP (SOCK_STREAM), UDP (sock_ DRAM), ICMP (Sock_raw)). Recognized port number: Well-known ports, known as the number of terminals, ranging from 0 to 1023, these port numbers are generally fixed to some services. For example, 21 ports are assigned to the FTP (File Transfer Protocol) service, 25 ports are assigned to the SMTP (Simple Mail Transfer Protocol) service, 80 ports are assigned to the HTTP service, 135 ports are assigned to the RPC (Remote Procedure Call) service, and so on: The port number is from 1025 to 49151. They are loosely bound to some services. It is also said that there are many services bound to these ports, which are also used for many other purposes. Most of these ports do not explicitly define service objects, and different programs can be defined according to their actual needs, such as the remote control software and Trojan horse programs that are described later. Remember these common program ports in the Trojan horse protection and killing is very necessary. Common Trojans used by the port in the following will have a detailed list of general selection port number is to choose the registration port number, there is no clear definition of service objects, different communications can be defined according to their needs.
The implementation process is as follows: server:
#include <arpa/inet.h>//contains the various protocol families used by the socket function, send (), recv () #include <unistd.h>//the header file that invokes the Linux system function (read (), Write (), send (), select ()) #include <iostream> #include <thread> #include <list> #define PORT 7000 #
Define IP "127.0.0.1" int s;
struct sockaddr_in servaddr;
Socklen_t Len;
Std::list<int> Li; void Getconn () {while (1) {int conn = accept (s, (struct sockaddr*) &servaddr, &len);//The second parameter holds the corresponding I of the client socket
P Address and port information Li.push_back (conn);
printf ("%d\n", conn);
} void GetData () {struct Timeval TV;
Tv.tv_sec = 2;
tv.tv_usec = 0;
while (1) {Std::list<int>::iterator it;
For (It=li.begin (); It!=li.end (); ++it) {Fd_set RfDs;
Fd_zero (&rfds);
int maxfd = 0;
int retval = 0;
Fd_set (*it, &rfds);
if (Maxfd < *it) {maxfd = *it; } retval = Select (maxfd+1, &rfds, NULL, NULL,&TV)//implementation of non-blocking communication, that is, the need to wait for the occurrence of time, once the execution must return, the results returned are different to represent the result of the function execution if (retval = = 1) {printf ("Select ER
Ror\n ");
}else if (retval = = 0) {}else{char buf[1024];
memset (buf, 0, sizeof (BUF));
Long len = recv (*it, buf, sizeof (BUF), 0);
printf ("%s", buf);
} sleep (1);
} void Sendmess () {while (1) {char buf[1024];
Fgets (buf, sizeof (BUF), stdin); read a row from the file stream, send it to the buffer, and use the following points to note std::list<int>::iterator it;
For (It=li.begin (); It!=li.end (); ++it) {Send (*it, buf, sizeof (BUF), 0);
int main () {s = socket (af_inet, sock_stream, 0);
memset (&servaddr, 0, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port = htons (port);
SERVADDR.SIN_ADDR.S_ADDR = inet_addr (IP); if (Bind (s, (struct sockaddr*) &servaddr, sizeof (SERVADDR)) ==-1) {perRor ("bind");
Exit (1);
} if (listen (s) = = 1) {perror ("listen");
Exit (1);
len = sizeof (SERVADDR);
Std::thread T (getconn);
T.detach ();
Std::thread T1 (sendmess);
T1.detach ();
Std::thread T2 (GetData);
T2.detach ();
while (1) {} return 0;
}
Client
#include <arpa/inet.h> #include <unistd.h> #include <iostream> #define MYPORT 7000 #define Buffer_size
1024 int main (int argc,char *argv[]) {int sock_cli;
Fd_set RfDs;
struct Timeval tv;//set time int retval, MAXFD;
Definition SOCKFD SOCK_CLI = socket (af_inet,sock_stream, 0);
Define sockaddr_in struct sockaddr_in servaddr;
memset (&servaddr, 0, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Char s[1024];
int A;
Std::cout << "Enter the port number and IP address for which you want to establish a connection:" << Std::endl;
scanf ("%d", &a);
GetChar ();
scanf ("%s", s); Servaddr.sin_port = Htons (a); Server port, using htons to convert host byte order to network byte sequence for packet transmission SERVADDR.SIN_ADDR.S_ADDR = INET_ADDR (s); Server IP//connection server, successful return 0, error return-1 if (sock_cli, (struct sockaddr *) &servaddr, sizeof (SERVADDR)) <
0) {perror ("connect");
Exit (1);
while (1) {/* Clears the collection of readable file descriptors/Fd_zero (&RFDS); /* The text of the standard inputThe Assembly descriptor is added to the set/Fd_set (0, &rfds);
MAXFD = 0;
/* Add the current attached file descriptor to the set/Fd_set (SOCK_CLI, &rfds);
* * Find the largest file descriptor in the file descriptor collection/if (Maxfd < sock_cli) maxfd = SOCK_CLI;
/* Set Timeout time * * tv.tv_sec = 5;
tv.tv_usec = 0; /* Wait for chat/retval = select (maxfd+1, &rfds, NULL, NULL, &TV);//int Select (int maxfdp,fd_set *readfds,fd_set * Writefds,fd_set *errorfds,struct timeval*timeout);
Monitor the file descriptor changes for the file we need-read or write or exception if (retval = 1) {printf ("Select error, client program exits \ n");
Break
}else if (retval = = 0) {//Timeout printf ("The client does not have any input information, and the server does not have information to come, waiting...\n");
Continue }else{//file can read or write or error/* Server sent Message */if (Fd_isset (Sock_cli,&rfds)) {char Recvbuf[bu
Ffer_size];
Long Len;
Len = recv (sock_cli, Recvbuf, sizeof (RECVBUF), 0);
printf ("%s", recvbuf); memset (Recvbuf, 0, sizeof (RECVBUF)); /* User input information, start processing information and send/if (Fd_isset (0, &rfds)) {char sendbuf[1024];
scanf ("%s", sendbuf);
Fgets (sendbuf, sizeof (SENDBUF), stdin); Send (SOCK_CLI, SendBuf, strlen (SENDBUF), 0);
Send memset (sendbuf, 0, sizeof (SENDBUF));
}} close (SOCK_CLI);
return 0;
}