-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_sender.cpp
65 lines (54 loc) · 1.77 KB
/
struct_sender.cpp
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
#include <iostream>
#include <string>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
// Define your struct
struct MyStruct {
int id;
std::string name;
double value;
};
// Function to send struct over UDP
bool sendStructOverUDP(const MyStruct& obj, const std::string& ipAddress, int port) {
// Create a UDP socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
std::cerr << "Failed to create socket" << std::endl;
return false;
}
// Prepare the server address
struct sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(port);
if (inet_pton(AF_INET, ipAddress.c_str(), &(serverAddress.sin_addr)) <= 0) {
std::cerr << "Invalid IP address" << std::endl;
close(sockfd);
return false;
}
// Convert struct to a buffer
char buffer[sizeof(MyStruct)];
std::memcpy(buffer, &obj, sizeof(MyStruct));
// Send the buffer over UDP
ssize_t bytesSent = sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
if (bytesSent < 0) {
std::cerr << "Failed to send data" << std::endl;
close(sockfd);
return false;
}
std::cout << "Struct sent successfully" << std::endl;
// Close the socket
close(sockfd);
return true;
}
int main() {
MyStruct myStruct { 1, "Example", 3.14 };
std::string ipAddress = "127.0.0.1"; // IP address of the destination
int port = 1234; // Port number
if (sendStructOverUDP(myStruct, ipAddress, port)) {
std::cout << "Struct sent over UDP successfully" << std::endl;
} else {
std::cerr << "Failed to send struct over UDP" << std::endl;
}
return 0;
}