Developing our own server client programs in Linux, we pursue all programs from the minimum model, which is the simplest and easiest application framework to expand.
As a habit, I like to write my own G ++ files in SH files. Every time, I only need sh g ++. SH is OK, improving your work efficiency when everything is possible.
Linux client Model
# Include <iostream> # include <sys/socket. h> # include <netinet/in. h> # include <netdb. h> # include <ARPA/inet. h> # define maxline 100 # define SA struct sockaddr # define socket intusing namespace STD; const char * IP = {"127.0.0.1"}; const int Port = 8124; int main () {cout <"this is a client! "<Endl; cout <" conn Server IP: "<IP <Endl; cout <" conn server port: "<port <Endl; socket sockfd; if (sockfd = socket (af_inet, sock_stream, 0) <0) {// create a socket, TCP protocol, stream socket, communication protocol perror ("socket error "); return-1;} struct sockaddr_in servaddr; // communication address type variable memset (& servaddr, 0, sizeof (servaddr); servaddr. sin_family = af_inet; servaddr. sin_port = htons (port); struct hostent * HP; HP = gethostbyname (IP); memcpy (char *) & servaddr. sin_addr, (char *) HP-> h_addr, HP-> h_length); If (connect (sockfd, (Sa *) & servaddr, sizeof (servaddr) <0) // establish the connection {perror ("Connect error"); Return-1 ;}cout <"you:" <Endl; string message ("Hello nihao "); // CIN> message; If (send (sockfd, message. c_str (), message. size (), 0) <0) // send request string {perror ("send error"); Return-1 ;}int N; char recvline [maxline + 1]; while (n = Recv (sockfd, recvline, maxline, 0)> 0) // accept, blocking {recvline [N] = 0; cout <"Recv from server: "<recvline <Endl;} If (n <0) perror (" Recv error "); close (sockfd); // close socket connection return 0 ;}
Linux client model:
# Include <iostream> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # define maxline 100 # define SA struct sockaddr # define socket intusing namespace STD; int main () {cout <"this is a server! "<Endl; struct sockaddr_in server, client; socket listen_sock = socket (af_inet, sock_stream, 0); // create a listener socket If (listen_sock <0) {perror ("socket error"); Return-1;} memset (char *) & server, 0, sizeof (server); server. sin_family = af_inet; server. sin_port = htons (8124); If (BIND (listen_sock, (Sa *) & server, sizeof (server) <0) // bind the listening port {perror ("BIND error"); Return-1 ;}listen (listen_sock, 5 ); // Listen to sockfd a socket descriptor that has been bound to an unconnected backlog queue socklen_t n = (socklen_t) sizeof (client); socket conn_sock = (socket) accept (listen_sock, (Sa *) & client, & N); // create a connection socket If (conn_sock <0) perror ("Accept error "); else {// accept client request information unsigned char Buf [maxline + 1]; memset (BUF, 0, sizeof (BUF); If (n = Recv (conn_sock, Buf, maxline, 0) <0) perror ("Recv error"); else {Buf [N] = 0; cout <"Recv from client: "<Buf <Endl ;} // The server processes the business // return the server processing result string if (send (conn_sock, "server! ", 7, 0) <0) perror (" send error "); close (conn_sock);} Close (listen_sock); Return 0 ;}
The services you need are expanded based on your business. This is the server entry-level code. Yes.