设计一个linux c语言,基于TCP的多线程服务器和客户端发送程 序
下面第一个回答为多进程,请高手更改成多线程服务器gccserver.c-oserver客户端gccclient.c-oclient新手,改完客户端和服务器都要,调通就给分...
下面第一个回答为多进程,请高手更改成多线程
服务器gcc server.c -o server
客户端gcc client.c -o client
新手,改完客户端和服务器都要,调通就给分,拜托啦 展开
服务器gcc server.c -o server
客户端gcc client.c -o client
新手,改完客户端和服务器都要,调通就给分,拜托啦 展开
展开全部
不太明白你的意思,你所指的多线程是server每accept一个连接就fork一个线程吗?
参考资料: http://mktime.tk
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
客户端
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 1234
#define MAXDATASIZE 1000
void process(FILE *fp,int sockfd);
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.\n");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr=*((struct in_addr *)he->h_addr);
if(connect(fd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
{
perror("connect() error.\n");
exit(1);
}
process(stdin,fd);
close(fd);
}
void process(FILE *fp,int sockfd)
{
char sendbuf[MAXDATASIZE];
char recvbuf[MAXDATASIZE];
int num;
printf("Input your name:\n");
if(fgets(sendbuf,MAXDATASIZE,fp)==NULL)
{
printf("lease enter your name,now you have exit.\n");
return;
}
send(sockfd,sendbuf,strlen(sendbuf),0);
while(getMessage(sendbuf,MAXDATASIZE,fp)!=NULL)
{
send(sockfd,sendbuf,strlen(sendbuf),0);
if((num=recv(sockfd,recvbuf,MAXDATASIZE,0))==0)
{
printf("Server no send you any data.\n");
return;
}
recvbuf[num]='\0';
printf("Server Message:%s\n",recvbuf);
}
printf("Exit.\n");
}
char *getMessage(char *sendline,int len,FILE *fp)
{
printf("Input string to server:\n");
return(fgets(sendline,len,fp));
}
服务器
#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 <signal.h>
#include <wait.h>
#include <errno.h>
#define PORT 1234
#define BACKLOG 2
#define MAXDATASIZE 1000
void process_cli(int connectfd,struct sockaddr_in client);
void sig_handler(int s);
main()
{
int opt,listenfd,connectfd;
pid_t pid;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
struct sigaction act;
struct sigaction oact;
act.sa_handler=sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
if(sigaction(SIGCHLD,&act,&oact)<0)
{
perror("Sigaction failed!\n");
exit(1);
}
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Creating socket failed.\n");
exit(1);
}
opt=SO_REUSEADDR;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
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.\n");
exit(1);
}
if(listen(listenfd,BACKLOG)==-1)
{
perror("listen() error.\n");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1)
{
if((connectfd=accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)
{
if(errno==EINTR) continue;
perror("accept() error.\n");
exit(1);
}
if((pid=fork())>0)
{
close(connectfd);
continue;
}
else if(pid==0)
{
close(listenfd);
process_cli(connectfd,client);
exit(0);
}
else
{
printf("fork error.\n");
exit(1);
}
}
close(listenfd);
}
void process_cli(int connectfd,struct sockaddr_in client)
{
int i,num;
char recvbuf[MAXDATASIZE];
char sendbuf[MAXDATASIZE];
char cli_name[MAXDATASIZE];
printf("You got a connection from %s.\n",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]='\0';
printf("Client's name is :%s.\n",cli_name);
while(num=recv(connectfd,recvbuf,MAXDATASIZE,0))
{
recvbuf[num]='\0';
printf("Received client(%s) message: %s\n",cli_name,recvbuf);
/*for(i=0;i<num;i++)
{
sendbuf[i]=recvbuf[num-i-1];
}
sendbuf[num]='\0';
send(connectfd,sendbuf,strlen(sendbuf),0);*/
send(connectfd,recvbuf,strlen(recvbuf),0);
}
close(connectfd);
}
void sig_handler(int s)
{
pid_t pid;
int stat;
while((pid=waitpid(-1,&stat,WNOHANG))>0)
printf("child %d terminated.\n",pid);
return;
}
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 1234
#define MAXDATASIZE 1000
void process(FILE *fp,int sockfd);
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.\n");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr=*((struct in_addr *)he->h_addr);
if(connect(fd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
{
perror("connect() error.\n");
exit(1);
}
process(stdin,fd);
close(fd);
}
void process(FILE *fp,int sockfd)
{
char sendbuf[MAXDATASIZE];
char recvbuf[MAXDATASIZE];
int num;
printf("Input your name:\n");
if(fgets(sendbuf,MAXDATASIZE,fp)==NULL)
{
printf("lease enter your name,now you have exit.\n");
return;
}
send(sockfd,sendbuf,strlen(sendbuf),0);
while(getMessage(sendbuf,MAXDATASIZE,fp)!=NULL)
{
send(sockfd,sendbuf,strlen(sendbuf),0);
if((num=recv(sockfd,recvbuf,MAXDATASIZE,0))==0)
{
printf("Server no send you any data.\n");
return;
}
recvbuf[num]='\0';
printf("Server Message:%s\n",recvbuf);
}
printf("Exit.\n");
}
char *getMessage(char *sendline,int len,FILE *fp)
{
printf("Input string to server:\n");
return(fgets(sendline,len,fp));
}
服务器
#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 <signal.h>
#include <wait.h>
#include <errno.h>
#define PORT 1234
#define BACKLOG 2
#define MAXDATASIZE 1000
void process_cli(int connectfd,struct sockaddr_in client);
void sig_handler(int s);
main()
{
int opt,listenfd,connectfd;
pid_t pid;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
struct sigaction act;
struct sigaction oact;
act.sa_handler=sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
if(sigaction(SIGCHLD,&act,&oact)<0)
{
perror("Sigaction failed!\n");
exit(1);
}
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Creating socket failed.\n");
exit(1);
}
opt=SO_REUSEADDR;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
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.\n");
exit(1);
}
if(listen(listenfd,BACKLOG)==-1)
{
perror("listen() error.\n");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1)
{
if((connectfd=accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)
{
if(errno==EINTR) continue;
perror("accept() error.\n");
exit(1);
}
if((pid=fork())>0)
{
close(connectfd);
continue;
}
else if(pid==0)
{
close(listenfd);
process_cli(connectfd,client);
exit(0);
}
else
{
printf("fork error.\n");
exit(1);
}
}
close(listenfd);
}
void process_cli(int connectfd,struct sockaddr_in client)
{
int i,num;
char recvbuf[MAXDATASIZE];
char sendbuf[MAXDATASIZE];
char cli_name[MAXDATASIZE];
printf("You got a connection from %s.\n",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]='\0';
printf("Client's name is :%s.\n",cli_name);
while(num=recv(connectfd,recvbuf,MAXDATASIZE,0))
{
recvbuf[num]='\0';
printf("Received client(%s) message: %s\n",cli_name,recvbuf);
/*for(i=0;i<num;i++)
{
sendbuf[i]=recvbuf[num-i-1];
}
sendbuf[num]='\0';
send(connectfd,sendbuf,strlen(sendbuf),0);*/
send(connectfd,recvbuf,strlen(recvbuf),0);
}
close(connectfd);
}
void sig_handler(int s)
{
pid_t pid;
int stat;
while((pid=waitpid(-1,&stat,WNOHANG))>0)
printf("child %d terminated.\n",pid);
return;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询