一般的面向连接服务器程序的代码框架:
并发服务器的一般模型:
eg:大写字符串转小写字符串(可并发执行)〈通过fork实现〉
ser.c:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#define BUFF_SIZE 100
#define IP_LEN 16
//实现将一个字符串中所有的大写字母转成小写字母
void func(char* str)
{
if(str==NULL)
return;
char* p=str;
while(*p !='\0')
{
if('A'<=*p && *p<='Z')
*p=*p-'A'+'a';
p++;
}
}
int main()
{
struct sockaddr_in sin; //服务器IP
struct sockaddr_in cin; //客户端IP
int lfd; //监听套接字
int cfd; //客户端套接字
socklen_t len;
char buff[BUFF_SIZE]={0};
char addr_p[IP_LEN]={0};//存储字符串IP地址
int n;
bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(6000);
sin.sin_addr.s_addr=INADDR_ANY; //服务器可以接受任意地址
lfd=socket(AF_INET,SOCK_STREAM,0);
bind(lfd,(struct sockaddr*)&sin,sizeof(sin));
listen(lfd,5);
while(1)
{
int len=sizeof(cin);
cfd=accept(lfd,(struct sockaddr*)&cin,&len);//accept()会返回客户端的套接字
if(cfd==-1)
{
//perror("accept error");
//exit(0);
continue;
}
pid_t pid=fork();
if(pid<0)
{
perror("fork error");
exit(0);
}
else if(pid==0)//在子进程中
{
while(1)
{
close(lfd);//关闭监听套接字,子进程负责处理
n=recv(cfd,buff,BUFF_SIZE,0);
if(n==-1)
{
perror("recv error");
exit(0);
}
else if(n==0)//客户端已关闭
{
printf("one client has closed\n");
close(cfd);//关闭连接套接字
exit(0);
}
inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p));
//将二进制IP转为点分十进制IP
printf("client:IP:%s,port:%d\n",addr_p,ntohs(cin.sin_port));
printf("the data from client is:%s\n",buff);
func(buff);
n=send(cfd,buff,strlen(buff)+1,0);
if(n==-1)
{
perror("send error");
exit(0);
}
}
if(close(cfd)==-1)
{
perror("close error");
exit(0);
}
exit(0);
}
if(close(cfd)==-1)//在父进程中
{
perror("close error");
exit(0);
}
bzero(buff,BUFF_SIZE);
bzero(addr_p,IP_LEN);
}
if(close(lfd)==-1)
{
perror("close error");
exit(0);
}
return 0;
}
1,1 Top
cli.c:
#include<stdlib.h>
#include<ctype.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#define BUFF_SIZE 100
int main()
{
struct sockaddr_in sin;
int sfd;
char buff[BUFF_SIZE]={0};
char str[BUFF_SIZE]={0};
bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(6000);
inet_pton(AF_INET,"127.0.0.1",&sin.sin_addr);
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
exit(0);
}
if((connect(sfd,(struct sockaddr*)&sin,sizeof(sin)))==-1)
{
perror("connect error");
exit(0);
}
while(1)
{
fgets(str,BUFF_SIZE,stdin);
str[strlen(str)-1]='\0';
if(strcmp(str,"end")==0)
{
break;
}
if((send(sfd,str,strlen(str)+1,0))==-1)
{
perror("send error");
exit(0);
}
if((recv(sfd,buff,BUFF_SIZE,0))==-1)
{
perror("recv error");
exit(0);
}
printf("recive from server:%s\n",buff);
bzero(str,BUFF_SIZE);
bzero(buff,BUFF_SIZE);
}
if((close(sfd))==-1)
{
perror("close error");
exit(0);
}
return 0;
}
运行结果:
用线程实现:
ser.c:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<pthread.h>
#define BUFF_SIZE 100
#define IP_LEN 16
//实现将一个字符串中所有的大写字母转成小写字母
void func(char* str)
{
if(str==NULL)
return;
char* p=str;
while(*p !='\0')
{
if('A'<=*p && *p<='Z')
*p=*p-'A'+'a';
p++;
}
}
//线程函数,负责收发数据
void* pthread_fun(void* arg)
{
int* cfd=(int*)arg;
char buff[BUFF_SIZE]={0};
while(1)
{
int n=recv(cfd,buff,BUFF_SIZE,0);
if(n==-1)
{
perror("recv error");
exit(0);
}
else if(n==0)//客户端已关闭
{
printf("one client has closed\n");
close(cfd);//关闭连接套接字
exit(0);
}
printf("the data from client is:%s\n",buff);
func(buff);
n=send(cfd,buff,strlen(buff)+1,0);
if(n==-1)
{
perror("send error");
exit(0);
}
}
if(close(cfd)==-1)
{
perror("close error");
exit(0);
}
pthread_exit(NULL);//退出线程
}
int main()
{
struct sockaddr_in sin; //服务器IP
struct sockaddr_in cin; //客户端IP
int lfd; //监听套接字
int cfd; //客户端套接字
socklen_t len;
char buff[BUFF_SIZE]={0};
char addr_p[IP_LEN]={0};//存储字符串IP地址
int n;
bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(6000);
sin.sin_addr.s_addr=INADDR_ANY; //服务器可以接受任意地址
lfd=socket(AF_INET,SOCK_STREAM,0);
bind(lfd,(struct sockaddr*)&sin,sizeof(sin));
listen(lfd,5);
while(1)
{
int len=sizeof(cin);
cfd=accept(lfd,(struct sockaddr*)&cin,&len);//accept()会返回客户端的套接字
if(cfd==-1)
{
//perror("accept error");
//exit(0);
continue;
}
inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p));
//将二进制IP转为点分十进制IP
printf("client:IP:%s,port:%d\n",addr_p,ntohs(cin.sin_port));
pthread_t pid;
pthread_create(&pid,NULL,pthread_fun,(void*)cfd);
bzero(buff,BUFF_SIZE);
bzero(addr_p,IP_LEN);
}
if(close(lfd)==-1)
{
perror("close error");
exit(0);
}
return 0;
}
cli.c:同上
运行结果: