Original: Linux simple client server communication model (TCP)
Server side: SERVER.C
#include <stdio.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<sys/socket.h>#include<sys/types.h>#include<stdio.h>#include<unistd.h>#include<netdb.h>#include<netinet/inch.h>#defineServerPort 6080#defineMaxuser 8typedefstruct_server{intsock_fd; intnew_fd; structsockaddr_in my_addr;} Server;typedefstruct_client{intsock_fd; structsockaddr_in my_addr;} Client;StaticServer *server;StaticClient *client;voidSocket_server_init (Server *server) {Server-my_addr.sin_family =af_inet; Server-My_addr.sin_port =htons (ServerPort); Server-MY_ADDR.SIN_ADDR.S_ADDR =Inaddr_any; Bzero (& (Server-My_addr.sin_zero),8);}intSocket_server_create_bind_listen (Server *server) { intSin_size,ret, on; if(Server-SOCK_FD = socket (Af_inet,sock_stream,0)) == -1) {perror ("faild to create socket: \ n"); return-1; } on=1; RET= setsockopt (Server-SOCK_FD, Sol_socket, SO_REUSEADDR, &on,sizeof(on)); if(Ret=bind (Server-sock_fd, (structSOCKADDR *) &serverMy_addr,sizeof(structSOCKADDR)) ==-1) {perror ("failed to bind \ n"); return-1; } if(Listen (server-Sock_fd,maxuser) ==-1) {perror ("failed to listen \ n"); return-1; } return 1;}voidServer_send_msg (intFD) { Charbuff[1024x768]; while(1) {fgets (buff,1024x768, stdin); if(Send (Fd,buff,1024x768,0)==-1) {perror ("error to send:"); Break; } }}voidServer_wait_service (Server *server,client *client) { intnew_fd, Ret,sin_size; Charbuf[1024x768]; pthread_t Tid; Sin_size=sizeof(Client-my_addr); NEW_FD= Accept (Server->SOCK_FD, (structSOCKADDR *) &client->my_addr,&sin_size); if(NEW_FD <=0) {perror ("Accept Error:"); }Else{ret= Pthread_create (&tid,null,server_send_msg,new_fd); if(ret!=0) {perror ("error to create Pthread"); } while(1){ if(Recv (New_fd,buf,sizeof(BUF),0) >0) {printf ("\nclient said:\t\t%s\n", BUF); } } }}intMainintArgChar*argv[]) {Server= (Server *) malloc (sizeof(Server)); Client= (Client *) malloc (sizeof(Client)); Socket_server_init (server); if(Socket_server_create_bind_listen (server)) {server_wait_service (server,client); } return 0;}
Client code: CLIENT.C
#include <stdio.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<sys/socket.h>#include<sys/types.h>#include<stdio.h>#include<netdb.h>#defineServerPort 6080typedefstruct_server{intsock_fd; intnew_fd; structsockaddr_in my_addr;} Server;typedefstruct_client{intsock_fd; structsockaddr_in my_addr;} Client; Server*server; Client*client;voidSocket_server_init (Server *server) {Server-my_addr.sin_family =af_inet; Server-My_addr.sin_port =htons (ServerPort); Server-MY_ADDR.SIN_ADDR.S_ADDR =Inaddr_any; Bzero (& (Server-My_addr.sin_zero),8);}voidSocket_client_init (Client *client) {Client-my_addr.sin_family =af_inet; Client-My_addr.sin_port = htons (serverport+Ten); Client-MY_ADDR.SIN_ADDR.S_ADDR =Inaddr_any; Bzero (& (Client-My_addr.sin_zero),8);}intSocket_client_create_connect (Server * server,client *client) { if(Client-SOCK_FD = socket (Af_inet,sock_stream,0)) == -1) {perror ("failed to create client socket \ n"); return-1; } Else if(Connect (client-sock_fd, (structSOCKADDR *) &serverMy_addr,sizeof(server->my_addr)) ==-1) {printf ("failed to connect the server \ n"); return-1; }Else return 1;}voidClient_recv_msg (Client *client) { Charbuf[1024x768]; while(1){ if(Recv (Client->sock_fd,buf,sizeof(BUF),0) >0) {printf ("\nserver said: \t\t%s\n", BUF); }Else Break; }}intMainintargcChar*argv[]) {Server= (Server *) malloc (sizeof(Server)); Client= (Client *) malloc (sizeof(Client)); Chardata[1024x768]; Socket_server_init (server); Socket_client_init (client); pthread_t Tid; if(Socket_client_create_connect (server,client)) {pthread_create (&tid, null,client_recv_msg, client); while(1) {fgets (data,1024x768, stdin); if(Send (Client->sock_fd,data,1024x768,0)==-1) {perror ("error to send:"); Break; } } } return 0;}
Makefile:
All : server clientserver: server.c -o server Server.c-pthreadclient: client.c -O client client.c-Pthreadclean :-F Server Client
Execution effect:
Server
[Email protected]:/mnt/hgfs/e/lessons/Personal summary/chat#./server
Client Said:hello
Hehe
Client Said:g
Client Said:goodbay
Client
[Email protected]:/mnt/hgfs/e/lessons/Personal summary/chat#./client
Hello
Server Said:hehe
S^hg
Goodbay
Last year's code, make a backup.
Simple client server communication model under Linux (TCP)