#!/usr/local/bin/perl -w # UDP Server gets mysql_data from mysql or a file # receives request from client # compares request with mysql_data # responds to client # use strict; use DBI; use Cwd; use IO::Socket; # # Zuweisung der benötigten Directories für das Laden # ### Pfade intitialisieren my $aktuellesdir = getcwd; my $dirprefix = "$aktuellesdir/"; my $fehler_datei = "$dirprefix"."serv.log"; # Logdatei; =pod ## Database fields my $l_id; my $l_ipadress; my $l_spindle; my $l_vin; my $l_requestset; my $l_setok; my $l_requesttime; my $l_donetime; =cut ## Hilfsvariable my $line; ## Zeit (my $sec, my $min, my $std, my $tag, my $mon, my $jahr) = localtime(time()); $mon = $mon + 1; $jahr = $jahr+1900; my $heute = "$tag.$mon.$jahr $std:$min" ; print "Heute: $heute\n"; print "Fehler_datei: $fehler_datei\n"; =pod ## open logfile open (FH_ERROR, ">>$fehler_datei" ) or die "Unable to open datei $fehler_datei: $!"; print "Now connecting to a mysql database mysql ...\n"; # host=192.168.178.22 # host=DELL my $dbmy = DBI->connect("DBI:mysql:ftc;host=192.168.178.22","root","root") or die "Can't connect to mySQL : $DBI::errstr\n"; print "Locken der Tabellen in MYSQL...\n"; print "Now preparing SQL Select statement for prod_vinrequests...\n"; my $sql_select = qq {SELECT ID, IPAdress, Spindle, VIN, RequestSet, SetOK, RequestTime, DoneTime FROM ftc.prod_vinrequests}; my $stmy = $dbmy->prepare ($sql_select); ### csv Datei open (MYCSV, ">prodvin.txt") or die "Kann Datei nicht oeffnen $!\n"; print "Now executing SQL Select statement for prod_vinrequests...\n"; $stmy->execute(); while ( ( $l_id, $l_ipadress, $l_spindle, $l_vin, $l_requestset, $l_setok, $l_requesttime, $l_donetime ) = $stmy->fetchrow_array() ) { #print "l_id: $l_id | l_ipadress: $l_ipadress | l_spindle: $l_spindle | l_vin: $l_vin | l_requestset: $l_requestset | l_setok: $l_setok | l_requesttime: l_requesttime | l_donetime: l_donetime \n"; #print "$l_id :: $l_ipadress :: $l_spindle :: $l_vin :: $l_requestset :: $l_setok :: l_requesttime :: l_donetime \n"; $line = $l_id .",". $l_ipadress .",". $l_spindle .",". $l_vin .",". $l_requestset .",". $l_setok; print "line::: $line \n"; print MYCSV ""; ## zum leeren print MYCSV $line ."\n"; } ## while #$dbmy->do("UNLOCK TABLES"); print "Now disconnecting from the mysql database mysql...\n"; $dbmy->disconnect or warn "Disconnect failure from mysql $DBI::errstr\n"; close(MYCSV); close(FH_ERROR); =cut ######### Server section ######### use Socket qw(:DEFAULT :crlf); $/ = CRLF; #use constant DEFAULT_HOST =>'localhost'; #use constant DEFAULT_HOST =>'192.169.212.50'; use constant DEFAULT_PORT =>'4712'; use constant MAX_MSG_LEN => 100; #my $line; ## Hilfsvariable fuer line my $quellfile = "$dirprefix"."empfangsfile.txt"; my $EMPFANG; #open (RECVFILE, ">>$quellfile") or die "Kann Datei $quellfile nicht oeffnen $!\n"; ### UDP Variablen initialisieren my ($sock, $PORTNO, $MAXLEN, $nachricht); $PORTNO = shift ||DEFAULT_PORT ; #$PORTNO = 0; $MAXLEN = 1024; my $laenge; my $startzeit; $sock = IO::Socket::INET->new ( LocalPort=>$PORTNO, Proto=>'udp') or die "socket: $@"; print "Warte auf UDP Nachricht auf Port $PORTNO$/"; while ($sock->recv($nachricht, $MAXLEN)) { $laenge = length($nachricht); $startzeit = scalar localtime; print "Zeitstempel: $startzeit : Laenge: $laenge : Der Client sagte ''$nachricht''$/"; # $sock->send("Du sagtest: ''$nachricht'' zu mir $/"); # $sock->send("Zeitstempel: $startzeit : Du sagtest: ''$nachricht'' zu mir $/"); $sock->send("Zeitstempel: ''$startzeit'' : Du sagtest: ''$nachricht'' zu mir $/"); open (RECVFILE, ">>$quellfile") or die "Kann Datei $quellfile nicht oeffnen $!\n"; print RECVFILE $nachricht; close(RECVFILE); } die "recv: $!";