急!!!用Perl编写Mysql 接口出现的问题

急!!!用Perl编写Mysql 接口出现的问题

急!!!用Perl编写Mysql 接口出现的问题
源代码如下:
!/usr/bin/perl
# This is a sample program written in Perl that does the same thing as the
# java program ODBCServer.java
#
# This program needs DBI and DBD installed to works
#
#
use DBI;
use IO::Socket;
use Sys::Hostname;

# Main program
my $buffer = "";
my $bufLength = 1514;
my $DBDServer = "DBI:mysql:NTOP";
my $socket = NULL;
my $rv;
my $serverName = "localhost";
my $serverPort = "3306";
my $serverUser = "mysql";
my $serverPass = "lindan";
my $serverDb = "NTOP";

# Listen to the udp port
$socket = IO::Socket::INET->new(

LocalAddr => 'localhost',

LocalPort => 4000,

Proto => 'udp',

Type => SOCK_DGRAM);

# Open the database connection
my $dbh =
DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort
",$serverUser,$serverPass) or die "Couldn't connect to database: " .

DBI->errstr;
# Infinite loop
while(1) {

# Receive the SQL statement
recv($socket, $buffer, $bufLength, 0) or die "recv: $!";

# Print the SQL statement
print "$buffer\n";

# Correct the SQL if needed
$buffer =~ s/^DELETE \*/DELETE/; # Change 'DELETE *' for 'DELETE'

# Correct dates, not needed if ntop gives us yyyy-mm-dd, ntop up
# to 1.2a12 gives mm/dd/yy . 06.02.00 Ralf Amandi
# @dates= ($buffer =~ m!'../../.. ..:..:..'!g);
# foreach $date (@dates) {
# # mm/dd/yy -> yy-mm-dd
# @items=split("[/ ']",$date,6);
# $new="'$items[3]-$items[1]-$items[2] $items[4]'";
# $buffer =~ s/$date/$new/g;
# }

# Correct insert into host (in the mdb database the default
# for FirstSeen is set to now(). mySQL can not handle this). 06.02.00 Ralf Amandi
if (substr($buffer,0,59) eq 'INSERT INTO Hosts (IPaddress, MACaddress, NICvendor) VALUES' ) {
$buffer='INSERT INTO Hosts (IPaddress, MACaddress, NICvendor, FirstSeen) VALUES '.substr($buffer,59,length($buffer)-60).',now())';
}

# Execute the SQL statement
$rv = $dbh -> do($buffer);

# Print a blank line
print "\n";
}
在Ret Hat 9.0下运行时发现错误提示:
Use of uninitialized value in ref-to-glob cast at mySQLserver.pl line 46.
recv() on unopened socket at mySQLserver.pl line 46.
recv: 错误的文件描述符 at mySQLserver.pl line 46.

[2]+ Exit 9 perl -w mySQLserver.pl >/dev/null
line 46 是 recv($socket, $buffer, $bufLength, 0) or die "recv: $!";
请高手帮忙指教一下吧!我有急用!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[quote]--!/usr/bin/per.
[quote]
#!/usr/bin/perl
# This is a sample program written in Perl that does the same thing as the
# java program ODBCServer.java
#
# This program needs DBI and DBD installed to works
#
#
use DBI;
use IO::Socket;
use Sys::Hostname;

# Main program
my $buffer = "";
my $bufLength = 1514;
my $DBDServer = "DBI:mysql:NTOP";
my $socket = NULL;
my $rv;
my $serverName = "localhost";
my $serverPort = "3306";
my $serverUser = "mysql";
my $serverPass = "lindan";
my $serverDb = "NTOP";

# Listen to the udp port
$socket = IO::Socket::INET->new(

LocalAddr => 'localhost',

LocalPort => 4000,

Proto => 'udp',

Type => SOCK_DGRAM) [color=red]or die "$!\n"[/color];

# Open the database connection
my $dbh =
DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort
",$serverUser,$serverPass) or die "Couldn't connect to database: " .

DBI->errstr;
# Infinite loop
while(1) {

# Receive the SQL statement
recv($socket, $buffer, $bufLength, 0) or [color=red]warn[/color] "recv: $!";

# Print the SQL statement
print "$buffer\n";

# Correct the SQL if needed
$buffer =~ s/^DELETE \*/DELETE/; # Change 'DELETE *' for 'DELETE'

# Correct dates, not needed if ntop gives us yyyy-mm-dd, ntop up
# to 1.2a12 gives mm/dd/yy . 06.02.00 Ralf Amandi
# @dates= ($buffer =~ m!'../../.. ..:..:..'!g);
# foreach $date (@dates) {
# # mm/dd/yy -> yy-mm-dd
# @items=split("[/ ']",$date,6);
# $new="'$items[3]-$items[1]-$items[2] $items[4]'";
# $buffer =~ s/$date/$new/g;
# }

# Correct insert into host (in the mdb database the default
# for FirstSeen is set to now(). mySQL can not handle this). 06.02.00 Ralf Amandi
if (substr($buffer,0,59) eq 'INSERT INTO Hosts (IPaddress, MACaddress, NICvendor) VALUES' ) {
$buffer='INSERT INTO Hosts (IPaddress, MACaddress, NICvendor, FirstSeen) VALUES '.substr($buffer,59,length($buffer)-60).',now())';
}

# Execute the SQL statement
$rv = $dbh -> do($buffer);

# Print a blank line
print "\n";
}
[/quote]
我这里调试没有问题,是不是你的 redhat 4000 端口被占用了?
上面的代码改动了点地方,你运行一下看看,如果出错,贴上错误信息,再论




   

急!!!用Perl编写Mysql 接口出现的问题
[root@localhost database]# perl -w mySQLserver.pl >/dev/null &
[1] 3409
[root@localhost database]# perl -w mySQLserver.pl >/dev/null &
[2] 3411
[root@localhost database]# 地址已在使用

[2]+ Exit 98 perl -w mySQLserver.pl >/dev/null
急!!!用Perl编写Mysql 接口出现的问题
谢谢 cnhacktnt
但是我按照你的方法改了以后就出现了上面的错误
还有你在上贴提到的redhat 4000 端口被占用的情况,怎么在红帽上查看和排除这个问题?
[quote]perl -w mySQLserv.
[quote]perl -w mySQLserver.pl >/dev/null &[/quote]
这已经把 mysqlserver.pl 调到后台执行了,如果你再 perl -w mySQLserver.pl >/dev/null & ,当然会报错,端口被上一个脚本占用了

你要调试,先用 netstat -an 命令确认机器上的 4000 号端口没有被其他进程占用,如果已经被占用,把脚本中的4000号端口改成其他未被占用的端口;如果没有被占用的话的话再[quote]perl -w mySQLserver.pl[/quote]
如果没有错误,此时你再在其他的终端里 netstat -an 看看,现在4000号端口已经被你的脚本进程打开了,现在它就在监听状态下了,你自己再按照需求去调试就可以了




   

偶的问题已解决--谢谢cn.
偶的问题已解决
谢谢cnhacktnt的及时帮助: )