1使用多线程基于TCP协议的并发执行,一个服务器对应多个客户端实现通信实验
#include <myhead.h>
//主线程负责监听客户端连接请求
//子线程负责数据收发
#define IP "192.168.60.77"
#define PORT 5555
#define BACKLOG 10
typedef struct
{
struct sockaddr_in client;
int newfd;
}ZYJ;
void * fun(void *sss)
{
int newfd=((ZYJ *)sss)->newfd;
struct sockaddr_in client=((ZYJ*)sss)->client;
char buff[1024];
printf("%s发来信息\n",inet_ntoa(client.sin_addr));
while(1)
{
bzero(buff,sizeof(buff));
int len=recv(newfd,buff,sizeof(buff),0);
if(len==0)
{
printf("客户端退出\n");
}
printf("收到消息:%s\n",buff);
strcat(buff,"-1973");
send(newfd,buff,sizeof(buff),0);
}
pthread_exit(NULL);//线程退出
}
int main(int argc, const char *argv[])
{
//1.创建套接字
int oldfd=socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2.绑定
struct sockaddr_in server={
.sin_family=AF_INET,
.sin_port=htons(PORT),
.sin_addr.s_addr=inet_addr(IP)
};
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//3.监听
if(listen(oldfd,BACKLOG)==-1)
{
perror("listen");
return -1;
}
struct sockaddr_in client;
socklen_t client_len;
while(1)
{
int newfd=accept(oldfd,(struct sockaddr*)&client,&client_len);
ZYJ sb;
sb.newfd=newfd;//结构体成员新描述符
sb.client=client;//结构体成员客户端信息赋值
pthread_t tid;
tid=pthread_create(&tid,NULL,fun,&sb);
if(tid==-1)
{
perror("thread_create");
return -1;
}
pthread_detach(tid);//挂起线程由系统回收资源
}
return 0;
}
#include <myhead.h>
#define IP "192.168.60.77"
#define PORT 5555
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
struct sockaddr_in server={
.sin_family=AF_INET,
.sin_port=htons(PORT),
.sin_addr.s_addr=inet_addr(IP)
};
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//3.数据收发
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
send(oldfd,buff,sizeof(buff),0);
int len=recv(oldfd,buff,sizeof(buff),0);
if(len==0)
{
printf("服务器意外退出\n");
break;
}
printf("接受服务器消息:%s\n",buff);
}
return 0;
}
2.将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收信息
#include <myhead.h>
#define IP "192.168.61.255"
#define PORT 5535
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_DGRAM,0);
int n=1;
setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&n,sizeof(n));
printf("允许广播成功\n");
//绑定
struct sockaddr_in send={
.sin_family=AF_INET,
.sin_port=htons(PORT),
.sin_addr.s_addr=inet_addr(IP)
};
/*
if(bind(oldfd,(struct sockaddr *)&send,sizeof(send))==-1)
{
perror("bind");
return -1;
}
*/
printf("绑定成功");
char buff[1024];
while(1)//信息发送
{
bzero(buff,sizeof(buff));
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1]='\0';
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr*)&send,sizeof(send));
printf("发送成功\n");
}
close(oldfd);
return 0;
}
#include <myhead.h>
#define IP "192.168.61.255"
#define PORT 9999
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2.填充结构体
struct sockaddr_in recv={
.sin_family=AF_INET,
.sin_port=htons(PORT),
.sin_addr.s_addr=inet_addr(IP)
};
//3.绑定
if(bind(oldfd,(struct sockaddr*)&recv,sizeof(recv))==-1)
{
perror("bind");
return -1;
}
printf("绑定成功\n");
char buff[1024];
struct sockaddr_in ajin;
socklen_t ajin_len=sizeof(ajin);
while(1)
{
recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&ajin,&ajin_len);
printf("接收到了%s的信息:%s\n",inet_ntoa(ajin.sin_addr),buff);
//strcat(buff,"^_^");
//sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr*)&recv,sizeof(recv));
//printf("转发成功\n");
}
close(oldfd);
return 0;
}