summaryrefslogtreecommitdiff
path: root/testing/Lossless_UDP_testclient.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/Lossless_UDP_testclient.c')
-rw-r--r--testing/Lossless_UDP_testclient.c130
1 files changed, 130 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
29void 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
44int 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