udp下载
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d: %s %s\n", __LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
typedef struct {
char *client_ip;
int client_hort;
char *server_ip;
int server_hort;
void (*handler)();
} udp_options;
int sfd;
struct sockaddr_in s_in;
struct sockaddr_in c_in;
char *data_packets = NULL;
ssize_t req;
ssize_t res;
socklen_t addrlen;
uint16_t block_code;
int error_count = 0;
FILE *file;
void get_data();
void packet_handler();
void *udp(void *);
void handler();
void init_ftp(char *, int);
int main(int argc, const char *argv[]) {
udp_options options = {
"192.168.2.47",
6666,
"192.168.2.57",
69,
handler};
udp(&options);
}
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d: %s %s\n", __LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
typedef struct {
char *client_ip;
int client_hort;
char *server_ip;
int server_hort;
void (*handler)();
} udp_options;
int sfd;
struct sockaddr_in s_in;
struct sockaddr_in c_in;
char *data_packets = NULL;
ssize_t req;
ssize_t res;
socklen_t addrlen;
uint16_t block_code;
int error_count = 0;
FILE *file;
void get_data();
void packet_handler();
void *udp(void *);
void handler();
void init_ftp(char *, int);
int main(int argc, const char *argv[]) {
udp_options options = {
"192.168.2.47",
6666,
"192.168.2.57",
69,
handler};
udp(&options);
}
void packet_handler() {
uint16_t *opcode = (uint16_t *) data_packets;
if (req != 516) {
if (ntohs(*opcode) == (uint16_t) 5) {
uint16_t e_code = ntohs(*((uint16_t *) data_packets + 1));
printf("%d:%s", e_code, data_packets + 4);
exit(0);
} else {
fwrite(data_packets + 4, req - 4, 1, file);
exit(0);
}
} else {
block_code = *(opcode + 1);
fwrite(data_packets + 4, 512, 1, file);
}
}
void get_data() {
while (1) {
uint16_t ack[2] = {ntohs((short) 4), block_code};
printf("%d\n", ntohs(block_code));
res = sendto(sfd, (char *) ack, 4, 0, (struct sockaddr *) &s_in, addrlen);
if (res < 0) {
ERR_MSG("send");
exit(-1);
}
bzero(data_packets, 516);
req = recvfrom(sfd, data_packets, 516, 0, (struct sockaddr *) &s_in, &addrlen);
if (req < 0) {
ERR_MSG("recvfrom");
exit(-1);
}
packet_handler();
}
}
void *udp(void *arg) {
udp_options *options = (udp_options *) arg;
c_in.sin_family = AF_INET;
c_in.sin_port = htons(options->client_hort);
c_in.sin_addr.s_addr = inet_addr(options->client_ip);
s_in.sin_family = AF_INET;
s_in.sin_port = htons(options->server_hort);
s_in.sin_addr.s_addr = inet_addr(options->server_ip);
sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sfd < 0) {
ERR_MSG("socket");
exit(-1);
}
printf("create socket success sfd=%d __%d__\n", sfd, __LINE__);
if (bind(sfd, (struct sockaddr *) &c_in, sizeof(c_in)) < 0) {
ERR_MSG("bind");
exit(-1);
}
printf("bind success __%d__\n", __LINE__);
options->handler();
close(sfd);
return NULL;
}
void init_ftp(char *filename, int type) {
char *mode = "octet";
unsigned long len = 4 + strlen(filename) + strlen(mode);
char *buf = (char *) malloc(len);
data_packets = (char *) malloc(516);
sprintf(buf, "%c%c%s%c%s%c", 0, (char) type, filename, 0, mode, 0);
addrlen = sizeof(s_in);
res = sendto(sfd, buf, len, 0, (struct sockaddr *) &s_in, addrlen);
if (res < 0) {
ERR_MSG("send");
exit(-1);
}
bzero(data_packets, 516);
req = recvfrom(sfd, data_packets, 516, 0, (struct sockaddr *) &s_in, &addrlen);
if (req < 0) {
ERR_MSG("recvfrom");
exit(-1);
}
file = fopen("./qybl.jpg", "w");
packet_handler();
}
void handler() {
init_ftp("qybl.jpg", 1);
get_data();
}
void packet_handler() {
uint16_t *opcode = (uint16_t *) data_packets;
if (req != 516) {
if (ntohs(*opcode) == (uint16_t) 5) {
uint16_t e_code = ntohs(*((uint16_t *) data_packets + 1));
printf("%d:%s", e_code, data_packets + 4);
exit(0);
} else {
fwrite(data_packets + 4, req - 4, 1, file);
exit(0);
}
} else {
block_code = *(opcode + 1);
fwrite(data_packets + 4, 512, 1, file);
}
}
void get_data() {
while (1) {
uint16_t ack[2] = {ntohs((short) 4), block_code};
printf("%d\n", ntohs(block_code));
res = sendto(sfd, (char *) ack, 4, 0, (struct sockaddr *) &s_in, addrlen);
if (res < 0) {
ERR_MSG("send");
exit(-1);
}
bzero(data_packets, 516);
req = recvfrom(sfd, data_packets, 516, 0, (struct sockaddr *) &s_in, &addrlen);
if (req < 0) {
ERR_MSG("recvfrom");
exit(-1);
}
packet_handler();
}
}
void *udp(void *arg) {
udp_options *options = (udp_options *) arg;
c_in.sin_family = AF_INET;
c_in.sin_port = htons(options->client_hort);
c_in.sin_addr.s_addr = inet_addr(options->client_ip);
s_in.sin_family = AF_INET;
s_in.sin_port = htons(options->server_hort);
s_in.sin_addr.s_addr = inet_addr(options->server_ip);
sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sfd < 0) {
ERR_MSG("socket");
exit(-1);
}
printf("create socket success sfd=%d __%d__\n", sfd, __LINE__);
if (bind(sfd, (struct sockaddr *) &c_in, sizeof(c_in)) < 0) {
ERR_MSG("bind");
exit(-1);
}
printf("bind success __%d__\n", __LINE__);
options->handler();
close(sfd);
return NULL;
}
void init_ftp(char *filename, int type) {
char *mode = "octet";
unsigned long len = 4 + strlen(filename) + strlen(mode);
char *buf = (char *) malloc(len);
data_packets = (char *) malloc(516);
sprintf(buf, "%c%c%s%c%s%c", 0, (char) type, filename, 0, mode, 0);
addrlen = sizeof(s_in);
res = sendto(sfd, buf, len, 0, (struct sockaddr *) &s_in, addrlen);
if (res < 0) {
ERR_MSG("send");
exit(-1);
}
bzero(data_packets, 516);
req = recvfrom(sfd, data_packets, 516, 0, (struct sockaddr *) &s_in, &addrlen);
if (req < 0) {
ERR_MSG("recvfrom");
exit(-1);
}
file = fopen("./qybl.jpg", "w");
packet_handler();
}
void handler() {
init_ftp("qybl.jpg", 1);
get_data();
}