aboutsummaryrefslogtreecommitdiff
path: root/demo/stream/platform/windows/server.c
diff options
context:
space:
mode:
authorHugo Lindström <hugolm84@gmail.com>2020-07-09 12:59:25 +0200
committerGarrett D'Amore <garrett@damore.org>2024-01-01 21:00:11 -0800
commitb989bedf8051b8ce3c27c4385d56239b21764700 (patch)
treef2a6b77e44d2fda3c25e60aec28f3eea5ed64025 /demo/stream/platform/windows/server.c
parentad2d7eae574badf67c0778a5ae395d895ad07584 (diff)
downloadnng-b989bedf8051b8ce3c27c4385d56239b21764700.tar.gz
nng-b989bedf8051b8ce3c27c4385d56239b21764700.tar.bz2
nng-b989bedf8051b8ce3c27c4385d56239b21764700.zip
Add a nng_stream example paired with a socket server on win/linux
Diffstat (limited to 'demo/stream/platform/windows/server.c')
-rw-r--r--demo/stream/platform/windows/server.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/demo/stream/platform/windows/server.c b/demo/stream/platform/windows/server.c
new file mode 100644
index 00000000..09603ebc
--- /dev/null
+++ b/demo/stream/platform/windows/server.c
@@ -0,0 +1,87 @@
+// Copyright 2020 Hugo Lindström <hugolm84@gmail.com>
+
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <winsock2.h>
+
+void
+wsa_fatal(const char *func)
+{
+ fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
+ exit(1);
+}
+
+int
+server(int portno)
+{
+ WSADATA wsa;
+ SOCKET s, new_socket;
+ struct sockaddr_in server, client;
+ int c;
+ char * message;
+
+ printf("Initialising Winsock...\n");
+
+ if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
+ wsa_fatal("Failed to call WSAStartup");
+ }
+
+ printf("Initialised WSA.\n");
+
+ // Create a socket
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
+ wsa_fatal("Could not create socket");
+ }
+
+ printf("Socket created.\n");
+
+ // Prepare the sockaddr_in structure
+ server.sin_family = AF_INET;
+ server.sin_addr.s_addr = INADDR_ANY;
+ server.sin_port = htons(portno);
+
+ // Bind
+ if (bind(s, (struct sockaddr *) &server, sizeof(server)) ==
+ SOCKET_ERROR) {
+ wsa_fatal("Bind failed");
+ }
+
+ printf("Bind done\n");
+
+ // Listen to incoming connections
+ listen(s, 3);
+
+ // Accept and incoming connection
+ printf("Waiting for incoming connections...\n");
+
+ c = sizeof(struct sockaddr_in);
+
+ while ((new_socket = accept(s, (struct sockaddr *) &client, &c)) !=
+ INVALID_SOCKET) {
+ printf("Connection accepted\n");
+ // Reply to the client
+ message = "Hello Client!";
+ if (send(new_socket, message, (int) strlen(message), 0) ==
+ SOCKET_ERROR) {
+ wsa_fatal("Failed to send message to client!");
+ }
+ }
+
+ if (new_socket == INVALID_SOCKET) {
+ wsa_fatal("accept failed");
+ }
+
+ if (closesocket(s) == SOCKET_ERROR) {
+ wsa_fatal("Failed to close socket");
+ }
+
+ if (WSACleanup() == SOCKET_ERROR) {
+ wsa_fatal("Failed to WSACleanup");
+ }
+ return 0;
+}