Index: tools/FAD/SocketFunctions/SocketFunctions.cpp
===================================================================
--- tools/FAD/SocketFunctions/SocketFunctions.cpp	(revision 256)
+++ tools/FAD/SocketFunctions/SocketFunctions.cpp	(revision 256)
@@ -0,0 +1,226 @@
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <cstdlib>
+#include <time.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include "SocketFunctions.h"
+
+/*
+OpenSockets opens NumberOfSockets TCP Sockets.
+a pointer to an array of SocketDescriptors is returned.
+In case of error, the NULL pointer is returned.
+
+In order to Close the Sockets and free memory call
+void CloseSockets(int * SocketDescriptors, unsigned int NumberOfSockets);
+*/
+
+int * OpenSockets(unsigned int NumberOfSockets){
+int verbose=0;
+
+if (verbose)
+
+	if (NumberOfSockets < 1) {
+		fprintf (	stderr, 
+							"Error: OpenSockets was called with a NumberOfSockets = %d.\n", 
+							NumberOfSockets );
+							return NULL;
+	}
+	if (NumberOfSockets > MAX_NUMBER_OF_OPENED_TCP_SOCKETS) {
+		fprintf (	stderr, 
+							"Warning: OpenSockets was called with a NumberOfSockets = %d.\n", 
+							NumberOfSockets );
+							return NULL;
+	}
+	
+	int *SocketDescriptor;
+	if (verbose) fprintf (stderr, "malloc\n");
+	SocketDescriptor = (int *) malloc( NumberOfSockets * sizeof(int) );
+	if (verbose) fprintf (stderr, "after malloc\n");
+	// what if malloc was not able to get enough memory?
+	// error chekc missing here
+	// TODO
+	
+	for (unsigned int i = 0; i < NumberOfSockets; i++) {
+		if ((SocketDescriptor[i] = socket (PF_INET, SOCK_STREAM, 0)) == -1) {
+		//
+		// what does PF_INET and SOCK_STREAM stand for?
+		//
+			printf ("Error: Could not open socket Nr.: %d\n", i);
+			return NULL;
+		} 
+	}
+	return SocketDescriptor;
+	if (verbose) printf ("end of OpenSockets\n");
+} // end of OpenSockets
+
+
+
+
+
+	/*
+	EmptySockets is used to read as much data from the TCP-Sockets in question
+	as possible. After waiting "timeout_usec" (default 125ms) -- see header file
+	without any data beeing present, the Socket is assumed to be empty.
+	
+	Hopefully it is.
+	
+	return is true if there was no error during the select statement.
+	false if select returned <0, which should not happen at all.
+	What to do if this happened?
+	*/
+	
+	
+bool EmptySockets(int * SocketDescriptor, int NumberOfDescriptors, long timeout_usec){
+	
+	fd_set Sockets; // select looks into this set, and checks which Sockets state changed
+	timeval timeout; // select needs a timeout to be deifned.
+	int max_fd=0; // select needs to know the maximum descriptor in fd_set Sockets.
+	int select_return; // used for debugging 
+	
+	unsigned char wastebin[WASTEBIN_SIZE]; // data ffrom sockets is dumped here
+	
+	// find the maximum of SocketDescriptors
+	// needed by select() -- see man select
+	
+	
+	FD_ZERO (&Sockets);
+	for (int j = 0; j < NumberOfDescriptors; j++)
+	{
+		if (max_fd<SocketDescriptor[j]) {
+			max_fd = SocketDescriptor[j];
+		}
+		FD_SET (SocketDescriptor[j], &Sockets);
+	}
+	max_fd ++;
+	 
+	while (1){
+		timeout.tv_sec = 0;
+		timeout.tv_usec = timeout_usec;
+		select_return = select ( max_fd, &Sockets, NULL, NULL, &timeout);
+		if (select_return == 0) { 
+			// none of the Sockets status changed
+			// nothing to read
+			break;
+		} else if (select_return <=0) {
+			return false; // this should never happen
+			fprintf(stderr, "error in EmptySockets: select returned %d abort. \n" , select_return);
+		} else { // select returned > 0
+			// now some Sockets might contain data
+			// it is checked for every Socket using: FD_ISSET
+			// then a pieces of WASTEBIN_SIZE chars is read out and discarded.
+			for (int j = 0; j < NumberOfDescriptors; j++) {		
+				if (FD_ISSET (SocketDescriptor[j], &Sockets)){						
+					read(SocketDescriptor[j], wastebin, WASTEBIN_SIZE);
+				}
+			}
+			//now every Socket, which had still some bytes, lost WASTEBIN_SIZE of it.
+			// still some might contain data -- so select is executed again.
+		}// end of else case: select returned > 0
+	} // end of while(1) loop -- jumped out if select returns 0
+return true;
+} 
+
+/*
+	GetMaxFileDescriptor returns the value of the maximal
+	filedescriptor from NumberOfSockets SocketDescriptors.
+	The maximal filedescriptor + 1 is needed for select().
+*/
+
+int GetMaxFileDescriptor(unsigned int NumberOfSockets, int *SocketDescriptor){
+	int max_fd;
+	for (unsigned int i = 0; i < NumberOfSockets; i++)
+	{
+			if (SocketDescriptor[i] > max_fd)
+			{
+				max_fd = SocketDescriptor[i];
+			}
+	}
+	return max_fd;
+}
+
+
+
+
+/*
+Connect2Server connects NumberOfSockets TCP Sockets
+defined by SocketDescriptor
+to host defined by ServerIPaddress
+starting at Port StartPort, by adding 1 for each Socket
+
+return value is NumberOfSockets if successful
+it is zero if no Server was found and negative 
+when connecting didn't work.
+The negative magnitude indicates which connection didn't work
+*/
+
+int Connect2Server(	int *SocketDescriptor, 
+											unsigned int NumberOfSockets,  
+											unsigned int StartPort , 
+											char *ServerIPaddress , 
+											int verbose){
+	
+	struct sockaddr_in SocketAddress[NumberOfSockets];
+	struct in_addr Serveripv4addr;
+	
+	// Convert IP-Address to binary
+	if (inet_pton (AF_INET, ServerIPaddress, &Serveripv4addr) <= 0)
+	{
+		fprintf (stderr, "Error: network address not valid\n");
+		return 0;
+	}
+
+	if (verbose > 0) fprintf (stdout, "Trying to connect to %s...\n", ServerIPaddress);
+	
+	for (unsigned int i = 0; i < NumberOfSockets; i++)
+	{
+		SocketAddress[i].sin_family = PF_INET;
+		SocketAddress[i].sin_port = htons ((unsigned short) StartPort + i);
+		SocketAddress[i].sin_addr = Serveripv4addr;
+		
+		if (connect (SocketDescriptor[i], (struct sockaddr *) &SocketAddress[i], sizeof (SocketAddress[i])) == -1)
+		{
+			fprintf (stderr, "Error: Could not connect to server %s (port %d)\n", ServerIPaddress, StartPort + i);
+			return -i;
+		}
+		else if (verbose>0) {
+				printf ("Connected to %s:%d\n", ServerIPaddress, StartPort + i);
+		}
+	}
+	
+	return NumberOfSockets;
+}
+
+/*
+CloseSockets closes NumberOfSockets TCP Sockets
+indicated by SocketDescriptor and frees the associated memory.
+*/
+int CloseSockets (int * SocketDescriptor , 
+									unsigned int NumberOfSockets) 
+{
+	int close_return_val;
+	int return_val=0;
+	
+	for (unsigned int i = 0; i < NumberOfSockets; i++)
+	{
+		if (  (close_return_val = close (SocketDescriptor[i])) != 0) {
+		fprintf (stderr, "Error: while trying to close SocketDescriptor[%d] = %d, close returned %d\n",
+							i , 
+							SocketDescriptor[i],
+							close_return_val );
+							return_val = close_return_val;
+							// no return close_return_val 
+							// is performed here, because I want to try to close
+							// the other Ports before returning.
+		}
+		// ERRNO should better be checked and printed as well.
+		// TODO
+	}
+	
+	return return_val;
+
+} // end of CloseSockets
+	
Index: tools/FAD/SocketFunctions/SocketFunctions.h
===================================================================
--- tools/FAD/SocketFunctions/SocketFunctions.h	(revision 256)
+++ tools/FAD/SocketFunctions/SocketFunctions.h	(revision 256)
@@ -0,0 +1,59 @@
+#define MAX_NUMBER_OF_OPENED_TCP_SOCKETS 100
+
+/*
+OpenSockets opens NumberOfSockets TCP Sockets.
+a pointer to an array of SocketDescriptors is returned.
+In case of error, the NULL pointer is returned.
+
+In order to Close the Sockets and free memory call
+void CloseSockets(int * SocketDescriptors, unsigned int NumberOfSockets);
+*/
+int * OpenSockets(unsigned int NumberOfSockets);
+
+/*
+	EmptySockets is used to read as much data from the TCP-Sockets in question
+	as possible. After waiting "timeout_usec" (default 125ms) -- see header file
+	without any data beeing present, the Socket is assumed to be empty.
+	
+	Hopefully it is.
+	
+	return is true if there was no error during the select statement.
+	false if select returned <0, which should not happen at all.
+	What to do if this happened?
+	*/
+	#define WASTEBIN_SIZE 1000
+bool EmptySockets(int *SocketDescriptor, int NumberOfDescriptors, long timeout_usec = 125000L);
+
+/*
+	GetMaxFileDescriptor returns the value of the maximal
+	filedescriptor from NumberOfSockets SocketDescriptors.
+	The maximal filedescriptor + 1 is needed for select().
+*/
+int GetMaxFileDescriptor(unsigned int NumberOfSockets, int *SocketDescriptor);
+
+/*
+Connect2Server connects NumberOfSockets TCP Sockets
+defined by SocketDescriptor
+to host defined by ServerIPaddress
+starting at Port StartPort, by adding 1 to each Socket
+
+return value is NumberOfSockets if successful
+it is zero if no Server was found and negative 
+when connecting didn't work.
+The negative magnitude indicates which connection didn't work
+*/
+int Connect2Server(	int *SocketDescriptor, 
+											unsigned int NumberOfSockets,  
+											unsigned int StartPort , 
+											char *ServerIPaddress , 
+											int verbose = 0);
+											
+											
+/*
+CloseSockets closes NumberOfSockets TCP Sockets
+indicated by SocketDescriptor and frees the associated memory.
+*/
+int CloseSockets (int * SocketDescriptor , 
+									unsigned int NumberOfSockets);
+
+									
Index: tools/FAD/simple_daq/Makefile
===================================================================
--- tools/FAD/simple_daq/Makefile	(revision 255)
+++ tools/FAD/simple_daq/Makefile	(revision 256)
@@ -2,5 +2,5 @@
 CFLAGS=-c -Wall
 LDFLAGS=
