Actually include rdpsrv.cpp as well :-)
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 3 Feb 2005 22:24:12 +0000 (22:24 +0000)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 3 Feb 2005 22:24:12 +0000 (22:24 +0000)
rdpsrv.cpp [new file with mode: 0644]

diff --git a/rdpsrv.cpp b/rdpsrv.cpp
new file mode 100644 (file)
index 0000000..ea4892f
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
+
+static const int RDP_PORT = 3389;
+int create_server_socket();
+
+int main()
+{
+       int server_sock = create_server_socket();
+       for ( ;; ) {
+               sockaddr_in sin;
+               socklen_t len;
+               int sock = accept(server_sock, (sockaddr *)&sin, &len);
+       
+               if (sock == -1) {
+                       perror("accept()");
+                       exit(1);
+               }
+
+               printf("Got socket.\n");
+
+               close(sock);
+       }
+}
+
+int create_server_socket()
+{
+       int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+       const unsigned int one = 1, zero = 0;
+       struct sockaddr_in addr;
+       int err;
+
+       setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
+       ioctl(server_sock, FIONBIO, &zero);
+
+       addr.sin_family = AF_INET;
+       addr.sin_addr.s_addr = INADDR_ANY;
+       addr.sin_port = htons(RDP_PORT);
+
+       do {
+               err = bind(server_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
+
+               if (err == -1) {
+                       perror("bind()");
+
+                       /* try to recover from recoverable errors... */
+                       if (errno == ENOMEM || errno == EADDRINUSE) {
+                               puts("Waiting 1 sec before trying again...");
+                               sleep(1);
+                       } else {
+                               puts("Giving up.");
+                               exit(1);
+                       }
+               }
+       } while (err == -1);
+
+       listen(server_sock, 20);
+       return server_sock;
+}
+