summaryrefslogtreecommitdiff
path: root/testing/Lossless_UDP_testserver.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/Lossless_UDP_testserver.c')
-rw-r--r--testing/Lossless_UDP_testserver.c124
1 files changed, 124 insertions, 0 deletions
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
30void 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
45int 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