プログラムは、マルチプロセスの同時サーバーインスタンスです。サーバとクライアントプログラムを含みます。次のような情報は、コンパイルして実行します。
オペレーティングシステム:CentOSの7
コンパイルツール:GCC
デバッグツール:GDB
次のような機能を実現:
1、接続されている顧客のアドレスが表示されたら、クライアントの接続を待っているサーバは、その後、顧客の名前を受け取り、画面に表示されます。そして、クライアント(文字列)からの情報を受け取ります。それは、文字列を受信するたびに、ディスプレイは、および「正常に送信」メッセージを送信します(「正常に送信」)クライアント。その後、私たちは、クライアントが接続を閉じるまで、クライアントに情報を待ち続けています。サーバーは、複数の顧客を処理する能力を持っています。
2、クライアントが最初のサーバに接続します。接続に成功した後、正常に接続のための情報を表示します。そして、顧客は、ユーザーが入力した名前を受け取り、サーバプログラムに名前を送信します。そして、その文字列がサーバーに送信されたユーザ入力文字列を受け取り、サーバが受信して、正常に送信された番組情報を送り返します。その後、我々は、ユーザーがCtrlキー+ Cを入力するまで、ユーザーの入力を待ち続け ユーザ入力はCtrl + Cを受信した場合、クライアントは接続をクローズして終了します。
サーバープログラムは次のよう:
#include<stdio.h>
#include<strings.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#define PORT 1234
#define BACKLOG 2
#define MAXDATASIZE 1000
void process_cli(int connectfd,struct sockaddr_in client);
int main()
{
int listenfd,connectfd;
pid_t pid;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket() error");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(listenfd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
{
perror("bind() error");
exit(1);
}
if(listen(listenfd,BACKLOG)==-1)
{
perror("listen() error");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((connectfd=accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)
{
perror("accept() error");
exit(1);
}
if((pid=fork())<0)
{
printf("fork error\n");
exit(1);
}
else if(pid==0)
{
close(listenfd);
process_cli(connectfd,client);
exit(0);
}
else
{
close(connectfd);
continue;
}
}
close(listenfd);
}
void process_cli(int connectfd, struct sockaddr_in client)
{
int num,i;
char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE];
printf("You got a connection from:%s.",inet_ntoa(client.sin_addr));
num=recv(connectfd,cli_name,MAXDATASIZE,0);
if(num==0)
{
close(connectfd);
printf("client disconnected.\n");
return;
}
cli_name[num-1]='\0';
printf("Client's name is %s.\n",cli_name);
while((num=recv(connectfd,recvbuf,MAXDATASIZE,0))>0)
{
recvbuf[num]='\0';
printf("Received client (%s) message:%s",cli_name,recvbuf);
send(connectfd,"sent successfully.\n",19,0);
}
close(connectfd);
}
次のようにクライアントプログラムは次のとおりです。
#include<stdio.h>
#include<unistd.h>
#include<strings.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#define PORT 1234
#define MAXDATASIZE 100
void process(FILE *fp,int socket);
char* getMessage(char* sendline,int len,FILE* fp);
int main(int argc,char *argv[])
{
int fd;
struct hostent *he;
struct sockaddr_in server;
if(argc!=2)
{
printf("Usage :%s <IP Address>\n",argv[0]);
exit(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
printf("gethostbyname() error.\n");
exit(1);
}
if((fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket() error");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
server.sin_addr=*((struct in_addr *)he->h_addr);
if(connect(fd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
{
perror("connect() error");
exit(1);
}
process(stdin,fd);
close(fd);
}
void process(FILE *fp,int sockfd)
{
int numbytes;
char recvline[MAXDATASIZE],sendline[MAXDATASIZE];
printf("Connected to server.\n");
printf("Input name :");
if(fgets(sendline,MAXDATASIZE,fp)==NULL)
{
printf("\nExit.\n");
return;
}
send(sockfd,sendline,strlen(sendline),0);
while((getMessage(sendline,MAXDATASIZE,fp))!=NULL)
{
send(sockfd,sendline,strlen(sendline),0);
if((numbytes=recv(sockfd,recvline,MAXDATASIZE,0))==0)
{
printf("Server terminated.\n");
return;
}
recvline[numbytes]='\0';
printf("Server Message :%s\n",recvline);
}
printf("\nExit.\n");
}
char *getMessage(char *sendline,int len,FILE *fp)
{
printf("Input string to server:");
return(fgets(sendline,MAXDATASIZE,fp));
}
次のような結果を実行しているサーバープログラムは以下のとおりです。
[root@mylinux 20160920]# ./srv
You got a connection from:127.0.0.1.Client's name is client1.
Received client (client1) message:1234
Received client (client1) message:abc
Received client (client1) message:efg
You got a connection from:127.0.0.1.Client's name is client2.
Received client (client2) message:12345667
Received client (client2) message:qazwsx
Received client (client1) message:123abcd
クライアントは、次のように1つの結果は実行されます。
[root@mylinux 20160920]# ./cli 127.0.0.1
Connected to server.
Input name :client1
Input string to server:1234
Server Message :sent successfully.
Input string to server:abc
Server Message :sent successfully.
Input string to server:efg
Server Message :sent successfully.
Input string to server:123abcd
Server Message :sent successfully.
Input string to server:
Exit.
次のようにクライアント2の結果は以下のとおりです。
[root@mylinux 20160920]# ./cli 127.0.0.1
Connected to server.
Input name :client2
Input string to server:12345667
Server Message :sent successfully.
Input string to server:qazwsx
Server Message :sent successfully.
Input string to server:
Exit.