diff options
-rw-r--r-- | testing/Lossless_UDP_testclient.c | 130 | ||||
-rw-r--r-- | testing/Lossless_UDP_testserver.c | 124 |
2 files changed, 254 insertions, 0 deletions
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c new file mode 100644 index 00000000..241f5bdc --- /dev/null +++ b/testing/Lossless_UDP_testclient.c | |||
@@ -0,0 +1,130 @@ | |||
1 | /* Lossless_UDP testclient | ||
2 | * A program that connects and sends a file using our lossless UDP algorithm. | ||
3 | * | ||
4 | * Best used in combination with Lossless_UDP_testserver | ||
5 | * | ||
6 | * Compile with: gcc -O2 -Wall -o test ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c | ||
7 | * | ||
8 | * Command line arguments are the ip and port to cennect and send the file to. | ||
9 | * EX: ./test 127.0.0.1 33445 filename.txt | ||
10 | */ | ||
11 | |||
12 | #include "../core/network.h" | ||
13 | #include "../core/Lossless_UDP.h" | ||
14 | |||
15 | #ifdef WIN32 | ||
16 | |||
17 | #define c_sleep(x) Sleep(1*x) | ||
18 | |||
19 | #else | ||
20 | #include <unistd.h> | ||
21 | #include <arpa/inet.h> | ||
22 | #define c_sleep(x) usleep(1000*x) | ||
23 | |||
24 | #endif | ||
25 | |||
26 | |||
27 | #define PORT 33446 | ||
28 | |||
29 | void printpacket(char * data, uint32_t length, IP_Port ip_port) | ||
30 | { | ||
31 | uint32_t i; | ||
32 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | ||
33 | printf("--------------------BEGIN-----------------------------\n"); | ||
34 | for(i = 0; i < length; i++) | ||
35 | { | ||
36 | if(data[i] < 16) | ||
37 | printf("0"); | ||
38 | printf("%hhX",data[i]); | ||
39 | } | ||
40 | printf("\n--------------------END-----------------------------\n\n\n"); | ||
41 | } | ||
42 | |||
43 | |||
44 | int main(int argc, char *argv[]) | ||
45 | { | ||
46 | if (argc < 4) | ||
47 | { | ||
48 | printf("usage: %s ip port filename\n", argv[0]); | ||
49 | exit(0); | ||
50 | } | ||
51 | |||
52 | char buffer[128]; | ||
53 | int read; | ||
54 | |||
55 | FILE *file = fopen(argv[3], "rb"); | ||
56 | if ( file==NULL ){return 1;} | ||
57 | |||
58 | |||
59 | //initialize networking | ||
60 | //bind to ip 0.0.0.0:PORT | ||
61 | IP ip; | ||
62 | ip.i = 0; | ||
63 | init_networking(ip, PORT); | ||
64 | perror("Initialization"); | ||
65 | |||
66 | IP_Port serverip = {{{inet_addr(argv[1])}}, htons(atoi(argv[2]))}; | ||
67 | int connection = new_connection(serverip); | ||
68 | uint64_t timer = current_time(); | ||
69 | while(1) | ||
70 | { | ||
71 | if(is_connected(connection) != 2) | ||
72 | { | ||
73 | if(is_connected(connection) == 1) | ||
74 | { | ||
75 | printf("Connecting took: %llu us", (unsigned long long)(current_time() - timer)); | ||
76 | } | ||
77 | break; | ||
78 | } | ||
79 | c_sleep(1); | ||
80 | } | ||
81 | timer = current_time(); | ||
82 | |||
83 | IP_Port ip_port; | ||
84 | char data[MAX_UDP_PACKET_SIZE]; | ||
85 | uint32_t length; | ||
86 | |||
87 | //read first part of file | ||
88 | read = fread(buffer, 1, 128, file); | ||
89 | |||
90 | while(1) | ||
91 | { | ||
92 | while(recievepacket(&ip_port, data, &length) != -1) | ||
93 | { | ||
94 | if(LosslessUDP_handlepacket(data, length, ip_port)) | ||
95 | { | ||
96 | printpacket(data, length, ip_port); | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | printf("Received handled packet with length: %u\n", length); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | doLossless_UDP(); | ||
105 | |||
106 | if(is_connected(connection) == 1) | ||
107 | { | ||
108 | |||
109 | if(write_packet(connection, buffer, read)) | ||
110 | { | ||
111 | read = fread(buffer, 1, 128, file); | ||
112 | } | ||
113 | if(sendqueue(connection) == 0) | ||
114 | { | ||
115 | if(read == 0) | ||
116 | { | ||
117 | printf("Sent file successfully in: %llu us", (unsigned long long)(current_time() - timer)); | ||
118 | break; | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | printf("Connecting Lost after: %llu us", (unsigned long long)(current_time() - timer)); | ||
125 | } | ||
126 | c_sleep(1); | ||
127 | } | ||
128 | |||
129 | return 0; | ||
130 | } \ No newline at end of file | ||
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c new file mode 100644 index 00000000..9816b8b0 --- /dev/null +++ b/testing/Lossless_UDP_testserver.c | |||
@@ -0,0 +1,124 @@ | |||
1 | /* Lossless_UDP testserver | ||
2 | * A program that waits for a lossless UDP connection and then saves all the data recieved to a file. | ||
3 | * | ||
4 | * Best used in combination with Lossless_UDP_testclient | ||
5 | * | ||
6 | * Compile with: gcc -O2 -Wall -o test ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c | ||
7 | * | ||
8 | * Command line argument is the name of the file to save what we recieve to. | ||
9 | * EX: ./test filename1.txt | ||
10 | */ | ||
11 | |||
12 | #include "../core/network.h" | ||
13 | #include "../core/Lossless_UDP.h" | ||
14 | |||
15 | //Sleep function (x = milliseconds) | ||
16 | #ifdef WIN32 | ||
17 | |||
18 | #define c_sleep(x) Sleep(1*x) | ||
19 | |||
20 | #else | ||
21 | #include <unistd.h> | ||
22 | #include <arpa/inet.h> | ||
23 | #define c_sleep(x) usleep(1000*x) | ||
24 | |||
25 | #endif | ||
26 | |||
27 | |||
28 | #define PORT 33445 | ||
29 | |||
30 | void printpacket(char * data, uint32_t length, IP_Port ip_port) | ||
31 | { | ||
32 | uint32_t i; | ||
33 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | ||
34 | printf("--------------------BEGIN-----------------------------\n"); | ||
35 | for(i = 0; i < length; i++) | ||
36 | { | ||
37 | if(data[i] < 16) | ||
38 | printf("0"); | ||
39 | printf("%hhX",data[i]); | ||
40 | } | ||
41 | printf("\n--------------------END-----------------------------\n\n\n"); | ||
42 | } | ||
43 | |||
44 | |||
45 | int main(int argc, char *argv[]) | ||
46 | { | ||
47 | if (argc < 2) | ||
48 | { | ||
49 | printf("usage: %s filename\n", argv[0]); | ||
50 | exit(0); | ||
51 | } | ||
52 | |||
53 | char buffer[128]; | ||
54 | int read; | ||
55 | |||
56 | FILE *file = fopen(argv[3], "rb"); | ||
57 | if ( file==NULL ){return 1;} | ||
58 | |||
59 | |||
60 | //initialize networking | ||
61 | //bind to ip 0.0.0.0:PORT | ||
62 | IP ip; | ||
63 | ip.i = 0; | ||
64 | init_networking(ip, PORT); | ||
65 | perror("Initialization"); | ||
66 | |||
67 | int connection; | ||
68 | uint64_t timer = current_time(); | ||
69 | |||
70 | IP_Port ip_port; | ||
71 | char data[MAX_UDP_PACKET_SIZE]; | ||
72 | uint32_t length; | ||
73 | |||
74 | while(1) | ||
75 | { | ||
76 | connection = incoming_connection(); | ||
77 | if(connection != -1) | ||
78 | { | ||
79 | if(is_connected(connection) == 1) | ||
80 | { | ||
81 | printf("Recieved the connection."); | ||
82 | } | ||
83 | break; | ||
84 | } | ||
85 | c_sleep(1); | ||
86 | } | ||
87 | |||
88 | timer = current_time(); | ||
89 | |||
90 | while(1) | ||
91 | { | ||
92 | while(recievepacket(&ip_port, data, &length) != -1) | ||
93 | { | ||
94 | if(LosslessUDP_handlepacket(data, length, ip_port)) | ||
95 | { | ||
96 | printpacket(data, length, ip_port); | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | printf("Received handled packet with length: %u\n", length); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | doLossless_UDP(); | ||
105 | if(is_connected(connection) == 1) | ||
106 | { | ||
107 | read = read_packet(connection, buffer); | ||
108 | if(read != 0) | ||
109 | { | ||
110 | if(!fwrite(buffer, read, 1, file)) | ||
111 | { | ||
112 | printf("file write error\n"); | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | printf("Connecting Lost after: %llu us", (unsigned long long)(current_time() - timer)); | ||
119 | } | ||
120 | c_sleep(1); | ||
121 | } | ||
122 | |||
123 | return 0; | ||
124 | } \ No newline at end of file | ||