summaryrefslogtreecommitdiff
path: root/libc/manual/examples/mkisock.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc/manual/examples/mkisock.c')
-rw-r--r--libc/manual/examples/mkisock.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/libc/manual/examples/mkisock.c b/libc/manual/examples/mkisock.c
new file mode 100644
index 000000000..2fd8b3d2f
--- /dev/null
+++ b/libc/manual/examples/mkisock.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+int
+make_socket (uint16_t port)
+{
+ int sock;
+ struct sockaddr_in name;
+
+ /* Create the socket. */
+ sock = socket (PF_INET, SOCK_STREAM, 0);
+ if (sock < 0)
+ {
+ perror ("socket");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Give the socket a name. */
+ name.sin_family = AF_INET;
+ name.sin_port = htons (port);
+ name.sin_addr.s_addr = htonl (INADDR_ANY);
+ if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
+ {
+ perror ("bind");
+ exit (EXIT_FAILURE);
+ }
+
+ return sock;
+}