Skip to content

Instantly share code, notes, and snippets.

@ratmcu
Last active January 23, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratmcu/16aa0cafe3058439e4946ae7b9c65680 to your computer and use it in GitHub Desktop.
Save ratmcu/16aa0cafe3058439e4946ae7b9c65680 to your computer and use it in GitHub Desktop.
native windows winsock UDP cpp server and python client and
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <sstream>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 1024
#define DEFAULT_PORT "27014"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
// hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
int count = 0;
std::stringstream buffer;
ZeroMemory(recvbuf, sizeof(recvbuf));
while (true)
{
buffer.str("");
buffer << "hello world " << count << '\n';
std::cout << buffer.str();
count++;
memcpy(recvbuf, buffer.str().c_str(), buffer.str().length());
int i = sendto(ListenSocket, recvbuf, 1024, 0, result->ai_addr, result->ai_addrlen);
if (i == -1)
std::cout << "Error sending the packet\n";
Sleep(100);
}
WSACleanup();
return 0;
}
import socket
import sys
PORT = 27014 # The same port as used by the server
s = None
s = socket.socket(2, 2, 0)
s.bind(("127.0.0.1", PORT))
print('opened socket')
# s.settimeout(3.0)
while True:
try:
data = s.recv(1024)
print('Received', str(data.decode("utf-8", errors='ignore')).split('\0')[0])
except KeyboardInterrupt:
sys.exit(1)
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment