r2223 - alfs-POC/src

jhuntwork at linuxfromscratch.org jhuntwork at linuxfromscratch.org
Sat Nov 26 09:37:45 PST 2005


Author: jhuntwork
Date: 2005-11-26 10:37:44 -0700 (Sat, 26 Nov 2005)
New Revision: 2223

Modified:
   alfs-POC/src/alfs.c
   alfs-POC/src/alfsd.c
Log:
Some more error checking

Modified: alfs-POC/src/alfs.c
===================================================================
--- alfs-POC/src/alfs.c	2005-11-26 05:20:20 UTC (rev 2222)
+++ alfs-POC/src/alfs.c	2005-11-26 17:37:44 UTC (rev 2223)
@@ -14,7 +14,8 @@
 				      later in the connect() function
 				      and prevents that line from wrapping
 				   */
-	
+	#define PORT 1234
+		
 	int sockfd, n, len;
 	char recvline [MAXLINE+1];
 	char input[256];
@@ -35,7 +36,7 @@
 	servaddr.sin_family = AF_INET;
 
 	/* Use port 1234 */
-	servaddr.sin_port = htons(1234);
+	servaddr.sin_port = htons(PORT);
 
 	/* convert provided IP address into numeric */
 	inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

Modified: alfs-POC/src/alfsd.c
===================================================================
--- alfs-POC/src/alfsd.c	2005-11-26 05:20:20 UTC (rev 2222)
+++ alfs-POC/src/alfsd.c	2005-11-26 17:37:44 UTC (rev 2223)
@@ -11,9 +11,10 @@
 
 	#define MAXLINE 4096
 	#define LISTENQ 5
+	#define PORT 1234
 	#define SA struct sockaddr
 
-	int listenfd, connfd;
+	int listenfd, connfd, syscmd;
 	struct sockaddr_in servaddr;
 	char buff[MAXLINE];
 	
@@ -26,9 +27,9 @@
 	/* set to IPv4 */
 	servaddr.sin_family = AF_INET;
 
-	/* listen on any IP address port 1234 */
+	/* listen on any IP address port PORT */
 	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-	servaddr.sin_port = htons(1234);
+	servaddr.sin_port = htons(PORT);
 
 	/* bind to socket and listen for incoming requests */	
 	bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
@@ -40,19 +41,33 @@
 		/* process goes to sleep after 'accept' until a client
 		   connection arrives and is accepted after TCP's
 		   three-way handshake*/
-		connfd = accept(listenfd, (SA *) NULL, NULL);
+	    connfd = accept(listenfd, (SA *) NULL, NULL);
 
 
             if (!fork()) { /* this is the child process */
+
+		/* read a string from the socket into a buffer */
                 if (recv(connfd, buff, sizeof(buff), 0) == -1)
                     perror("recv");
 
 		printf("SERVER: Executing %s\n", buff);
 
-		if (system(buff) == -1)
-		    perror("system");
+		/* execute the string as a system command */
+		syscmd = system(buff);
 
-		snprintf(buff, 29, "CLIENT: Command successful!\n");
+		switch (syscmd)
+		{
+		  case -1:
+			perror("system");
+		  case 0:
+		  	snprintf(buff, 29, "CLIENT: Command successful!\n");
+		  case 127:
+		  case 32512:
+		    {
+		    	snprintf(buff, 29, "CLIENT: Command failed!\n");
+		    }
+		}
+		printf("Exit status of command was: %d\n", syscmd);
 		write(connfd, buff, strlen(buff));
 		close(connfd);
                 exit(0);




More information about the alfs-log mailing list