r2219 - alfs-POC/client-server

jhuntwork at linuxfromscratch.org jhuntwork at linuxfromscratch.org
Fri Nov 25 16:53:56 PST 2005


Author: jhuntwork
Date: 2005-11-25 17:53:54 -0700 (Fri, 25 Nov 2005)
New Revision: 2219

Modified:
   alfs-POC/client-server/client.c
   alfs-POC/client-server/server.c
Log:
Initial tests with sending commands to the server

Modified: alfs-POC/client-server/client.c
===================================================================
--- alfs-POC/client-server/client.c	2005-11-25 21:41:25 UTC (rev 2218)
+++ alfs-POC/client-server/client.c	2005-11-26 00:53:54 UTC (rev 2219)
@@ -15,8 +15,9 @@
 				      and prevents that line from wrapping
 				   */
 	
-	int sockfd, n;
+	int sockfd, n, len;
 	char recvline [MAXLINE+1];
+	char input[81];
 	struct sockaddr_in servaddr;
 	
 	if (argc != 2) {
@@ -39,12 +40,26 @@
 	/* convert provided IP address into numeric */
 	inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
 
-
 	/* connect to socket descriptor to port set above*/
-	connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
+	if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == -1)
+	{
+		printf("Connection failed!\n");
+		exit(1);
+	} else {
+		printf("Connected to %s\n", argv[1]);
+	}
 
+	/* Read input from the user to send to the server */
+	printf("Enter a command to send to the server:\n");
+	scanf("%s", input);
+	len = strlen(input);
+	printf("You entered: %s\n", input);
+
+	/* Send the input to the server */
+	send(sockfd, input, len, 0);
+
 	/* read data from server */	
-	n = read(sockfd, recvline, MAXLINE)
+	n = read(sockfd, recvline, MAXLINE);
 	recvline[n] = 0;
 
 	if (fputs(recvline, stdout) == EOF)

Modified: alfs-POC/client-server/server.c
===================================================================
--- alfs-POC/client-server/server.c	2005-11-25 21:41:25 UTC (rev 2218)
+++ alfs-POC/client-server/server.c	2005-11-26 00:53:54 UTC (rev 2219)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <netinet/in.h>
@@ -40,8 +41,14 @@
 		   three-way handshake*/
 		connfd = accept(listenfd, (SA *) NULL, NULL);
 
+		recv(connfd, buff, sizeof(buff), 0);
+
+		printf("SERVER: Executing \"%s\"\n", buff);
+
+		system(buff);
+
 		/* fill buffer with data to send to client */		
-		snprintf(buff, sizeof(buff), "This is a test\r\n");
+		snprintf(buff, sizeof(buff), "CLIENT: Command successful!\r\n");
 
 		/* send buffer to client */
 		write(connfd, buff, strlen(buff));




More information about the alfs-log mailing list