如何解决Websocket客户端Perl与Node.js
尝试对Perl Websocket客户端脚本进行故障排除。 我已经创建了我认为与Node.js中相同的功能,并且效果很好。 Perl脚本仅在第一次(连接)时起作用,但是在第二次尝试时会引发错误
“无法连接。...无法建立连接,因为 目标机器积极拒绝它” 引用
为什么node.js客户端可以工作,而perl客户端却不能工作? 这是Perl脚本
#!/usr/bin/perl -w
use IO::Socket::INET;
my $addr = '127.0.0.1';
my $port = '8744';
my $i=0;
while (1) {
print "Triggering (count: $i)...\n";
my $sock = IO::Socket::INET->new(PeerAddr => $addr,PeerPort => $port,Proto => 'tcp') or warn("unable to connect to ".$addr.":".$port.":
$!\n");
print $sock "FOO\r\n";
close $sock;
sleep 30;
$i++;
}
这是节点脚本
(function foo() {
var net = require('net');
var client = new net.Socket();
client.connect(8744,'127.0.0.1',function() {
console.log('Connected');
client.write('Hello,server! Love,Client.');
});
client.on('data',function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close',function() {
console.log('Connection closed');
});
setTimeout(foo,30000);
})();
更新。 我将在此处附加我的整个服务器代码。在进一步的调试中,我注意到Perl客户端立即导致在此行的第一个连接上的服务器中引发SocketException错误 while((i = stream.Read(bytes,0,bytes.Length))!= 0)
消息是 重新抛出为IOException。无法从传输连接读取数据。 。
这可能是我问题的根源。
public class TCPServer : MonoBehaviour
{
public bool isConnection,senddata1,senddata2,valuechanged,valuechanged2;
public Thread mThread;
public TcpListener server;
public TcpClient client;
public NetworkStream stream;
byte[] msg1;
string toSend;
bool available,selection,endpointChanged,parsedata;
public String data,prevdata;
int q = 0;
int FPS = 0;
Int32 _port;
private void Awake()
{
UnityThread.initUnityThread();
}
void Start()
{
isConnection = false;
senddata1 = false;
senddata2 = false;
//print ("StartThread");
ThreadStart ts = new ThreadStart(Update1);
mThread = new Thread(ts);
mThread.Start();
}
void Update1()
{
server = null;
try
{
// Set the TcpListener on port 8744.
Int32 port = 8744;
server = new TcpListener(IPAddress.Any,port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Thread.Sleep(10);
UnityThread.executeInUpdate(() =>
{
Debug.Log("Waiting for a connection... ");
});
client = server.AcceptTcpClient();
if (client != null)
{
UnityThread.executeInUpdate(() =>
{
Debug.Log("0 ASSERT TCPServer Connected!");
});
}
data = null;
// Get a stream object for reading and writing
stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes,bytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(bytes,i);
UnityThread.executeInUpdate(() =>
{
Debug.Log("1 ASSERT TCPServer Received: data = " + data);
});
switch (data)
{
case "TRIGGER":
UnityThread.executeInUpdate(() =>
{
Debug.Log("1 ASSERT TRIGGER RECEIVED");
});
break;
case "Disconnect":
goto q;
default:
prevdata = "RECEIVED_NO_TRIGGER";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(prevdata);
// Send back a response.
stream.Write(msg,msg.Length);
break;
}
}
q:
// Shutdown and end connection
client.Close();
}//end while true
}
catch (SocketException e)
{
Debug.LogError("SocketException:" + e);
Debug.LogException( e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。