0
以下是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();
你設法尚未解決這個問題?也許你已經做到了,但如果沒有,你能否解釋一下你如何設置GPS設備?您使用哪些命令來設置服務器的主機名/ IP和端口? 你在同一臺機器上測試了你的perl客戶端嗎?也許你需要在服務器上打開一些端口。 – sourcx
@弗蘭克我做了你上面所說的一切。但是我無法打開我的端口進行外部連接。在我使用共享主機之前,出於安全原因無法打開端口。但是現在我正在研究亞馬遜ec2虛擬雲服務器。這裏也是我的應用程序無法打開端口。建議我一些有用的東西...... – Choxx
你可能想檢查這個答案 - http://stackoverflow.com/a/13054630 – Satyajit