Commit 82aeff99 authored by Bruce Cowan's avatar Bruce Cowan

Add Windows version of previous

parent 7f8af790
Pipeline #2334 failed with stages
in 1 minute and 27 seconds
......@@ -7,12 +7,17 @@ glib_dep = dependency('glib-2.0', version: '>= 2.16', required: false)
openmp_dep = dependency('openmp', required: false)
thread_dep = dependency('threads', required: false)
incdir = include_directories('lib')
if host_machine.system() == 'windows'
cc = meson.get_compiler('c')
socket_deps = [cc.find_library('ws2_32')]
else
socket_deps = []
endif
subdir('lib')
executable('add', 'add.c')
executable('add2', 'add2.c', link_with: lib_utils, include_directories: incdir)
executable('add2', 'add2.c', link_with: lib_utils, include_directories: 'lib')
executable('angle', 'angle.c', dependencies: libm)
executable('binary', 'binary.c')
executable('complex', 'complex.c', dependencies: libm)
......@@ -34,6 +39,7 @@ executable('world', 'world.c')
subdir('array')
subdir('hashtable')
subdir('list')
subdir('sockets')
subdir('winter')
if openmp_dep.found()
......
executable('udp-server', 'udp-server.c')
executable('thread-incr', 'thread-incr.c',
dependencies: thread_dep,
link_with: lib_utils,
include_directories: incdir)
include_directories: '../lib')
if host_machine.system() == 'windows'
udp_server_src = ['udp-server-win.c']
else
udp_server_src = ['udp-server.c']
endif
executable('udp-server', udp_server_src, dependencies: socket_deps)
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#define PORT 46123
#define BUF_LENGTH 1024
static SOCKET sock;
static void
handle_error (const char *funcname)
{
fprintf (stderr, "%s failed: %d\n", funcname, WSAGetLastError ());
closesocket (sock);
exit (EXIT_FAILURE);
}
int
main (void)
{
WSADATA wsa;
int result = WSAStartup (MAKEWORD (2, 2), &wsa);
if (result != 0)
{
fprintf (stderr, "WSAStartup: %d\n", result);
exit (EXIT_FAILURE);
}
if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
handle_error ("socket");
SOCKADDR_IN addr;
memset (&addr, 0, sizeof (SOCKADDR_IN));
addr.sin_family = AF_INET;
addr.sin_port = htons (PORT);
addr.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (sock, (SOCKADDR *) &addr, sizeof (addr)) == SOCKET_ERROR)
handle_error ("bind");
while (true)
{
char buf[BUF_LENGTH];
struct sockaddr_in src_addr;
int addrlen = sizeof (SOCKADDR_IN);
char str[INET_ADDRSTRLEN];
int bytes = recvfrom (sock, buf, BUF_LENGTH, 0,
(SOCKADDR *) &src_addr, &addrlen);
if (bytes == SOCKET_ERROR)
handle_error ("recvfrom");
if (InetNtop (AF_INET, &src_addr.sin_addr, str, INET_ADDRSTRLEN) == NULL)
{
fprintf (stderr, "Could not convert address to string\n");
exit (EXIT_FAILURE);
}
printf ("Received %d bytes from %s\n", bytes, str);
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment