完成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.129" //本机IP地址
#define PORT 6666 //端口号
pthread_t pid_r,pid_s;
char buf[128] = "";
void* call_send(void* arg_s){
int newfd = *((int *)arg_s);
while(1){
//发送
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(strcmp(buf,"quit")==0){
break;
}
printf("服务器发:%s\n",buf);
if(send(newfd,buf,sizeof(buf),0) < 0){
ERR_MSG("send");
return NULL;
}
}
pthread_cancel(pid_r);
pthread_exit(NULL);
}
void* call_recv(void* arg_r){
ssize_t res = 0;
int newfd = *((int *)arg_r);
while(1){
bzero(buf,sizeof(buf));
//接收
res = recv(newfd, buf,sizeof(buf),0);
if(res < 0){
ERR_MSG("recv");
return NULL;
}else if(0 == res){
break;
}
printf("服务器收:%s\n",buf);
}
pthread_cancel(pid_s);
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;
}
//允许端口快速重用--->允许程序退出后,端口号能被新的进程快速覆盖
//必须放在bind之前
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))< 0){
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功\n");
//根据地址族的不同,地址信息结构体不一致,AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序,1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP地址的网络字节序,ifconfig
//绑定服务器的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); //真实地址信息结构体的大小
//从已经完成连接的队列投中获取一个客户端的信息,生成一个新的文件描述符
//生成的新的文件描述符才是与客户端通信的文件描述符
int newfd = 0;
newfd = accept(sfd,(struct sockaddr*)&cin, &addrlen);
if(newfd < 0){
ERR_MSG("accept");
return -1;
}
printf("[%s:%d]newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
//创建线程
if(pthread_create(&pid_s,NULL,call_send,&newfd) != 0){
fprintf(stderr,"pthread_create failded\n");
return -1;
}
if(pthread_create(&pid_r,NULL,call_recv,&newfd) != 0){
fprintf(stderr,"pthread_create failded\n");
return -1;
}
pthread_join(pid_s,NULL);
pthread_join(pid_r,NULL);
//关闭文件描述符
close(newfd);
close(sfd);
printf("服务器退出成功\n");
return 0;
}
客户端代码:
#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.129" //本机IP地址
#define PORT 6666 //端口号
pthread_t pid_r,pid_s;
char buf[128] = "";
void* call_recv(void* arg_r){
ssize_t res = 0;
int sfd = *((int*)arg_r);
while(1){
//接收
bzero(buf,sizeof(buf));
res = recv(sfd,buf,sizeof(buf),0);
if(res < 0){
ERR_MSG("recv");
return NULL;
}else if(0 == res){
break;
}
printf("客户端收:%s\n",buf);
}
pthread_cancel(pid_s);
pthread_exit(NULL);
}
void* call_send(void* arg_s){
int sfd = *((int*)arg_s);
while(1){
//发送
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(strcmp(buf,"quit")==0){
break;
}
printf("客户端发:%s\n",buf);
if(send(sfd,buf,sizeof(buf),0) < 0){
ERR_MSG("send");
return NULL;
}
}
pthread_cancel(pid_r);
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;
}
//根据地址族的不同,地址信息结构体不一致,AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//连接服务器
if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
ERR_MSG("connect");
return -1;
}
//创建线程
if(pthread_create(&pid_r,NULL,call_recv,&sfd) != 0){
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&pid_s,NULL,call_send,&sfd) != 0){
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(pid_r,NULL);
pthread_join(pid_s,NULL);
close(sfd);
printf("客户端退出成功\n");
return 0;
}
运行结果: