?用Socket实现简单的服务器和客户端的通信,在服务器端使用pthread实现多线程接收信息。
1. 服务器端
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<pthread.h>
#define BUFSIZE 512
int new_sockfd;//连接套接字
void *fun(void *arg);
void *fun(void *arg)
{
int z;//read返回值
char reqBuf[BUFSIZE];//接收缓存区
z=read(new_sockfd,reqBuf,sizeof reqBuf);
if(z<0)
{
fprintf(stderr,"read error:%s\n",strerror(errno));
exit(1);
}
if(z==0)
{
printf("close new_sockfd\n");
close(new_sockfd);
return NULL;
}
reqBuf[z]=0;
printf("receive from client: %s\n",reqBuf);
printf("write down your message to client\n");
fgets(reqBuf,sizeof reqBuf,stdin);
z=write(new_sockfd,reqBuf,sizeof reqBuf);
if(z<0){
printf("write error");
exit(1);
}
}
int main(int argc,char *argv[])
{
int sockfd;//服务器套接字
struct sockaddr_in server_addr;//服务器地址
struct sockaddr_in client_addr;//客户端地址
socklen_t size;
int portnumber;//端口号
pthread_t pth;
if(argc!=2)
{
fprintf(stderr,"%s portnumber\n",argv[0]);
exit(1);
}
if((portnumber=atoi(argv[1]))<0)
{
fprintf(stderr,"%s portnumber\n",argv[0]);
exit(1);
}
if((sockfd=socket(PF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"socket error:%s\n",strerror(errno));
exit(1);
}
memset(&server_addr,0,sizeof server_addr);
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);
if((bind(sockfd,(struct sockaddr *)(&server_addr),sizeof server_addr))==-1)
{
fprintf(stderr,"bind error:%s\n",strerror(errno));
exit(1);
}
if(listen(sockfd,128)==-1){
fprintf(stderr,"listen error:%s\n",strerror(errno));
exit(1);
}
printf("wait for the request from client\n");
while(1)
{
size=sizeof(struct sockaddr_in);
if((new_sockfd = accept(sockfd,(struct sockaddr *)(&client_addr),&size))==-1)
fprintf(stderr,"accept error:%s\n",strerror(errno));
fprintf(stdout,"server got connection from %s\n",inet_ntoa(client_addr.sin_addr));
while(1)
{
//创建线程
if(pthread_create(&pth,NULL,(void *)fun,(void *)&new_sockfd)!=0)
{
perror("pthread create fail");
exit(-1);
}
}
}
}
2. 客户端
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#define BUFSIZE 512
int main(int argc,char *argv[])
{
int sockfd;
char buf[BUFSIZE];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber;
int nbytes;
int z;
char reqBuf[BUFSIZE];
host=gethostbyname(argv[1]);
portnumber=atoi(argv[2]);
if((sockfd=socket(PF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"socket error:%s\n",strerror(errno));
exit(1);
}
memset(&server_addr,0,sizeof server_addr);
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr*)host->h_addr);
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof server_addr)==-1)
{
fprintf(stderr,"connect error:%s\n",strerror(errno));
exit(1);
}
printf("connect success\n");
while(1)
{
printf("please input your message to server\n");
if(!fgets(reqBuf,sizeof reqBuf,stdin))
{
printf("fgets error\n");
break;
}
z=strlen(reqBuf);
if(z>0&&reqBuf[--z]=='\n')
reqBuf[z]=0;
if(z==0)
continue;
if(!strcasecmp(reqBuf,"QUIT"))
{
printf("the Client closed.please enter any key to exit\n");
getchar();
write(sockfd,reqBuf,strlen(reqBuf));
break;
}
z=write(sockfd,reqBuf,strlen(reqBuf));
if(z<0)
{
fprintf(stderr,"write error:%s\n",strerror(errno));
exit(1);
}
if((z=read(sockfd,reqBuf,sizeof(reqBuf)))==-1)
{
fprintf(stderr,"read error:%s\n",strerror(errno));
exit(1);
}
if(z==0)
{
printf("the server closed.please enter any key to exit\n");
getchar();
break;
}
reqBuf[z]=0;
printf("receive message:%s\n",reqBuf);
}
close(sockfd);
return 0;
}
先运行服务器端:./server 9000
后运行客户端:./client 0.0.0.0 9000
本地回环测试
|