//广播接收端
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define ERR(msg) do{fprintf(stderr,"%d--",__LINE__);\
perror(msg);}while(0)
#define SER_PORT 7777
#define SER_IP "192.168.8.255"//广播IP地址
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR("socket");
return -1;
}
//绑定服务器的IP和端口号
struct sockaddr_in sin={AF_INET,\
htons(SER_PORT),inet_addr(SER_IP)};
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR("bind");
return -1;
}
printf("服务器已就绪\n");
char buf[128]="";
struct sockaddr_in cin;
socklen_t addr=sizeof(cin);
while(1)
{
//接收广播消息
bzero(buf,sizeof(buf));
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addr)<0)
{
ERR("recvfrom");
return -1;
}
printf("[%s:%d] %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
//关闭文件描述符
close(sfd);
return 0;
}
//广播发送端
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define ERR(msg) do{fprintf(stderr,"%d--",__LINE__);\
perror(msg);}while(0)
#define SER_PORT 7777
#define SER_IP "192.168.8.255"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR("socket");
return -1;
}
//设置广播
int a=1;
if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&a,sizeof(a))<0)
{
ERR("setsockopt");
return -1;
}
//获取服务器的IP和端口号
struct sockaddr_in sin={AF_INET,\
htons(SER_PORT),inet_addr(SER_IP)};
char buf[128]="";
socklen_t addr=sizeof(sin);
while(1)
{
//发送消息
bzero(buf,sizeof(buf));
printf("input:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR("sendto");
return -1;
}
}
//关闭文件描述符
close(sfd);
return 0;
}
//组播接收端
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define ERR(msg) do{fprintf(stderr,"%d--",__LINE__);\
perror(msg);}while(0)
#define SER_PORT 7777
#define SER_IP "224.1.1.1"
#define LOCAL_IP "192.168.8.205"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR("socket");
return -1;
}
//设置组播
struct ip_mreqn info={inet_addr(SER_IP),inet_addr(LOCAL_IP),0};
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&info,sizeof(info))<0)
{
ERR("setsockopt");
return -1;
}
//绑定服务器的IP和端口号
struct sockaddr_in sin={AF_INET,\
htons(SER_PORT),inet_addr(SER_IP)};
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR("bind");
return -1;
}
printf("服务器已就绪\n");
char buf[128]="";
struct sockaddr_in cin;
socklen_t addr=sizeof(cin);
while(1)
{
//接收组播的消息
bzero(buf,sizeof(buf));
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addr)<0)
{
ERR("recvfrom");
return -1;
}
printf("[%s:%d] %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
//关闭文件描述符
close(sfd);
return 0;
}
//组播发送端
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define ERR(msg) do{fprintf(stderr,"%d--",__LINE__);\
perror(msg);}while(0)
#define SER_PORT 7777
#define SER_IP "224.1.1.1"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR("socket");
return -1;
}
//获取服务器的IP和端口号
struct sockaddr_in sin={AF_INET,\
htons(SER_PORT),inet_addr(SER_IP)};
char buf[128]="";
socklen_t addr=sizeof(sin);
while(1)
{
//发送消息给服务器
bzero(buf,sizeof(buf));
printf("input:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR("sendto");
return -1;
}
}
//关闭文件描述符
close(sfd);
return 0;
}
TFTP的下载
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define ERR(msg) do{fprintf(stderr,"%d--",__LINE__);\
perror(msg);}while(0)
#define SER_PORT 69
#define SER_IP "192.168.8.81"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR("socket");
return -1;
}
//获取服务器的IP和端口号
struct sockaddr_in sin={AF_INET,\
htons(SER_PORT),inet_addr(SER_IP)};
//读写请求
char buf[128]="";
int size=sprintf(buf,"%c%c%s%c%s%c",0,1,"1_armcli.c",0,"octet",0);
if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR("sendto");
return -1;
}
//接收数据
char data[516]="";
//发送ACK
char ack[4]="";
struct sockaddr_in temp;
socklen_t addr=sizeof(temp);
int i=1;
//打开文件接收下载的数据
FILE *fp=fopen("./copy.c","w");
if(fp==NULL)
{
ERR("fopen");
return -1;
}
while(1)
{
//接收服务器的数据
bzero(data,sizeof(data));
if(recvfrom(sfd,data,sizeof(data),0,(struct sockaddr*)&temp,&addr)<0)
{
ERR("recvfrom");
return -1;
}
fputs(data+4,fp);
//发送消息给服务器
short* p5=(short*)ack;
*p5=htons(4);
*(p5+1)=htons(i);
if(sendto(sfd,ack,sizeof(ack),0,(struct sockaddr*)&temp,addr)<0)
{
ERR("sendto");
return -1;
}
if(strlen(data+4)<512)
{
printf("%ld\n",strlen(data+4));
printf("下载完成\n");
break;
}
i++;
}
//关闭文件描述符
fclose(fp);
close(sfd);
return 0;
}