r2218 - alfs-POC/client-server

gerard at linuxfromscratch.org gerard at linuxfromscratch.org
Fri Nov 25 13:41:26 PST 2005


Author: gerard
Date: 2005-11-25 14:41:25 -0700 (Fri, 25 Nov 2005)
New Revision: 2218

Modified:
   alfs-POC/client-server/client.c
   alfs-POC/client-server/server.c
Log:
Added comments

Modified: alfs-POC/client-server/client.c
===================================================================
--- alfs-POC/client-server/client.c	2005-11-25 20:56:38 UTC (rev 2217)
+++ alfs-POC/client-server/client.c	2005-11-25 21:41:25 UTC (rev 2218)
@@ -9,8 +9,11 @@
 main (int argc, char **argv)
 {
 
-	#define MAXLINE 4096
-	#define SA struct sockaddr
+	#define MAXLINE 4096 /* used for buffer size */
+	#define SA struct sockaddr /* just a shortcut, saves typing
+				      later in the connect() function
+				      and prevents that line from wrapping
+				   */
 	
 	int sockfd, n;
 	char recvline [MAXLINE+1];
@@ -20,23 +23,36 @@
 		printf("usage: %s IPaddress\n", argv[0]);
 		exit(1);
 	}
-	
+
+	/* setup socked descriptor */
 	sockfd = socket(AF_INET, SOCK_STREAM, 0);
-		
+
+	/* set structure to zero */
 	bzero(&servaddr, sizeof(servaddr));
+
+	/* set to IPv4 */
 	servaddr.sin_family = AF_INET;
+
+	/* Use port 1234 */
 	servaddr.sin_port = htons(1234);
+
+	/* 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));
-	
-	while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
-		recvline[n] = 0;
-		if (fputs(recvline, stdout) == EOF)
-			printf("fputs error\n");
-	}
+
+	/* read data from server */	
+	n = read(sockfd, recvline, MAXLINE)
+	recvline[n] = 0;
+
+	if (fputs(recvline, stdout) == EOF)
+		printf("fputs error\n");
+
 	if (n < 0)
 		printf("read error\n");
 	
 	exit(0);
 }
+

Modified: alfs-POC/client-server/server.c
===================================================================
--- alfs-POC/client-server/server.c	2005-11-25 20:56:38 UTC (rev 2217)
+++ alfs-POC/client-server/server.c	2005-11-25 21:41:25 UTC (rev 2218)
@@ -15,22 +15,40 @@
 	struct sockaddr_in servaddr;
 	char buff[MAXLINE];
 	
+	/* setup socket descriptor */
 	listenfd = socket(AF_INET, SOCK_STREAM, 0);
-	
+
+	/* set structure to zero */	
 	bzero(&servaddr, sizeof(servaddr));
+
+	/* set to IPv4 */
 	servaddr.sin_family = AF_INET;
+
+	/* listen on any IP address port 1234 */
 	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 	servaddr.sin_port = htons(1234);
-	
+
+	/* bind to socket and listen for incoming requests */	
 	bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
 	listen(listenfd, LISTENQ);
-	
+
+	/* endless loop. Service one client at a time, then go back
+	   to listening */	
 	for ( ; ; ) {
+		/* 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);
-		
+
+		/* fill buffer with data to send to client */		
 		snprintf(buff, sizeof(buff), "This is a test\r\n");
+
+		/* send buffer to client */
 		write(connfd, buff, strlen(buff));
-	
+
+		/* close connection, restart while loop, goes back to sleep
+		  after accept	*/
 		close(connfd);
 	}
 }
+




More information about the alfs-log mailing list