1、广播
接收端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>#define ERR_MSG(msg) do{\
printf("%s:%s:%d\n", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)int main(int argc, const char *argv[])
{
if(3 != argc){
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(-1);
}int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
ERR_MSG("socket");
return -1;
}struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);
if(bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len) < 0)
{
ERR_MSG("bind");
return -1;
}struct sockaddr_in clientaddr;
memset(&clientaddr, 0, sizeof(clientaddr));
socklen_t clientaddr_len = sizeof(clientaddr);char buf[128] = "";
int rec_data = 0;
int res = 0;while(1)
{
if((rec_data = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&clientaddr, &clientaddr_len)) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(!strncmp(buf, "quit", 4)){
printf("客户端[%s:%d]退出了..\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
break;
}
printf("客户端[%s:%d]发来了数据[%s]\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), buf);
}close(sockfd);
return 0;
}
发送端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>#define ERR_MSG(msg) do{\
printf("%s:%s:%d\n", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)int main(int argc, const char *argv[])
{
if(3 != argc)
{
printf("Usage : %s <IP> <port>\n", argv[0]);
return -1;
}int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
ERR_MSG("socket");
}struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);
int value = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}char buff[128] = "";
while(1)
{
printf("please enter send message>>>");
fgets(buff, 128, stdin);
buff[strlen(buff)-1] = '\0';if(sendto(sockfd, buff, 128, 0, (struct sockaddr *)&serveraddr, serveraddr_len) < 0)
{
ERR_MSG("send");
return -1;
}
if(!strcmp(buff, "quit"))
{
break;
}
}
close(sockfd);return 0;
}
效果
2、组播
发送端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>#define ERR_MSG(msg) do{\
printf("%s:%s:%d\n", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)int main(int argc, const char *argv[])
{
if(3 != argc){
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(-1);
}int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
ERR_MSG("socket error");
return -1;
}struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);
if(bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len) < 0)
{
ERR_MSG("bind");
return -1;
}struct ip_mreqn mreqn;
mreqn.imr_multiaddr.s_addr = inet_addr(argv[1]);
mreqn.imr_address.s_addr = inet_addr("192.168.43.155");
mreqn.imr_ifindex = 0;
if(setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}struct sockaddr_in clientaddr;
memset(&clientaddr, 0, sizeof(clientaddr));
socklen_t clientaddr_len = sizeof(clientaddr);char buff[128] = "";
int nbytes = 0;
int ret = 0;while(1)
{
if((nbytes = recvfrom(sockfd, buff, 128, 0, (struct sockaddr *)&clientaddr, &clientaddr_len)) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(!strncmp(buff, "quit", 4))
{
printf("客户端[%s:%d]退出了..\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
break;
}
printf("客户端[%s:%d]发来了数据[%s]\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), buff);
}close(sockfd);
return 0;
}
接收器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>#define ERR_MSG(msg) do{\
printf("%s:%s:%d\n", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)int main(int argc, const char *argv[])
{
if(3 != argc){
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(-1);
}int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
ERR_MSG("socket");
return -1;
}struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);
char buff[128] = "";
while(1)
{
printf("please enter meseage>>>");
fgets(buff, 128, stdin);
buff[strlen(buff)-1] = '\0';if(sendto(sockfd, buff, sizeof(buf), 0, (struct sockaddr *)&serveraddr, serveraddr_len) < 0)
{
ERR_MSG("send error");
return -1;
}
if(!strcmp(buff, "quit"))
{
break;
}
}
close(sockfd);return 0;
}
效果
本人
发送
接收
同学接收
3、fttp下载
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>#define ERR_MSG(msg) do{\
printf("%s:%s:%d\n", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)int main(int argc, const char *argv[])
{
if(3 != argc)
{
printf("Usage : %s <服务器ip地址> <服务器端口号>\n", argv[0]);
return -1;;
}int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
ERR_MSG("socket");
}//操作码
short con_num = 0;
//块编码
short pick_no = 0;
//错误信息
char error_msg[512] = {0};
//接受数据的
char buf[600] = {0};
//文件名
char filename[32] = {0};
//文件操作符
int fd = 0;
int rec_pick_num = 0;struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);
printf("请输入要下载的文件名:>>");
scanf("%s", filename);int rec_data = sprintf(buf, "%c%c%s%c%s%c", 0, 1, filename, 0, "octet", 0);
if(sendto(sockfd, buf, rec_data, 0, (struct sockaddr *)&serveraddr, serveraddr_len) < 0)
{
ERR_MSG("sendto error");
return -1;
}while(1)
{
if((rec_data = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&serveraddr, &serveraddr_len)) < 0)
{
ERR_MSG("recvfrom");
return -1;
}con_num = ntohs(*(short *)buf);
pick_no = ntohs(*(short *)(buf+2));
strncpy(error_msg, buf+4, rec_data-4);
if(con_num == 5)
{
printf("errno = [%d] errstr = [%s]\n", pick_no, error_msg);
return -1;
}else if(con_num == 3 && rec_pick_num+1 == pick_no)
{
rec_pick_num++;
if(pick_no == 1)
{
if((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664)) < 0)
{
ERR_MSG("open");
return -1;
}
}
if(write(fd, error_msg, rec_data-4) < 0)
{
ERR_MSG("write");
return -1;
}*(short *)buf = htons(4);
*(short *)(buf+2) = htons(pick_no);if(sendto(sockfd, buf, 4, 0, (struct sockaddr *)&serveraddr, serveraddr_len) < 0)
{
ERR_MSG("sendto");
return -1;
}if(rec_data-4 < 512)
{
break;
}
}
}close(fd);
printf("文件[%s]下载完成..\n", filename);
close(sockfd);return 0;
}
效果