网络编程多线程服务器应用
1、多进程多线程并发服务器实现一遍提交
服务器部分代码:
/多线程并发服务器
#include<myhead.h>
#define IP "192.168.60.88"
#define PORT 1028
#define BACKLOG 28
typedef struct
{
struct sockaddr_in client;
int newfd;
}Link;
void *fun(void *sss)
{
int newfd=((Link *)sss)->newfd;
struct sockaddr_in client=((Link *)sss)->client;
printf("%s发来信息\n",inet_ntoa(client.sin_addr));
char buff[1024];
while(1)
{
int len=recv(newfd,buff,sizeof(buff),0);
if(len==0)
{
printf("客户端退出\n");
break;
}
printf("收到消息:%s\n",buff);
strcat(buff,"2000");
send(newfd,buff,sizeof(buff),0);
}
pthread_exit(NULL);
}
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(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//监听
if(listen(oldfd,BACKLOG)==-1)
{
perror("listen");
return -1;
}
struct sockaddr_in client;
socklen_t client_len=sizeof(client);
while(1)
{
int newfd=accept(oldfd,(struct sockaddr *)&client,&client_len);
Link jy;
jy.newfd=newfd;
jy.client=client;
pthread_t tid;
tid=pthread_create(&tid,NULL,fun,&jy);
if(tid==-1)
{
perror("pthread_create");
return -1;
}
pthread_detach(tid);
}
return 0;
}
客户端部分:
#include <myhead.h>
#define IP "192.168.60.88"
#define PORT 1028
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(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//3、数据收发
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);//键盘输入字符串
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,sizeof(buff),0);
int len = recv(oldfd,buff,sizeof(buff),0);
if(len==0)
{
printf("服务器意外退出\n");
break;
}
printf("接收服务器消息:%s\n",buff);
}
close(oldfd);
return 0;
}
代码演示:
2、将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收信息实验。
发送端部分:
#include<myhead.h>
#define IP "192.168.61.255"
#define PORT 1028
int main(int argc, const char *argv[])
{
//1、创建UDP的套接字
int oldfd=socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、设置套接字允许广播
int n=1;
if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&n,sizeof(n))==-1)
{
perror("setsockopt");
return -1;
}
printf("允许广播设置成功\n");
//3、绑定
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("绑定成功\n");
*/
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 1028
int main(int argc, const char *argv[])
{
//1、创建套接字
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");
struct sockaddr_in cy;
socklen_t cy_len =sizeof(cy);
char buff[1024];
while(1)
{
recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cy,&cy_len);
printf("接收到了%s信息:%s\n",inet_ntoa(cy.sin_addr),buff);
strcat(buff,"^_^");
sendto(oldfd,buff ,sizeof(buff),0,(struct sockaddr *)&recv,sizeof(recv));
printf("转发成功\n");
}
close(oldfd);//关闭描述符
return 0;
}
代码演示: