該程序爲多進程併發服務器實例。包括服務器程序和客戶端程序。編譯及運行的相關信息如下:
操作系統:CentOS 7
編譯工具:GCC
調試工具:GDB
程序實現的功能如下:
1、服務器等候客戶連接,一旦連接成功則顯示客戶的地址,接着接收該客戶的名字並顯示到屏幕。然後接收來自該客戶的信息(字符串)。每當接收到一個字符串,則對其進行顯示,並向客戶端發送“sent successfully”("發送成功")的信息。之後,繼續等待該客戶端的信息直至該客戶關閉連接。服務器具有同時處理多個客戶的能力。
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.