#include <WS2tcpip.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define RECEIVER_IP "127.0.0.1"
#define PORT 7788
#define PACKET_LENGTH 48
// don't have Marshalling, this struct not include array pointer.
struct SendInfo
{
public:
int One;
int Two;
double Three;
double Four;
double Five;
double Heading;
int MissionStatus;
};
int main()
{
SendInfo sendinfo;
sendinfo.One = 10;
sendinfo.Two = 20;
sendinfo.Three = 30.33;
sendinfo.Four = 40.44;
sendinfo.Five = 50.55;
sendinfo.Six = 60.66;
sendinfo.Seven = 66;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN addr;
ZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
inet_pton(AF_INET, RECEIVER_IP, &(addr.sin_addr.s_addr));
char buf[48];
memset(&buf, '\0', 48);
*(int*)(buf + 0) = sendinfo.One;
*(int*)(buf + 4) = sendinfo.Two;
*(double*)(buf + 8) = sendinfo.Three;
*(double*)(buf + 16) = sendinfo.Four;
*(double*)(buf + 24) = sendinfo.Five;
*(double*)(buf + 32) = sendinfo.Six;
*(int*)(buf + 40) = sendinfo.Seven;
sendto(sock, (char*)&sendinfo, sizeof(SendInfo), 0, (SOCKADDR*)&addr, sizeof(addr));
closesocket(sock);
WSACleanup();
}
2022-06-12 수정.
가장 기본적인 예제이므로 필요하다면 내 Git 저장소 내의 cyp::communication::udp 클래스를 확인하면 좋을듯 하다.
'Programming > C++' 카테고리의 다른 글
[C++] 다형성 vitual 키워드에 관한 팁 (0) | 2021.06.09 |
---|---|
[C++] openSSL Hash 사용법 (0) | 2021.05.12 |
[C++] OpenCV digit_svm 예제 간단 분석 (0) | 2021.03.31 |
[C++] TCP 통신 (0) | 2020.12.20 |
[C++] 람다식 mutable 의 쓰임세. (0) | 2020.12.07 |