以下是Web服务器的客户端服务器应用程序的代码。此代码是工作的罚款,即第一,我会跑哪我的服务器程序绑定到指定的IP和端口服务器脚本,之后,当我运行的客户端脚本,并连接到相同的IP和端口,服务器将接受连接和调用subroutine_name子程序我创建了一个随机变量$ zz并使用test.php将其数据存储到数据库。 这是该服务器的客户端应用程序工作正常,但是当我'试图通过配置相同的IP和端口的GPS设备(tk103)连接到同一个正在运行的服务器脚本,没有任何反应。意味着服务器脚本和GPS设备之间没有建立连接。如何在Web服务器上接收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();
2015-06-21
Choxx
+0
你设法尚未解决这个问题?也许你已经做到了,但如果没有,你能否解释一下你如何设置GPS设备?您使用哪些命令来设置服务器的主机名/ IP和端口? 你在同一台机器上测试了你的perl客户端吗?也许你需要在服务器上打开一些端口。 –
+0
@弗兰克我做了你上面所说的一切。但是我无法打开我的端口进行外部连接。在我使用共享主机之前,出于安全原因无法打开端口。但是现在我正在研究亚马逊ec2虚拟云服务器。这里也是我的应用程序无法打开端口。建议我一些有用的东西...... –
+0
你可能想检查这个答案 - http://stackoverflow.com/a/13054630 –