【发布时间】:2015-06-21 10:30:12
【问题描述】:
以下是 Web 服务器的客户端服务器应用程序的代码。这段代码工作正常,首先我将运行服务器脚本,将我的服务器程序绑定到指定的 IP 和端口,然后当我运行客户端脚本并连接到相同的 IP 和端口时,服务器将接受连接并调用 subroutine_name 子程序,我在其中创建了一个随机变量 $zz 并使用 test.php 将其数据存储到数据库。 这对于这个服务器客户端应用程序来说工作得很好,但是当我试图通过配置相同的 IP 和端口将 GPS 设备(tk103) 连接到相同的运行服务器脚本时,什么也没有发生。表示服务器脚本和 GPS 设备之间没有建立连接。
请帮帮我。
这是我的客户端脚本。 client.pl
#!/usr/bin/perl
use cPanelUserConfig;
use strict;
print "Content-Type: text/html \n\n";
# auto-flush on socket
$| = 1;
use IO::Socket::INET;
# create a connecting socket
my $socket = new IO::Socket::INET (
PeerHost => '199.79.62.103',
PeerPort => '7784',
Proto => 'tcp',
);
print "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
# data to send to a server
my $req = 'hello world';
my $size = $socket->send($req);
print "sent data of length $size\n";
# notify server that request has been sent
shutdown($socket, 1);
# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";
$socket->close();
这是我的 PHP 服务器脚本。 server.pl
#!/usr/bin/perl
use cPanelUserConfig;
print "Content-Type: text/html \n\n";
use warnings;
use IO::Socket::INET;
# auto-flush on socket
$| = 1;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '199.79.62.103',
LocalPort => '7784',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
print "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7784\n";
while(1)
{
# waiting for a new client connection
my $client_socket = $socket->accept();
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";
subroutine_name();
# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
print "received data: $data\n";
# write response data to the connected client
$data = "ok mi";
$client_socket->send($data);
# notify client that response has been sent
shutdown($client_socket, 1);
}
sub subroutine_name{
my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "My first report generated by perl\n";
close $fh;
print "done\n";
$zz='hello world my world';
my $url = "http://www.geekzgarage.com/gps/test.php?"
. "ln=$zz";
# print "$url\n";
use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;
}
$socket->close();
【问题讨论】:
-
你解决了这个问题了吗?也许你已经这样做了,但如果没有,你能解释一下你是如何设置 GPS 设备的吗?您使用哪些命令来设置服务器的主机名/IP 和端口?您是否在同一台机器上测试过您的 perl 客户端?也许你需要在服务器上开放一些端口。
-
@Frank 我做了你上面所说的一切。但我无法打开我的端口以进行外部连接。在我使用共享主机之前,出于安全原因无法打开端口。但现在我正在研究 Amazon ec2 虚拟云服务器。在这里,我的应用程序也无法打开端口。给我一些有用的建议...
-
你可能想检查这个答案-stackoverflow.com/a/13054630