服务器代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.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.90"
struct sockaddr_in cin; //存储获取到的客户端的地址信息
socklen_t addrlen = sizeof(cin); //真实地址信息结构体的大小
pthread_t pth_r;
pthread_t pth_w;
void *pthfunc_r(void *p)
{
char buf[128] = "";
ssize_t res = 0;
int newfd = *(int *)p;
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)
{
fprintf(stderr,"[%s:%d] newfd = %d 客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
//请求制定线程退出
pthread_cancel(pth_w);
break;
}
printf("[%s:%d] newfd = %d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
}
}
void *pthfunc_w(void *p)
{
char buf[128] = "";
ssize_t res = 0;
int newfd = *(int *)p;
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");
return NULL;
}
printf("send success\n");
}
}
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(6666); //端口号的网络字节序1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP地址的网络字节序
//绑定服务器的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");
//从已经完成连接的队列头中获取一个客户端的信息,生成一个新的文件描述符
//生成的新的文件描述符才是与客户端通信的文件描述符
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(&pth_r,NULL,pthfunc_r,&newfd) != 0)
{
ERR_MSG("pthread_create");
return -1;
}
//创建发送线程
if(pthread_create(&pth_w,NULL,pthfunc_w,&newfd) != 0)
{
ERR_MSG("pthread_create");
return -1;
}
//阻塞线程
pthread_join(pth_r,NULL);
pthread_join(pth_w,NULL);
//关闭文件描述符
close(newfd);
close(sfd);
return 0;
}
客户端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.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.90"
struct sockaddr_in cin;
pthread_t pth_r;
pthread_t pth_w;
void *pthfunc_r(void *p)
{
char buf[128] = "";
ssize_t res = 0;
int sfd = *(int *)p;
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)
{
fprintf(stderr,"[%s:%d] sfd = %d 服务器下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),sfd);
//请求制定线程退出
pthread_cancel(pth_w);
break;
}
printf("[%s:%d] sfd = %d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),sfd,buf);
}
}
void *pthfunc_w(void *p)
{
char buf[128] = "";
ssize_t res = 0;
int sfd = *(int *)p;
while(1)
{
//发送
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(0 == strcmp(buf,"quit"))
{
//请求制定线程退出
pthread_cancel(pth_r);
break;
}
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return NULL;
}
printf("send success\n");
}
}
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
cin.sin_family = AF_INET; //必须填AF_INET;
cin.sin_port = htons(6666); //端口号的网络字节序1024~49151
cin.sin_addr.s_addr = inet_addr(IP); //本机IP地址的网络字节序
if(connect(sfd,(struct sockaddr *)&cin,sizeof(cin))<0)
{
ERR_MSG("connect");
return -1;
}
//创建接收线程
if(pthread_create(&pth_r,NULL,pthfunc_r,&sfd) != 0)
{
ERR_MSG("pthread_create");
return -1;
}
//创建发送线程
if(pthread_create(&pth_w,NULL,pthfunc_w,&sfd) != 0)
{
ERR_MSG("pthread_create");
return -1;
}
//阻塞线程
pthread_join(pth_r,NULL);
pthread_join(pth_w,NULL);
//关闭文件描述符
close(sfd);
return 0;
}
终端执行结果: