12月30日网络编程
作业:
1、将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收信息实验。
发送端:
#include <myhead.h>
#define IP "192.168.124.255"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、设置允许广播
int k = 999;
if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&k,sizeof(k))==-1)
{
perror("setsockopt");
return -1;
}
printf("允许广播设置成功\n");
struct sockaddr_in broadcast = {
.sin_family = AF_INET,
.sin_port = htons(PORT),//发送到该端口
.sin_addr.s_addr = inet_addr(IP)
};
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&broadcast,sizeof(broadcast));
if(strcmp(buff,"quit")==0)
{
printf("发送端退出\n");
break;
}
}
close(oldfd);
return 0;
}
接收端
#include <myhead.h>
#define IP "192.168.124.255"
#define PORT 6666
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 send = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = inet_addr(IP)
};
#if 1
if(bind(oldfd,(struct sockaddr *)&send,sizeof(send))==-1)
{
perror("bind");
return -1;
}
#endif
//接收消息
char buff[1024];
while(1)
{
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);
printf("%s",buff);
}
return 0;
}
2、使用多线程基于TCP协议的并发执行,一个服务器对应多个客户端实现通信实验。
//TCP多线程并发 服务器
#include <myhaed.h>
#define IP "192.168.124.40"
#define PORT 1234
void *fun(void *fd) //子线程函数体
{
int newfd=*(int *)fd;
char buff[1024];
while(1)
{
int res=recv(newfd,buff,sizeof(buff),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("%s\n",buff);
strcat(buff,"今晚早睡");
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,20)==-1)
{
perror("listen");
return -1;
}
//4.发送消息
struct sockaddr_in client;
int client_len=sizeof(client);
while(1)
{
//接收客户端请求,并生成新的套接字
int newfd=accept(oldfd,(struct sockaddr *)&client,&client_len);
if(newfd==-1)
{
perror("accept");
return -1;
}
//创建子线程与客户端通话
pthread_t tid;
if(pthread_create(&tid,NULL,fun,&newfd)==-1)
{
perror("pthread_create");
return -1;
}
}
pthread_join(tid,NULL);//回收子线程资源
close(newfd);
close(oldfd);
return 0;
}
//TCP多线程并发 客户端
#include <myhead.h>
#define IP "192.168.124.40"
#define SERPORT PORT 1234
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
#if 0
//2.绑定固定的IP和端口号
struct sockaddr_in client = {
.sin_family =AF_INET,
.sin_port = htons(6789),//自定义端口号
.sin_addr.s_addr = inet_addr(IP)
};
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("bind");
return -1;
}
#endif
//4.连接服务器
struct sockaddr_in server = {
.sin_family =AF_INET,
.sin_port = htons(SERPORT),//注意端口号需要服务器端口
.sin_addr.s_addr = inet_addr(IP)
};
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//5.收发消息
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,strlen(buff),0);
if(strcmp(buff,"quit")==0)//退出客户端
{
break;
}
bzero(buff,sizeof(buff));
recv(oldfd,buff,sizeof(buff),0);//阻塞接收服务器消息
printf("服务器发来消息:%s\n",buff);
}
return 0;
}