Multi-Process Concurrent server

Source: Internet
Author: User
Tags htons

Data.h

1 #ifndef data_h2 #defineData_h3#include <stdio.h>4#include <string.h>5#include <sys/types.h>6#include <sys/stat.h>7#include <sys/socket.h>8#include <stdlib.h>9#include <unistd.h>Ten#include <netinet/inch.h> One#include <arpa/inet.h> A#include <netdb.h> -#include <errno.h> - #definePORT 45178 the #defineMAXN 1100 - #defineBACKLOG 10 - #defineIP "127.0.0.1" - #endif

Server.c

#include"data.h"voidSend_message (intSOCKFD,structsockaddr_in Client) {    intnum; CharSENDBUF[MAXN],RECVBUF[MAXN]; CharCLINAME[MAXN]; printf ("You get a connection from%s \ n", Inet_ntoa (CLIENT.SIN_ADDR)); Num= Recv (SOCKFD,CLINAME,MAXN,0); printf ("client name is%s\n", Cliname);  while(num = recv (SOCKFD,RECVBUF,MAXN,0) ) {num--; Recvbuf[num]=' /'; printf ("Receive A message is%s from <===>%s\n", Recvbuf,cliname); Send (SOCKFD,RECVBUF,MAXN,0); }        }intMain () {intSOCKFD; structsockaddr_in server_in; if(SOCKFD = socket (Af_inet,sock_stream,0)) == -1) {fprintf (stderr,"Socket Fail,error%s", Strerror (errno)); return-1; } bzero (&server_in,sizeof(server_in)); Server_in.sin_family=af_inet; Server_in.sin_port=htons (PORT); Inet_pton (Af_inet,ip,&server_in.sin_addr); if(Bind (SOCKFD, (structsockaddr*) &server_in,sizeof(server_in)) == -1) {fprintf (stderr,"bind Fail,error%s", Strerror (errno)); return-1;    } listen (Sockfd,backlog); structsockaddr_in Client; intLen =sizeof(client); intAfD; intpid;  while(1)    {        if(AfD = Accept (SOCKFD, (structsockaddr*) (&client,&len)) = =-1) {fprintf (stderr,"Accept fail,error:%s\n", Strerror (errno)); Exit (1); }        if(PID = fork ()) >0) {close (AfD); Continue; }        Else if(PID = =0) {close (SOCKFD);            Send_message (afd,client); Exit (0); }        Else{printf ("fork () error!\n"); Exit (0); }} close (SOCKFD);}

Client.c

#include"data.h"voidSend_message (FILE *FP,intsockfd) {    CharSENDLINE[MAXN]; CharRECVBUF[MAXN]; intnum; printf ("Connect to server!\n"); printf ("Please input client name: \ n"); if(Fgets (SENDLINE,MAXN,FP) = =NULL) {printf ("Input name error!\n"); Exit (1); } Send (SOCKFD,SENDLINE,MAXN,0);  while(1) {printf ("Please input message!\n"); if(Fgets (SENDLINE,MAXN,FP) = = NULL) Break; Send (Sockfd,sendline,strlen (sendline),0); Bzero (&recvbuf,sizeof(RECVBUF)); Recv (SOCKFD,RECVBUF,MAXN,0); intLen =strlen (RECVBUF); Recvbuf[len]=' /'; printf ("client Receive message is%s\n", RECVBUF); } printf ("Client exit!\n");}intMain () {structsockaddr_in client_in; intSOCKFD; if(SOCKFD = socket (Af_inet,sock_stream,0)) == -1) {fprintf (stderr,"Socket Fail,error%s\n", Strerror (errno)); return-1; } bzero (&client_in,sizeof(client_in)); Client_in.sin_family=af_inet; Client_in.sin_port=htons (PORT); Inet_pton (Af_inet,ip,&client_in.sin_addr); Connect (SOCKFD, (structsockaddr*) &client_in,sizeof(client_in));    Send_message (STDIN,SOCKFD);    Close (SOCKFD); return 0;}

Multi-Process Concurrent server

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.