Added filename creation to basic connection
This commit is contained in:
		@ -1,5 +1,6 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
@ -36,12 +37,20 @@ int main(int argc, char *argv[])
 | 
			
		||||
		{ NULL, 0, NULL, 0 }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	exec_name = basename(argv[0]);
 | 
			
		||||
 | 
			
		||||
	int flag = 1;
 | 
			
		||||
	int sockfd = 0, connfd = 0;
 | 
			
		||||
	int port = 10800;
 | 
			
		||||
	uint32_t addr = INADDR_ANY;
 | 
			
		||||
	struct sockaddr_in sock;
 | 
			
		||||
 | 
			
		||||
	struct sockaddr_in client;
 | 
			
		||||
	socklen_t client_sz = sizeof(client);
 | 
			
		||||
	char timebuff[15], fnbuff[35];
 | 
			
		||||
 | 
			
		||||
	time_t timeraw;
 | 
			
		||||
	struct tm* timeptr;
 | 
			
		||||
 | 
			
		||||
	exec_name = basename(argv[0]);
 | 
			
		||||
 | 
			
		||||
	for(int ch; (ch = getopt_long(argc, argv, "b:p:vh", longopts, NULL)) != -1;){
 | 
			
		||||
		switch(ch){
 | 
			
		||||
@ -83,34 +92,52 @@ int main(int argc, char *argv[])
 | 
			
		||||
 | 
			
		||||
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
 | 
			
		||||
 | 
			
		||||
	struct sockaddr_in sockin;
 | 
			
		||||
	memset(&sockin, 0, sizeof(sockin));
 | 
			
		||||
	sockin.sin_family = AF_INET;
 | 
			
		||||
	sockin.sin_addr.s_addr = addr;
 | 
			
		||||
	sockin.sin_port = htons(port);
 | 
			
		||||
	memset(&sock, 0, sizeof(sock));
 | 
			
		||||
	sock.sin_family = AF_INET;
 | 
			
		||||
	sock.sin_addr.s_addr = addr;
 | 
			
		||||
	sock.sin_port = htons(port);
 | 
			
		||||
 | 
			
		||||
	if(bind(sockfd, (struct sockaddr*)&sockin, sizeof(sockin)) < 0){
 | 
			
		||||
	if(bind(sockfd, (struct sockaddr*)&sock, sizeof(sock)) < 0){
 | 
			
		||||
		fprintf(stderr, "%s: cannot bind to port - %s\n",
 | 
			
		||||
			exec_name, strerror(errno)
 | 
			
		||||
		);
 | 
			
		||||
		goto shutdown_error;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(listen(sockfd, 10) != 0){
 | 
			
		||||
	if(listen(sockfd, 5) != 0){
 | 
			
		||||
		fprintf(stderr, "%s: cannot listen - %s\n",
 | 
			
		||||
			exec_name, strerror(errno)
 | 
			
		||||
		);
 | 
			
		||||
		goto shutdown_error;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	connfd = accept(sockfd, NULL, NULL);
 | 
			
		||||
	while(1){
 | 
			
		||||
		// Wait for a connection on our bound socket
 | 
			
		||||
		connfd = accept(sockfd, (struct sockaddr*)&client, &client_sz);
 | 
			
		||||
		if(connfd == -1){
 | 
			
		||||
		fprintf(stderr, "%s: connection failed - %s\n",
 | 
			
		||||
			fprintf(stderr, "%s: accept failed - %s\n",
 | 
			
		||||
				exec_name, strerror(errno)
 | 
			
		||||
			);
 | 
			
		||||
			goto shutdown_error;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Collect the time of the connection, process it into 
 | 
			
		||||
		// a short string
 | 
			
		||||
		time(&timeraw);
 | 
			
		||||
		timeptr = localtime(&timeraw);
 | 
			
		||||
		strftime(&timebuff[0], 15, "%Y%m%d%H%M%S", timeptr);
 | 
			
		||||
		timeptr = NULL;
 | 
			
		||||
 | 
			
		||||
		// Take the short date/time connection string and add the client address
 | 
			
		||||
		// to create a filename
 | 
			
		||||
		snprintf(&fnbuff[0], 34, "%s-%s.dat", timebuff, inet_ntoa(client.sin_addr));
 | 
			
		||||
 | 
			
		||||
		fprintf(stdout, "filename: %s\n", fnbuff);
 | 
			
		||||
 | 
			
		||||
		// We're done, close the connection
 | 
			
		||||
		close(connfd); connfd = 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	shutdown_clean:
 | 
			
		||||
	if(connfd > 0) close(connfd);
 | 
			
		||||
	if(sockfd > 0) close(sockfd);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user