Simple client server communication model under Linux (TCP)

Source: Internet
Author: User
Tags stdin htons

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/In.h>#define ServerPort 6080#define MAXUSER 8typedefstruct_server{IntSOCK_FD;IntNEW_FD;structSockaddr_in my_addr;} Server;typedefstruct_client{IntSOCK_FD;structSockaddr_in my_addr;} Client;Static Server *ServerStatic Client *Clientvoid Socket_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 (& My_addr.sin_zero),8);}int Socket_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, (struct sockaddr *) &serverMY_ADDR,sizeofstruct sockaddr)) ==-1) {perror ("Failed to bind \ n");Return-1; }if (Listen (server-Sock_fd,maxuser) ==-1) {perror ("Failed to listen \ n");Return-1; }Return1;}void Server_send_msg (IntFD) {Char buff[1024];While1) {fgets (buff,1024, stdin);if (Send (Fd,buff,1024,0) ==-1) {perror ("Error to send:");Break; } }}void Server_wait_service (Server *server,client *Client) {IntNEW_FD, Ret,sin_size;Char buf[1024]; pthread_t Tid; Sin_size =sizeof (Client-MY_ADDR); NEW_FD = Accept (Server-&GT;SOCK_FD, (struct sockaddr *) &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"); }While1){if (recv (New_fd,buf,sizeof (BUF),0) >0 "\nclient said:\t\t%s\n ",buf);}}} int Main (int Arg, char *  argv[]) {Server = (server *) malloc (sizeofsizeofif (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>#define ServerPort 6080typedefstruct_server{IntSOCK_FD;IntNEW_FD;structSockaddr_in my_addr;} Server;typedefstruct_client{IntSOCK_FD;structSockaddr_in my_addr;} Client; Server *Server Client *Clientvoid Socket_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 (& My_addr.sin_zero),8);}void Socket_client_init (Client *Client) {client-my_addr.sin_family =Af_inet; Client-My_addr.sin_port = htons (serverport+10); Client-my_addr.sin_addr.s_addr =Inaddr_any; Bzero (& (My_addr.sin_zero, client),8);}int Socket_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; }ElseIf (Connect client,SOCK_FD, (struct sockaddr *) &serverMY_ADDR,sizeof (server->my_addr)) ==-1) {printf ("Failed to connect the server \ n");Return-1; }ElseReturn1;}void client_recv_msg (Client *Client) {Char buf[1024];While1){if (recv (Client->sock_fd,buf,sizeof (BUF),0) >0) {printf ("\nserver said: \t\t%s\n", buf); }ElseBreak; }}int main (int argc,char*Argv[]) {Server = (server *) malloc (sizeof(Server)); Client = (client *) malloc (sizeof(Client));Char data[1024]; 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;}                


All:    server clientserver:    server.c    gcc-o Server server.c-pthreadclient:    client.c    Gcc-o Client Client.c-pthreadclean:    rm-f Server Client 

Execution effect:


[Email protected]:/mnt/hgfs/e/lessons/Personal summary/chat#./server

Client Said:hello


Client Said:g

Client Said:goodbay


[Email protected]:/mnt/hgfs/e/lessons/Personal summary/chat#./client

Server Said:hehe


Last year's code, make a backup.

Simple client server communication model under Linux (TCP)

Related Article

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: 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.