-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathudp-recv.c.new
executable file
·137 lines (114 loc) · 4.29 KB
/
udp-recv.c.new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifdef _WIN32
//For Windows
int betriebssystem = 1;
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <ws2def.h>
#pragma comment(lib, "Ws2_32.lib")
#include <windows.h>
#include <io.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
using namespace std;
int betriebssystem = 2;
#endif
#define BUFSIZE 65536 // Set buffer size to 64 KB for application-level receive buffer
#define SERVICE_PORT 48879
ofstream myfile("test.dat");
bool keepRunning = true;
void handleSigint(int sig) {
cout << "\nClosing file and exiting...\n";
if (myfile.is_open()) {
myfile.close();
}
exit(0); // Exit the program
}
int main(int argc, char **argv)
{
signal(SIGINT, handleSigint); // Register signal handler
struct sockaddr_in myaddr; /* our address */
struct sockaddr_in remaddr; /* remote address */
socklen_t addrlen = sizeof(remaddr); /* length of addresses */
int recvlen; /* # bytes received */
int fd; /* our socket */
unsigned char buf[BUFSIZE]; /* receive buffer */
int event = 0;
int subevent = 0;
int counter = 0;
string line;
/* create a UDP socket */
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n");
return 0;
}
/* Set socket options for larger kernel receive buffer */
int bufferSize = 10485760; // Set buffer size to 10 MB
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) < 0) {
perror("setsockopt(SO_RCVBUF) failed");
return 0;
}
/* Set socket reuse option to avoid address already in use issues */
int optval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
perror("setsockopt(SO_REUSEADDR) failed");
return 0;
}
/* bind the socket to a specific IP address and port */
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = inet_addr("192.168.40.2"); // Replace with the IP address of eth1
myaddr.sin_port = htons(SERVICE_PORT);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
return 0;
}
printf("Bind completed successfully, waiting on port %d\n", SERVICE_PORT);
fflush(stdout); // Ensure output is immediately printed
/* now loop, receiving data and printing what we received */
while (keepRunning) {
recvlen = recvfrom(fd, reinterpret_cast<char *>(buf), BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
if (recvlen < 0) {
perror("recvfrom failed");
continue;
}
for (int i = 0; i < recvlen; i++) {
myfile << buf[i];
}
// Get the current position of the put pointer
streamoff fileSize = myfile.tellp();
// Convert the size from bytes to megabytes
double fileSizeInMB = fileSize / 1048576.0;
if (recvlen > 0) {
counter++;
buf[recvlen] = 0;
printf("%.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x [ ... ] "
"%.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x ",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5], buf[6], buf[7],
buf[recvlen - 8], buf[recvlen - 7], buf[recvlen - 6], buf[recvlen - 5],
buf[recvlen - 4], buf[recvlen - 3], buf[recvlen - 2], buf[recvlen - 1]);
if (buf[0] == 0xff && buf[1] == 0xff && buf[2] == 0xff && buf[3] == 0xff) {
event++;
subevent = buf[16] & 0xff;
} else if (buf[0] == 0x00 && buf[1] == 0xff && buf[2] == 0xff && buf[3] == 0xff) {
subevent = buf[12] & 0xff;
}
printf("[%d] ev.subev: %d.%d (file: %.2f MB)\n", counter, event, subevent,
fileSizeInMB);
fflush(stdout); // Ensure output is immediately printed
}
}
/* never exits */
}