The client sends the request to the server, and the server receives the request to do the processing and returns the processing results back to the client. The following TCP protocol is used to implement the connection between the server and the client.
1. Client
The Protocol (UDP or TCP) is agreed between the parties, and the socket is created according to the transport Protocol;
The IP address and port number of the server;
connecting to the server;
Gets the data that the server passes back.
#include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ time.h> #include <time.h> #include <fcntl.h> #include <netinet/in.h> #include <a rpa/inet.h> #include <sys/errno.h> #include <iostream> #include <stdlib.h> #include
<stdio.h> using namespace std;
const int maxline=1024;
int main (int argc,char** argv) {int sockfd,n;
Char recvline[maxline+1];
struct sockaddr_in servaddr;
if (argc!=2) {cout<< "usage:a.out<ipaddress" <<endl;
Exit (0);
} sockfd=socket (af_inet,sock_stream,0);
if (sockfd<0) {cout<< "socket error" <<endl;
Exit (0);
} memset (&servaddr,0, sizeof (SERVADDR));
Servaddr.sin_family=af_inet; Servaddr.sin_port=htons (8080);//convert unsigned short integer value to netThe byte order, in which the high-order byte of the value is deposited into the low byte in memory 0x1234 becomes 0x3412 if (Inet_pton (AF_INET,ARGV[1],&SERVADDR.SIN_ADDR) <=0)//IP address in the
Convert dotted decimal and integer {cout<< "inet_ptons error" <<endl;
Exit (0); } if (Connect (SOCKFD, (sockaddr*) &servaddr,sizeof (servaddr)) <0) {cout<< "Connect er
Ror "<<endl;
Exit (0);
while ((N=read (sockfd,recvline,maxline)) >0) {recvline[n]=0;
if (fputs (recvline,stdout) ==eof) {cout<< "fputs error" <<endl;
Exit (0);
} if (n<0) {cout<< "read error" <<endl;
Exit (0);
} exit (0); }