From e62c0275ff2cc06f4b1076c62555c8b98fb9f3e0 Mon Sep 17 00:00:00 2001 From: curt Date: Sat, 15 May 1999 01:08:00 +0000 Subject: [PATCH] Initial revision. --- Tools/Construct/Parallel/Makefile.am | 17 ++++ Tools/Construct/Parallel/client.cxx | 98 +++++++++++++++++++++++ Tools/Construct/Parallel/server.cxx | 114 +++++++++++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 Tools/Construct/Parallel/Makefile.am create mode 100644 Tools/Construct/Parallel/client.cxx create mode 100644 Tools/Construct/Parallel/server.cxx diff --git a/Tools/Construct/Parallel/Makefile.am b/Tools/Construct/Parallel/Makefile.am new file mode 100644 index 000000000..5f617bec5 --- /dev/null +++ b/Tools/Construct/Parallel/Makefile.am @@ -0,0 +1,17 @@ +bin_PROGRAMS = server client + +server_SOURCES = server.cxx + +server_LDADD = + +client_SOURCES = client.cxx + +client_LDADD = \ + $(top_builddir)/Lib/Bucket/libBucket.a \ + $(top_builddir)/Lib/Misc/libMisc.a + +INCLUDES += \ + -I$(top_builddir) \ + -I$(top_builddir)/Lib \ + -I$(top_builddir)/Tools/Lib \ + -I$(top_builddir)/Tools/Construct diff --git a/Tools/Construct/Parallel/client.cxx b/Tools/Construct/Parallel/client.cxx new file mode 100644 index 000000000..2f262fd7e --- /dev/null +++ b/Tools/Construct/Parallel/client.cxx @@ -0,0 +1,98 @@ +/* remote_exec.c -- Written by Curtis Olson */ +/* -- for CSci 5502 */ + + +#include +#include +#include +#include +#include +#include + +#include +#include // atoi() +#include // bcopy() + +#include "remote_exec.h" + +char *determine_port(char *host); + + +int make_socket (char *host, unsigned short int port) { + int sock; + struct sockaddr_in name; + struct hostent *hp; + + // Create the socket. + sock = socket (PF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror ("socket"); + exit (EXIT_FAILURE); + } + + // specify address family + name.sin_family = AF_INET; + + // get the hosts official name/info + hp = gethostbyname(host); + + // Connect this socket to the host and the port specified on the + // command line + bcopy(hp->h_addr, &(name.sin_addr.s_addr), hp->h_length); + name.sin_port = htons(port); + + if ( connect(sock, (struct sockaddr *) &name, + sizeof(struct sockaddr_in)) < 0 ) + { + close(sock); + perror("Cannot connect to stream socket"); + exit(-1); + } + + return sock; +} + + +main(int argc, char *argv[]) { + int sock, len; + fd_set ready; + int port; + char message[256]; + + /* Check usage */ + if ( argc < 3 ) { + printf("Usage: %s remote_machine port\n", argv[0]); + exit(1); + } + + port = atoi(argv[2]); + sock = make_socket(argv[1], port); + + /* build a command string from the argv[]'s */ + strcpy(message, "hello world!\n"); + + /* send command and arguments to remote server */ + if ( write(sock, message, sizeof(message)) < 0 ) { + perror("Cannot write to stream socket"); + } + + for ( ;; ) { + /* loop until remote program finishes */ + + FD_ZERO(&ready); + FD_SET(sock, &ready); + + /* block until input from sock or stdin */ + select(32, &ready, 0, 0, NULL); + + if ( FD_ISSET(sock, &ready) ) { + /* input coming from socket */ + if ( (len = read(sock, message, 1024)) > 0 ) { + write(1, message, len); + } else { + exit(0); + } + } + } + close(sock); +} diff --git a/Tools/Construct/Parallel/server.cxx b/Tools/Construct/Parallel/server.cxx new file mode 100644 index 000000000..251dd5440 --- /dev/null +++ b/Tools/Construct/Parallel/server.cxx @@ -0,0 +1,114 @@ +// remote_server.c -- Written by Curtis Olson +// -- for CSci 5502 + + +#include +#include + +#include // bind +#include + +#include + +#include + +// #include +// #include +// #include + + +#define MAXBUF 1024 + +int make_socket (unsigned short int* port) { + int sock; + struct sockaddr_in name; + socklen_t length; + + // 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_addr.s_addr = INADDR_ANY; + name.sin_port = 0 /* htons (port) */; + name.sin_addr.s_addr = htonl (INADDR_ANY); + if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) { + perror ("bind"); + exit (EXIT_FAILURE); + } + + // Find the assigned port number + length = sizeof(struct sockaddr_in); + if ( getsockname(sock, (struct sockaddr *) &name, &length) ) { + perror("Cannot get socket's port number"); + } + *port = ntohs(name.sin_port); + + return sock; +} + + +main() { + int sock, msgsock, length, pid; + fd_set ready; + short unsigned int port; + char buf[MAXBUF]; + + sock = make_socket( &port ); + + // Save the port number + // set_port( port ); + cout << "socket is connected to port = " << port << endl; + + /* Specify the maximum length of the connection queue */ + listen(sock, 3); + + for ( ;; ) { + FD_ZERO(&ready); + FD_SET(sock, &ready); + + /* block until we get some input on sock */ + select(32, &ready, 0, 0, NULL); + + if ( FD_ISSET(sock, &ready) ) { + /* printf("%d %d Incomming message --> ", getpid(), pid); */ + + msgsock = accept(sock, 0, 0); + + /* spawn a child */ + pid = fork(); + + if ( pid < 0 ) { + /* error */ + perror("Cannot fork child process"); + exit(-1); + } else if ( pid > 0 ) { + /* This is the parent */ + close(msgsock); + } else { + /* This is the child */ + + cout << "new process started to handle new connection" << endl; + // Read client's message + while ( (length = read(msgsock, buf, MAXBUF)) > 0) { + cout << "buffer length = " << length << endl; + buf[length] = '\0'; + cout << "Incoming command -> " << buf; + + // reply to the client + if ( write(sock, message, sizeof(message)) < 0 ) { + perror("Cannot write to stream socket"); + } + + + } + cout << "process ended" << endl; + exit(0); + } + } + } +} -- 2.39.5