r2216 - in alfs-POC: . client-server

gerard at linuxfromscratch.org gerard at linuxfromscratch.org
Fri Nov 25 12:47:43 PST 2005


Author: gerard
Date: 2005-11-25 13:47:43 -0700 (Fri, 25 Nov 2005)
New Revision: 2216

Added:
   alfs-POC/client-server/
   alfs-POC/client-server/Makefile
   alfs-POC/client-server/client.c
   alfs-POC/client-server/server.c
Log:
Simple client<->server connection

Added: alfs-POC/client-server/Makefile
===================================================================
--- alfs-POC/client-server/Makefile	2005-11-25 20:44:44 UTC (rev 2215)
+++ alfs-POC/client-server/Makefile	2005-11-25 20:47:43 UTC (rev 2216)
@@ -0,0 +1,10 @@
+all: client server
+
+client: client.c
+	gcc -W -Wall -Werror -pedantic -std=gnu99 -D_GNU_SOURCE client.c -o client
+
+server: server.c
+	gcc -W -Wall -Werror -pedantic -std=gnu99 -D_GNU_SOURCE server.c -o server
+
+clean:
+	rm client server

Added: alfs-POC/client-server/client.c
===================================================================
--- alfs-POC/client-server/client.c	2005-11-25 20:44:44 UTC (rev 2215)
+++ alfs-POC/client-server/client.c	2005-11-25 20:47:43 UTC (rev 2216)
@@ -0,0 +1,42 @@
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+
+int
+main (int argc, char **argv)
+{
+
+	#define MAXLINE 4096
+	#define SA struct sockaddr
+	
+	int sockfd, n;
+	char recvline [MAXLINE+1];
+	struct sockaddr_in servaddr;
+	
+	if (argc != 2) {
+		printf("usage: %s IPaddress\n", argv[0]);
+		exit(1);
+	}
+	
+	sockfd = socket(AF_INET, SOCK_STREAM, 0);
+		
+	bzero(&servaddr, sizeof(servaddr));
+	servaddr.sin_family = AF_INET;
+	servaddr.sin_port = htons(1234);
+	inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
+	
+	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");
+	}
+	if (n < 0)
+		printf("read error\n");
+	
+	exit(0);
+}

Added: alfs-POC/client-server/server.c
===================================================================
--- alfs-POC/client-server/server.c	2005-11-25 20:44:44 UTC (rev 2215)
+++ alfs-POC/client-server/server.c	2005-11-25 20:47:43 UTC (rev 2216)
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+int
+main ()
+{
+
+	#define MAXLINE 4096
+	#define LISTENQ 5
+	#define SA struct sockaddr
+
+	int listenfd, connfd;
+	struct sockaddr_in servaddr;
+	char buff[MAXLINE];
+	
+	listenfd = socket(AF_INET, SOCK_STREAM, 0);
+	
+	bzero(&servaddr, sizeof(servaddr));
+	servaddr.sin_family = AF_INET;
+	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+	servaddr.sin_port = htons(1234);
+	
+	bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
+	listen(listenfd, LISTENQ);
+	
+	for ( ; ; ) {
+		connfd = accept(listenfd, (SA *) NULL, NULL);
+		
+		snprintf(buff, sizeof(buff), "This is a test\r\n");
+		write(connfd, buff, strlen(buff));
+	
+		close(connfd);
+	}
+}




More information about the alfs-log mailing list