summaryrefslogtreecommitdiffstats
path: root/libvncclient/listen.c
diff options
context:
space:
mode:
authordscho <dscho>2003-07-27 21:51:57 +0000
committerdscho <dscho>2003-07-27 21:51:57 +0000
commit0fc57f2054f9e1b7d2efbe202fdaa4b8e39556f3 (patch)
tree6307f1bccd9937435ec3f05900d5ce8e412da169 /libvncclient/listen.c
parenta1ce2ac48f158d18c3e25a6131ab28f8f72cdf33 (diff)
downloadlibtdevnc-0fc57f2054f9e1b7d2efbe202fdaa4b8e39556f3.tar.gz
libtdevnc-0fc57f2054f9e1b7d2efbe202fdaa4b8e39556f3.zip
first alpha version of libvncclient
Diffstat (limited to 'libvncclient/listen.c')
-rw-r--r--libvncclient/listen.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/libvncclient/listen.c b/libvncclient/listen.c
new file mode 100644
index 0000000..c11aef7
--- /dev/null
+++ b/libvncclient/listen.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this software; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+/*
+ * listen.c - listen for incoming connections
+ */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <sys/utsname.h>
+#include <rfb/rfbclient.h>
+
+/*
+ * listenForIncomingConnections() - listen for incoming connections from
+ * servers, and fork a new process to deal with each connection.
+ */
+
+void
+listenForIncomingConnections(rfbClient* client)
+{
+ int listenSocket;
+ fd_set fds;
+
+ client->listenSpecified = TRUE;
+
+ listenSocket = ListenAtTcpPort(client->listenPort);
+
+ if ((listenSocket < 0)) exit(1);
+
+ fprintf(stderr,"%s -listen: Listening on port %d\n",
+ client->programName,client->listenPort);
+ fprintf(stderr,"%s -listen: Command line errors are not reported until "
+ "a connection comes in.\n", client->programName);
+
+ while (TRUE) {
+
+ /* reap any zombies */
+ int status, pid;
+ while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
+
+ /* TODO: callback for discard any events (like X11 events) */
+
+ FD_ZERO(&fds);
+
+ FD_SET(listenSocket, &fds);
+
+ select(FD_SETSIZE, &fds, NULL, NULL, NULL);
+
+ if (FD_ISSET(listenSocket, &fds)) {
+ client->sock = AcceptTcpConnection(listenSocket);
+ if (client->sock < 0) exit(1);
+ if (!SetNonBlocking(client->sock)) exit(1);
+
+ /* Now fork off a new process to deal with it... */
+
+ switch (fork()) {
+
+ case -1:
+ perror("fork");
+ exit(1);
+
+ case 0:
+ /* child - return to caller */
+ close(listenSocket);
+ return;
+
+ default:
+ /* parent - go round and listen again */
+ close(client->sock);
+ break;
+ }
+ }
+ }
+}
+
+