启动client端和server端后,首先由Client端发送数据“1”给server端,server端接收到数据后加1后发送到client端,server端再加1发给client端,依次类推,直到server端接收到20,client端接收到19,两方中断收发。
要求:用多线程的思想实现
代码如下:
server端:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h>
#define SERV_PORT 9000
#define LISTENQ 1024
#define MAXLINE 4096
#define SA struct sockaddr
void add(int connfd)
{
int k=0;
ssize_t n;
char line[MAXLINE];
while(k<20)
{
if((n=read(connfd,line,MAXLINE))==0)
return;
k=atoi(line);
printf("server receive number is: %d\n",k);
k=k+1;
sprintf(line,"%d",k);
write(connfd,line,strlen(line));
}
printf("my thread pid is %d\n",getpid());
close(connfd);
pthread_exit(NULL);
}
int
main(int argc,char **argv)
{
pthread_t tid;
int listenfd,connfd;
int ret;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
pid_t childpid;
if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("create socket error:%s(errno:%d)\n",strerror(errno),errno);
exit(0);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family =AF_INET;
servaddr.sin_addr.s_addr =htonl(INADDR_ANY);
servaddr.sin_port =htons(SERV_PORT);
if(bind(listenfd,(SA *) &servaddr,sizeof(servaddr))!=0)
{
printf("bind error:%s(errno:%d)\n",strerror(errno),errno);
exit(0);
}
if(listen(listenfd,LISTENQ)==-1)
{
printf("listen socket error:%s(errno:%d)\n",strerror(errno),errno);
exit(0);
}
printf("--------waiting for client request------\n");
for( ; ; )
{ //循环接收各个进程
clilen=sizeof(cliaddr);
if( (connfd=accept(listenfd,(SA *)&cliaddr,&clilen)) < 0)
{
printf("accept error:%s(errno:%d)\n",strerror(errno),errno);
exit(0);
}
ret=pthread_create(&tid,NULL,(void *)add,(void *)connfd);//创建一个线程
if(ret!=0)
{
printf("create pthread error");
exit(0);
}
}
printf("my pid is %d\n",getpid());
close(listenfd);
exit(0);
}
client端:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXLINE 4096
#define SERV_PORT 9000
#define SA struct sockaddr
int
main(int argc,char ** argv)
{
int sockfd;
char sendline[MAXLINE], recvline[MAXLINE];
struct sockaddr_in servaddr;
long arg1;
int m;
if(argc != 2)
printf("usage:./numaddcli <IPaddress>");
if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error:%s(error:%d)\n",strerror(errno),errno);
exit(0);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)
{
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if(connect(sockfd,(SA *) &servaddr,sizeof(servaddr))<0)
{
printf("connect error:%s(error: %d)\n",strerror(errno),errno);
exit(0);
}
printf("send message to server:\n");
scanf("%d",&m);
while(m<=19)
{
printf("the client send number:%d\n",m);
m=m+1;
sprintf(sendline,"%d",m);
write(sockfd,sendline,strlen(sendline));
if(read(sockfd,recvline,MAXLINE)==0)
printf("server terminated prematualy");
m=atoi(recvline);
}
printf("my pid is %d\n",getpid());
exit(0);
}