1. 基于UDP的TFTP文件传输
#include "test.h"
#define SER_IP "192.168.1.104"
#define SER_PORT 69
#define IP "192.168.191.128"
#define PORT 9999
enum mode
{
TFTP_READ = 1,
TFTP_WRITE = 2,
TFTP_DATA = 3,
TFTP_ACK = 4,
TFTP_ERR = 5
};
void get_filename(char* filename, int size)
{
bzero(filename, size);
printf("请输入文件名:");
fgets(filename, size, stdin);
filename[strlen(filename) - 1] = 0;
}
void rw_request(char* pack, int pack_size, char* filename, int mode, int* packlen)
{
bzero(pack, pack_size);
short* p1 = (short*)pack;
*p1 = htons(mode);
char* p2 = (char*)(p1 + 1);
strcpy(p2, filename);
char* p4 = p2 + strlen(p2) + 1;
strcpy(p4, "octet");
*packlen = 4 + strlen(p2) + strlen(p4);
}
void pack_data(char* pack, int num, char* rbuf, int len, int* packlen)
{
bzero(pack, sizeof(pack));
short* p1 = (short*)pack;
*p1 = htons(TFTP_DATA);
short* p2 = p1 + 1;
*p2 = htons(num);
char* p4 = (char*)(p2 + 1);
for (int i = 0; i < len; i++)
{
*(p4 + i) = rbuf[i];
}
*packlen = 4 + len;
}
void pack_ack(char* ack, int num)
{
bzero(ack, 4);
short* a = (short*)ack;
*a = htons(TFTP_ACK);
*(a + 1) = htons(num);
}
void pack_errmsg(char* pack, char* msg, int* packlen)
{
bzero(pack, sizeof(pack));
short* p = (short*)pack;
*p = htons(TFTP_ERR);
*(p + 1) = htons(0);
char* errmsg = (char*)(p + 2);
strcpy(errmsg, msg);
*packlen = 4 + strlen(errmsg);
}
void client_recv(int sfd, struct sockaddr_in* sin, socklen_t* socklen)
{
char filename[128];
get_filename(filename, sizeof(filename));
char pack[516] = "";
int packlen = 0;
rw_request(pack, sizeof(pack), filename, TFTP_READ, &packlen);
sendto(sfd, pack, packlen, 0, (struct sockaddr*)sin, *socklen);
int fd = -1;
if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
{
perror("create file error");
return;
}
char wbuf[512] = "";
int block_num = 0;
while (1)
{
bzero(pack, sizeof(pack));
packlen = recvfrom(sfd, pack, sizeof(pack), 0, (struct sockaddr*)sin, socklen);
short* p = (short*)pack;
short code = ntohs(*p);
short num = ntohs(*(p + 1));
if (3 == code && num == ++block_num)
{
write(fd, pack + 4, packlen - 4);
char ack[4];
pack_ack(ack, block_num);
sendto(sfd, ack, 4, 0, (struct sockaddr*)sin, *socklen);
if (packlen < 512)
{
printf("下载完成\n");
close(fd);
break;
}
}
}
}
void client_send(int sfd, struct sockaddr_in* sin, socklen_t* socklen)
{
char filename[128];
get_filename(filename, sizeof(filename));
char pack[516] = "";
int packlen = 0;
rw_request(pack, sizeof(pack), filename, TFTP_WRITE, &packlen);
sendto(sfd, pack, packlen, 0, (struct sockaddr*)sin, *socklen);
int fd = -1;
if ((fd = open(filename, O_RDONLY)) == -1)
{
perror("open error");
return;
}
char ack[4];
char rbuf[512] = "";
int len;
while (1)
{
recvfrom(sfd, ack, 4, 0, (struct sockaddr*)sin, socklen);
short* a = (short*)ack;
short code = ntohs(*a);
short num = ntohs(*(a + 1));
if (4 == code && (len = read(fd, rbuf, sizeof(rbuf))) > 0)
{
pack_data(pack, num + 1, rbuf, len, &packlen);
sendto(sfd, pack, packlen, 0, (struct sockaddr*)sin, *socklen);
bzero(rbuf, sizeof(rbuf));
}
else
{
printf("上传成功\n");
break;
}
}
}
int main(int argc, char const *argv[])
{
int sfd = -1;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sfd)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n", sfd);
int reuse = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
socklen_t socklen = sizeof(sin);
printf("1.上传\n");
printf("2.下载\n");
printf("0.退出\n");
printf("请输入:");
int n;
scanf("%d", &n);
getchar();
if (1 == n)
{
client_send(sfd, &sin, &socklen);
}
else if (2 == n)
{
client_recv(sfd, &sin, &socklen);
}
else
{
printf("输入错误\n");
}
close(sfd);
return 0;
}
2. TCP机械臂测试