emu48-mirror/Sources/Emu48/UDP.C
Gwenhael Le Moine 625fd3f663
2014-10-18: Updated to version 1.56
Signed-off-by: Gwenhael Le Moine <gwenhael.le.moine@gmail.com>
2024-03-19 23:35:30 +01:00

79 lines
1.8 KiB
C

/*
* udp.c
*
* This file is part of Emu48
*
* Copyright (C) 2011 Christoph Gießelink
*
*/
#include "pch.h"
#include "Emu48.h"
TCHAR szUdpServer[1024] = _T("localhost");
WORD wUdpPort = 5025; // scpi-raw
static IN_ADDR ip_addr = { 255, 255, 255, 255 };
VOID ResetUdp(VOID)
{
ip_addr.s_addr = INADDR_NONE; // invalidate saved UDP address
return;
}
BOOL SendByteUdp(BYTE byData)
{
WSADATA wsd;
SOCKET sClient;
SOCKADDR_IN sServer;
BOOL bErr = TRUE;
VERIFY(WSAStartup(MAKEWORD(1,1),&wsd) == 0);
if (ip_addr.s_addr == INADDR_NONE) // IP address not specified
{
LPSTR lpszIpAddr;
#if defined _UNICODE
DWORD dwLength = lstrlen(szUdpServer) + 1;
if ((lpszIpAddr = (LPSTR) _alloca(dwLength)) == NULL)
return TRUE; // server not found
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
szUdpServer, dwLength,
lpszIpAddr, dwLength, NULL, NULL);
#else
lpszIpAddr = szUdpServer;
#endif
ip_addr.s_addr = inet_addr(lpszIpAddr);
// not a valid ip address -> try to get ip address from name server
if (ip_addr.s_addr == INADDR_NONE)
{
PHOSTENT host = gethostbyname(lpszIpAddr);
if (host == NULL)
{
return TRUE; // server not found
}
ip_addr.s_addr = ((PIN_ADDR) host->h_addr_list[0])->s_addr;
}
}
// create UDP socket
if ((sClient = socket(AF_INET, SOCK_DGRAM, 0)) != INVALID_SOCKET)
{
sServer.sin_family = AF_INET;
sServer.sin_port = htons(wUdpPort);
sServer.sin_addr.s_addr = ip_addr.s_addr;
// transmit data byte
bErr = sendto(sClient, (LPCCH) &byData, sizeof(byData), 0, (LPSOCKADDR) &sServer, sizeof(sServer)) == SOCKET_ERROR;
closesocket(sClient);
}
WSACleanup(); // cleanup network stack
return bErr;
}