diff options
| author | Hugo Lindström <hugolm84@gmail.com> | 2020-07-09 12:59:25 +0200 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-01-01 21:00:11 -0800 |
| commit | b989bedf8051b8ce3c27c4385d56239b21764700 (patch) | |
| tree | f2a6b77e44d2fda3c25e60aec28f3eea5ed64025 /demo/stream/platform | |
| parent | ad2d7eae574badf67c0778a5ae395d895ad07584 (diff) | |
| download | nng-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')
| -rw-r--r-- | demo/stream/platform/posix/server.c | 53 | ||||
| -rw-r--r-- | demo/stream/platform/windows/server.c | 87 |
2 files changed, 140 insertions, 0 deletions
diff --git a/demo/stream/platform/posix/server.c b/demo/stream/platform/posix/server.c new file mode 100644 index 00000000..1b238f17 --- /dev/null +++ b/demo/stream/platform/posix/server.c @@ -0,0 +1,53 @@ +// 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 <netinet/in.h> +#include <stdio.h> +#include <stdlib.h> +#include <strings.h> +#include <sys/socket.h> +#include <unistd.h> + +void +error(const char *msg) +{ + perror(msg); + exit(1); +} + +int +server(int portno) +{ + int sockfd, newsockfd; + socklen_t clilen; + struct sockaddr_in serv_addr, cli_addr; + int n; + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + error("ERROR opening socket"); + } + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < + 0) { + error("ERROR on binding"); + } + listen(sockfd, 5); + clilen = sizeof(cli_addr); + newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); + if (newsockfd < 0) { + error("ERROR on accept"); + } + n = write(newsockfd, "Hello Client!", 13); + if (n < 0) + error("ERROR writing to socket"); + close(newsockfd); + close(sockfd); + return 0; +} 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; +} |