-SOURCES=simple_daq.cpp cmd_send.cpp
+SOURCES=simple_daq.cpp cmd_send.cpp ../SocketFunctions/SocketFunctions.cpp
 OBJECTS=$(SOURCES:.cpp=.o)
 EXECUTABLE=simple_daq
Index: tools/FAD/simple_daq/simple_daq.cpp
===================================================================
--- tools/FAD/simple_daq/simple_daq.cpp	(revision 255)
+++ tools/FAD/simple_daq/simple_daq.cpp	(revision 256)
@@ -1,5 +1,5 @@
 /********************************************************************\
 
-  Read 8 Channels from FAD-Board
+  Read SOCKETS_PER_FAD Channels from FAD-Board
   Write Commands to Socket 0, Commands must be (n * 16) Bit long
   
@@ -18,85 +18,59 @@
 #endif
 #include "simple_daq.h"
+#include "../SocketFunctions/SocketFunctions.h"
 
-int SocketDescriptor[8];
+
+int *SocketDescriptor;
 
 int main(int argc, char *argv[])
 {
+	// what kind of buffer is this?
 	char Buffer[MAX_COM_SIZE];
 	
-	// default server
-	char ServerName[IP_ADDR_LENGTH] = IP_ADDR_DEFAULT; // default IP-Address
+	int read_return;
 
-	int i, read_return;
+	fd_set ReadFileDescriptor;
+	int max_fd = 0;
+	
+	FILE* outfile[SOCKETS_PER_FAD];
+	char outfilename[PATH_MAX];
 
-	int DAQPort = FIRST_DAQPORT;
-	struct sockaddr_in SocketAddress[8];
-	struct in_addr Serveripv4addr;
-	fd_set ReadFileDescriptor;
-
-	FILE* fhandle[8];
-	char fname[PATH_MAX];
-
-	int max_fd = 0;
-
-	// Get IP-Address from command line
+	
+		SimpleDaqConfiguration *conf;
+	// Get configuration file path from command line
+	// Get configurarion from configurationfile.
+	// such as FAD IP Adress
 	if (argc > 1)
 	{
-		strncpy (ServerName, argv[1], IP_ADDR_LENGTH);
+		conf = getConfig(argv[1]);
+	} else {
+		conf = getConfig(CONFIG_FILE_PATH);
 	}
 	
-	// Convert IP-Addr to binary
-	if (inet_pton (AF_INET, ServerName, &Serveripv4addr) <= 0)
-	{
-		printf ("Error: network address not valid\n");
+	// Open sockets
+	SocketDescriptor = OpenSockets(SOCKETS_PER_FAD);
+	if (SocketDescriptor == NULL) {
 		exit_program (EXIT_FAILURE);
 	}
+	max_fd = GetMaxFileDescriptor(SOCKETS_PER_FAD, SocketDescriptor);
+	
+	// Connect to server
+	if( Connect2Server(	SocketDescriptor, SOCKETS_PER_FAD, FIRST_DAQPORT, conf->FADIPAddress, 1) != SOCKETS_PER_FAD) {
+			// Connect2Server prints some error messages in case of exceptions...
+			printf ("Error in Connect2Server()\n");
+			exit_program (EXIT_FAILURE);
+	}
 
-	// Open sockets
-	for (i = 0; i < 8; i++)
+	// Open files for output
+	for (int i = 0; i < SOCKETS_PER_FAD; i++)
 	{
-		if ((SocketDescriptor[i] = socket (PF_INET, SOCK_STREAM, 0)) == -1)
+		sprintf (outfilename, "%s/%s-%d.%s", conf->outfilepath, conf->outfilename, i, conf->outfileext);
+		if ((outfile[i] = fopen (outfilename, "w")) == NULL)
 		{
-			printf ("Error: Could not open socket Nr.: %d\n", i);
-			exit_program (EXIT_FAILURE);
-		}
-		else
-		{
-			if (SocketDescriptor[i] > max_fd)
-			{
-				max_fd = SocketDescriptor[i];
-			}
-		}
-	}
-	
-	// Open files for output
-	for (i = 0; i < 8; i++)
-	{
-		sprintf (fname, "socket-%d.dat", i);
-		if ((fhandle[i] = fopen (fname, "w")) == NULL)
-		{
-			printf ("Error: Could not open file %s\n", fname);
+			printf ("Error: Could not open file %s\n", outfilename);
 			exit_program (EXIT_FAILURE);
 		}
 	}
 
-	// Connect to server
-	printf ("Trying to connect to %s...\n", ServerName);
-	for (i = 0; i < 8; i++)
-	{
-		SocketAddress[i].sin_family = PF_INET;
-		SocketAddress[i].sin_port = htons ((unsigned short) DAQPort + i);
-		SocketAddress[i].sin_addr = Serveripv4addr;
-		
-		if (connect (SocketDescriptor[i], (struct sockaddr *) &SocketAddress[i], sizeof (SocketAddress[i])) == -1)
-		{
-			printf ("Error: Could not connect to server %s (port %d)\n", ServerName, DAQPort + i);
-			exit_program (EXIT_FAILURE);
-		}
-		else
-		{
-			printf ("Connected to %s:%d\n", ServerName, DAQPort + i);
-		}
-	}
 
 	signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
@@ -108,9 +82,7 @@
 		fflush (stdout);
 
-		FD_ZERO (&ReadFileDescriptor);
-		
+		FD_ZERO (&ReadFileDescriptor);		
 		FD_SET(STDIN_FILENO, &ReadFileDescriptor);
-		
-		for (i = 0; i < 8; i++)
+		for (int i = 0; i < SOCKETS_PER_FAD; i++)
 		{
 			FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
@@ -137,5 +109,5 @@
 		{
 			// Check all sockets
-			for (i = 0; i < 8; i++)
+			for (int i = 0; i < SOCKETS_PER_FAD; i++)
 			{
 				if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
@@ -153,7 +125,7 @@
 						printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
 					
-						fwrite (Buffer, 1, (size_t) read_return, fhandle[i]);
+						fwrite (Buffer, 1, (size_t) read_return, outfile[i]);
 						// Important!!!
-						fflush (fhandle[i]);
+						fflush (outfile[i]);
 					}
 				}
@@ -167,9 +139,7 @@
 // close sockets and exit
 void exit_program (int exit_status)
-{
-	int i;
-	
+{	
 	printf ("\nClosing Sockets...");
-	for (i = 0; i < 8; i++)
+	for (int i = 0; i < SOCKETS_PER_FAD; i++)
 	{
 		close (SocketDescriptor[i]);
@@ -185,2 +155,25 @@
 	exit_program (EXIT_SUCCESS);
 }
+
+
+// note: verbose is not used, but anyway defined.
+SimpleDaqConfiguration *getConfig (char *path, int verbose) {
+FILE* ConfigFile;
+// try to open config file
+// if not exists return NULL
+ConfigFile = fopen (path, "r");
+if (ConfigFile == NULL) {
+	return NULL;
+}
+
+//create SimpleDaqConfiguration
+SimpleDaqConfiguration *conf = new SimpleDaqConfiguration();
+
+// read config data from file and fill in SimpleDaqConfiguration
+fscanf( ConfigFile , "%s" , conf->FADIPAddress );
+fscanf( ConfigFile , "%s" , conf->outfilepath );
+fscanf( ConfigFile , "%s" , conf->outfilename );
+fscanf( ConfigFile , "%s" , conf->outfileext );
+
+return conf;
+}
Index: tools/FAD/simple_daq/simple_daq.h
===================================================================
--- tools/FAD/simple_daq/simple_daq.h	(revision 255)
+++ tools/FAD/simple_daq/simple_daq.h	(revision 256)
@@ -13,8 +13,7 @@
 #define MAX_COM_SIZE 32000
 #define IP_ADDR_LENGTH 16
-//#define IP_ADDR_DEFAULT "192.33.99.225"
-#define IP_ADDR_DEFAULT "129.217.160.119"
 
 #define FIRST_DAQPORT 5000
+#define SOCKETS_PER_FAD 8
 
 // Commands for FAD
@@ -43,7 +42,18 @@
 #define MAX_DACVAL 65535
 
+#define CONFIG_FILE_PATH "config.txt"
+
+class SimpleDaqConfiguration {
+public:
+	char FADIPAddress[IP_ADDR_LENGTH];
+	char outfilepath[200];
+	char outfilename[50];
+	char outfileext[10];
+};
+
 void cmd_send (char* Buffer, int Socket); // Send commands to socket
 void int_handler (int sig); // Handle signal SIGINT (CTRL-C)
 void exit_program (int exit_status); // Cleanup and exit
+SimpleDaqConfiguration *getConfig (char *path, int verbose = 0);
 
 #endif /*SOCKETCLIENT_H_*/
