Skip to content

Commit d42d9c7

Browse files
committed
Add programs to test sendfile, recvfile
1 parent 8e9c909 commit d42d9c7

File tree

4 files changed

+174
-2
lines changed

4 files changed

+174
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
test/run_tests
3434
progs/server
3535
progs/client
36+
progs/sendfile
37+
progs/recvfile
3638

3739
# Temporary things
3840
temp/

progs/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ CC = gcc
22
CC_FLAGS = -Wall -pedantic
33
CL_FLAGS = -L../src -ludt-c -lpthread
44
INC = ../include/udt.h
5-
OBJS = server.o client.o
6-
TARGETS = server client
5+
TARGETS = server client sendfile recvfile
76

87
all : $(TARGETS)
98

progs/recvfile.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <netdb.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
#include "../include/udt.h"
8+
9+
#define HOST "127.0.0.1"
10+
#define PORT "8000"
11+
#define BUFFER_SIZE 10240
12+
13+
int main(int argc, char *argv[])
14+
{
15+
socket_t sock;
16+
int err;
17+
struct addrinfo hints,
18+
*result;
19+
20+
udt_startup();
21+
22+
/* get address info */
23+
memset(&hints, 0, sizeof(hints));
24+
hints.ai_flags = AI_PASSIVE;
25+
hints.ai_family = AF_INET;
26+
hints.ai_socktype = SOCK_DGRAM;
27+
/*hints.ai_socktype = SOCK_STREAM;*/
28+
29+
if ((err = getaddrinfo(NULL, PORT, &hints, &result)) != 0) {
30+
fprintf(stderr, "Error: %s\n", gai_strerror(err));
31+
exit(err);
32+
}
33+
34+
/* create a socket */
35+
sock = udt_socket(result -> ai_family,
36+
result -> ai_socktype,
37+
result -> ai_protocol);
38+
if (sock == -1) {
39+
fprintf(stderr, "Could not create socket\n");
40+
exit(errno);
41+
}
42+
43+
/* connect to server */
44+
if (udt_connect(sock, result -> ai_addr, result -> ai_addrlen) == -1) {
45+
fprintf(stderr, "Could not connect to socket\n");
46+
exit(errno);
47+
} else {
48+
fprintf(stderr, "Connected\n");
49+
}
50+
51+
freeaddrinfo(result);
52+
53+
/* send, recv */
54+
char reqfile[BUFFER_SIZE];
55+
char filename[BUFFER_SIZE];
56+
printf("\n\tGet file: ");
57+
scanf("%s", reqfile);
58+
printf("\tSave as: ");
59+
scanf("%s", filename);
60+
udt_send(sock, reqfile, strlen(reqfile), 0);
61+
62+
/* send file */
63+
int filefd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
64+
int64_t offset = 0;
65+
if (filefd < -1) return 2;
66+
if (udt_recvfile(sock, filefd, &offset, 10, 0) < 0) return 1;
67+
close(filefd);
68+
69+
printf("Disconnected\n");
70+
71+
/* close the connection */
72+
udt_close(sock);
73+
return 0;
74+
}

progs/sendfile.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <netdb.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
8+
#include "../include/udt.h"
9+
10+
#define BACKLOG 5
11+
#define HOST "127.0.0.1"
12+
#define PORT "8000"
13+
#define BUFFER_SIZE 10240
14+
15+
int main(int argc, char *argv[])
16+
{
17+
socket_t sock, conn;
18+
int err;
19+
struct addrinfo hints, *result;
20+
21+
udt_startup();
22+
23+
/* get address info */
24+
memset(&hints, 0, sizeof(hints));
25+
hints.ai_flags = AI_PASSIVE;
26+
hints.ai_family = AF_INET;
27+
hints.ai_socktype = SOCK_DGRAM;
28+
/*hints.ai_socktype = SOCK_STREAM;*/
29+
30+
if ((err = getaddrinfo(NULL, PORT, &hints, &result)) != 0) {
31+
fprintf(stderr, "Error: %s\n", gai_strerror(err));
32+
exit(err);
33+
}
34+
35+
/* create a socket */
36+
sock = udt_socket(result -> ai_family,
37+
result -> ai_socktype,
38+
result -> ai_protocol);
39+
if (sock == -1) {
40+
fprintf(stderr, "Could not create socket\n");
41+
exit(errno);
42+
}
43+
44+
/* bind to address */
45+
if (udt_bind(sock, result -> ai_addr, result -> ai_addrlen) == -1) {
46+
fprintf(stderr, "Could not bind socket\n");
47+
exit(errno);
48+
}
49+
50+
freeaddrinfo(result);
51+
52+
if (hints.ai_socktype == SOCK_STREAM) {
53+
54+
/* listen for connections */
55+
if (udt_listen(sock, BACKLOG) == -1) {
56+
fprintf(stderr, "Could not listen on socket\n");
57+
exit(errno);
58+
} else {
59+
fprintf(stdout, "Listening on %s\n", PORT);
60+
}
61+
62+
/* get a connection */
63+
if ((conn = udt_accept(sock, NULL, NULL)) == -1) {
64+
fprintf(stderr, "Connection failed\n");
65+
exit(errno);
66+
} else {
67+
fprintf(stdout, "New connection\n");
68+
}
69+
70+
} else {
71+
conn = sock;
72+
fprintf(stdout, "Active on %s\n", PORT);
73+
}
74+
75+
/* send, recv filename */
76+
char filename[BUFFER_SIZE];
77+
memset(filename, 0, sizeof(filename));
78+
udt_recv(conn, filename, BUFFER_SIZE, 0);
79+
printf("\tFilename: %s\n\n", filename);
80+
81+
/* recv file */
82+
int filefd = open(filename, O_RDONLY);
83+
int64_t offset = 0;
84+
if (filefd < 0) return 2;
85+
if (udt_sendfile(conn, filefd, offset, 10, 0) < 0) return 1;
86+
close(filefd);
87+
88+
printf("\nClient disconnected\n");
89+
90+
/* close connection */
91+
if (udt_close(sock) == -1) {
92+
fprintf(stderr, "Could not close socket\n");
93+
exit(errno);
94+
}
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)