作业需求:
完成TCP的服务器,客户端,上交。服务器,客户端需要做到随时收发:多进程多线程实现
实现过程:
1.TCP服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.8.18"
#define PORT 6666
char buf[128] ="";
ssize_t res =0;
int newfd=0;
pthread_t tid1,tid2;
void *partA(void *arg)
{
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
}
else if(0==res)
{
fprintf(stderr,"newfd=%d 客户端下线\n",newfd);
break;
}
printf("newfd=%d : %s\n",newfd,buf);
if((strcmp(buf,"quit"))==0)
{
break;
}
}
pthread_cancel(tid2);
pthread_exit(NULL);
}
void *partB(void * arg)
{
while(1)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
}
printf("send success\n");
if((strcmp(buf,"quit"))==0)
{
break;
}
}
pthread_cancel(tid1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
pthread_t tid1,tid2;
printf("[%s:%d]newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
if(pthread_create(&tid1,NULL,partA,NULL)<0)
{
ERR_MSG("tid1");
return -1;
}
if(pthread_create(&tid2,NULL,partB,NULL)<0)
{
ERR_MSG("tid2");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
2.TCP客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.8.18"
#define PORT 6666
int sfd;
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
int newfd=0;
char buf[128] ="";
ssize_t res =0;
pthread_t tid1,tid2;
void *partA(void * arg)
{
while(1)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
break;
}
printf("send success\n");
if((strcmp(buf,"quit"))==0)
{
break;
}
}
pthread_cancel(tid2);
pthread_exit(NULL);
}
void *partB(void *arg)
{
while(1)
{
bzero(buf,sizeof(buf));
res = recv(sfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
break;
}
else if(0==res)
{
fprintf(stderr,"newfd=%d 客户端下线\n",sfd);
break;
}
printf("newfd=%d : %s\n",sfd,buf);
if((strcmp(buf,"quit"))==0)
{
break;
}
}
pthread_cancel(tid1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
printf("bind success\n");
if(-1==connect(sfd,(struct sockaddr*)&sin,sizeof(sin)))
{
ERR_MSG("connect error");
return -1;
}
if(pthread_create(&tid1,NULL,partA,NULL)<0)
{
ERR_MSG("tid1");
return -1;
}
if(pthread_create(&tid2,NULL,partB,NULL)<0)
{
ERR_MSG("tid2");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
实验结果:
ubuntu@ubuntu:10_复习$ ./a.out
允许端口快速重用成功
bind success
listen success
[192.168.8.18:36428]newfd=4
newfd=4 : 004
newfd=4 : 442
newfd=4 : 332
421
send success
235
send success
355
send success
quit
send success
newfd=4 客户端下线
ubuntu@ubuntu:10_复习$
ubuntu@ubuntu:10_复习$ gcc client.c -pthread
ubuntu@ubuntu:10_复习$ ./a.out
bind success
004
send success
442
send success
332
send success
newfd=3 : 421
newfd=3 : 235
newfd=3 : 355
newfd=3 : quit
ubuntu@ubuntu:10_复习$