Simple client server communication model under Linux (TCP)

Source: Internet
Author: User
Tags htons

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-&GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.