客户端
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, "line : %d\n", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.0.112"
int main(int argc, char const* argv[])
{
int tfd = socket(AF_INET, SOCK_DGRAM, 0);
if (tfd < 0) {
ERR_MSG("socket");
return -1;
}
// 填充服务器信息
struct sockaddr_in ain;
ain.sin_family = AF_INET;
ain.sin_port = htons(5555);
ain.sin_addr.s_addr = inet_addr(IP);
if (bind(tfd, (struct sockaddr*)&ain, sizeof(ain)) < 0) {
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128] = { 0 };
size_t ret = 0;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while (1) {
memset(buf, 0, sizeof(buf));
ret = recvfrom(tfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
if (ret < 0) {
ERR_MSG("recvfrom");
return -1;
}
printf("%s\n", buf);
strcat(buf, "*_*");
if (sendto(tfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, addrlen) < 0) {
ERR_MSG("sendto");
return -1;
}
}
close(tfd);
return 0;
}
客户端
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, "line : %d\n", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.0.112"
int main(int argc, char const* argv[])
{
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if (cfd < 0) {
ERR_MSG("socket");
return -1;
}
// 填充客户端的地址信息
struct sockaddr_in ain;
ain.sin_family = AF_INET;
ain.sin_port = htons(5555);
ain.sin_addr.s_addr = inet_addr(IP);
// if (bind(tfd, (struct sockaddr*)&ain, sizeof(ain)) < 0) {
// ERR_MSG("bind");
// return -1;
// }
// printf("bind success\n");
char buf[128] = { 0 };
size_t ret = 0;
struct sockaddr_in qin;
socklen_t addrlen = sizeof(qin);
while (1) {
memset(buf, 0, sizeof(buf));
printf("请输入>>\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
if (sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&qin, addrlen) < 0) {
ERR_MSG("sendto");
return -1;
}
ret = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&qin, &addrlen);
if (ret < 0) {
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]", inet_ntoa(qin.sin_addr), ntohs(qin.sin_port));
printf("%s\n", buf);
}
close(cfd);
return 0;
}
下载
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, "line : %d\n", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.0.127"
int main(int argc, char const* argv[])
{
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sfd < 0) {
ERR_MSG("socket");
return -1;
}
// 填充服务器的地址信息结构体,绑定到套接字上
struct sockaddr_in cin;
cin.sin_family = AF_INET; // 必须填充AF_INET
cin.sin_port = htons(69); // 主机字节序
cin.sin_addr.s_addr = inet_addr(IP); // ip字节序
// 发送下载请求
char buf[516] = { 0 };
char buffer[20] = { 0 };
bzero(buf, sizeof(buf));
printf("请输入下载的文件名:\n");
fgets(buffer, sizeof(buffer), stdin);
buffer[strlen(buffer) - 1] = '\0';
unsigned short* p1 = (unsigned short*)buf;
*p1 = htons(1); // 操作码
char* p2 = buf + 2;
strcpy(p2, buffer); // buffer 存放要下载的文件名
char* p3 = p2 + strlen(p2);
*p3 = 0;
char* p4 = p3 + 1;
strcpy(p4, "octet"); // 模式
char* p5 = p4 + strlen(p4);
*p5 = 0;
int size = 2 + 1 + 1 + strlen(p2) + strlen(p4);
/*
char buf[516] = { 0 };
int size = sprintf(buf, "%c%c%s%c%s%c", 0x00, 0x01, "5.png", 0, "octet", 0);*/
// 发送读请求包
if (sendto(sfd, buf, size, 0, (struct sockaddr*)&cin, sizeof(cin)) < 0) {
ERR_MSG("sendto");
return -1;
}
// 获取服务器临时端口
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
int fd = open(buffer, O_RDWR | O_TRUNC | O_CREAT, 0666);
if (fd < 0) {
ERR_MSG("open");
return -1;
}
size_t ret;
while (1) {
bzero(buf, sizeof(buf));
ret = recvfrom(sfd, buf, sizeof(buf), 0,
(struct sockaddr*)&cin, &addrlen);
if (ret < 0) {
ERR_MSG("recvfrom");
return -1;
}
if (buf[1] == 3) {
if (write(sfd, buf + 4, ret - 4) < 0) {
ERR_MSG("write");
return -1;
}
buf[1] = 4;
if (sendto(sfd, buf, 4, 0, (struct sockaddr*)&sin, addrlen) < 0) {
ERR_MSG("sendto");
return -1;
}
if (ret - 4 < 512) {
printf("%s下载完毕\n", buffer);
break;
}
} else if (buf[1] == 5) {
printf("%d %s\n", ntohs(*(short*)(buf + 2)), buf + 4);
break;
}
}
close(sfd);
return 0;
}