개발자 모드/통신
C++ UDP Client Source Code
인생은직구
2021. 3. 14. 21:59
728x90
// Client Test.cpp : 기본 프로젝트 파일입니다.
#include "stdafx.h"
#include <stdio.h>
#include <winsock.h>
#pragma comment( lib, "ws2_32.lib" )
using namespace System;
int main(array<System::String ^> ^args)
{
WSAData wsa;
SOCKADDR_IN sockAddr;
SOCKET sock;
try
{
if( WSAStartup( MAKEWORD( 2, 2 ), &wsa ) != 0 )
{
throw WSAGetLastError();
}
sock = socket( AF_INET, SOCK_DGRAM, 0 );
if( sock == INVALID_SOCKET )
{
throw WSAGetLastError();
}
memset( ( void * )&sockAddr, 0, sizeof( SOCKADDR_IN ) );
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons( 3001 ); // PORT는 Server와 동일하게 통일
sockAddr.sin_addr.s_addr = inet_addr( "192.168.2.11" ); // Source 장비의 IP를 입력
// 데이터 보내기
sendto( sock, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", strlen( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, ( SOCKADDR * )&sockAddr, sizeof( SOCKADDR_IN ) );
Sleep(1000);
}
catch( int ex )
{
printf( "Error Code: %d\n", ex );
}
closesocket( sock );
WSACleanup();
return 0;
}
728x90