r2222 - alfs-POC/src

jhuntwork at linuxfromscratch.org jhuntwork at linuxfromscratch.org
Fri Nov 25 21:20:21 PST 2005


Author: jhuntwork
Date: 2005-11-25 22:20:20 -0700 (Fri, 25 Nov 2005)
New Revision: 2222

Modified:
   alfs-POC/src/alfs.c
   alfs-POC/src/alfsd.c
Log:
Some improvemnts to the code - actually consistenly runs commands now

Modified: alfs-POC/src/alfs.c
===================================================================
--- alfs-POC/src/alfs.c	2005-11-26 01:09:17 UTC (rev 2221)
+++ alfs-POC/src/alfs.c	2005-11-26 05:20:20 UTC (rev 2222)
@@ -17,7 +17,7 @@
 	
 	int sockfd, n, len;
 	char recvline [MAXLINE+1];
-	char input[81];
+	char input[256];
 	struct sockaddr_in servaddr;
 	
 	if (argc != 2) {
@@ -51,7 +51,8 @@
 
 	/* Read input from the user to send to the server */
 	printf("Enter a command to send to the server:\n");
-	scanf("%s", input);
+	fgets(input, sizeof(input), stdin);
+	fflush(stdin);
 	len = strlen(input);
 	printf("You entered: %s\n", input);
 

Modified: alfs-POC/src/alfsd.c
===================================================================
--- alfs-POC/src/alfsd.c	2005-11-26 01:09:17 UTC (rev 2221)
+++ alfs-POC/src/alfsd.c	2005-11-26 05:20:20 UTC (rev 2222)
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <netinet/in.h>
+#include <sys/wait.h>
 
 int
 main ()
@@ -41,21 +42,26 @@
 		   three-way handshake*/
 		connfd = accept(listenfd, (SA *) NULL, NULL);
 
-		recv(connfd, buff, sizeof(buff), 0);
 
-		printf("SERVER: Executing \"%s\"\n", buff);
+            if (!fork()) { /* this is the child process */
+                if (recv(connfd, buff, sizeof(buff), 0) == -1)
+                    perror("recv");
 
-		system(buff);
+		printf("SERVER: Executing %s\n", buff);
 
-		/* fill buffer with data to send to client */		
-		snprintf(buff, sizeof(buff), "CLIENT: Command successful!\r\n");
+		if (system(buff) == -1)
+		    perror("system");
 
-		/* send buffer to client */
+		snprintf(buff, 29, "CLIENT: Command successful!\n");
 		write(connfd, buff, strlen(buff));
+		close(connfd);
+                exit(0);
+            }
 
-		/* close connection, restart while loop, goes back to sleep
-		  after accept	*/
-		close(connfd);
+	    close(connfd);
+
+            while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
+
 	}
 }
 




More information about the alfs-log mailing list