我有一个简单的nodejs Tcp服务器,它注册每个客户端并将来自客户端的每条消息广播到所有其他客户端。
问题是与silverlight插座连接。当我尝试连接到4530端口中运行nodejs Tcp服务器的公共IP时,我收到一条错误“Access Denied”。
nodejs代码:
net = require('net');
var clients = [];
net.createServer(function (socket) {
socket.name = socket.remoteAddress + ":" + socket.remotePort
clients.push(socket);
socket.on('data', function (data) {
broadcast(data, socket);
});
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
});
function broadcast(message, sender) {
clients.forEach(function (client) {
if (client === sender) return;
client.write(message);
});
}
}).listen(4530);
Silverlight代码:
void Connect(string IP_Address)
{
client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs()
{
RemoteEndPoint = new IPEndPoint(IPAddress.Parse(IP_Address), 4530)
};
socketEventArg.Completed += OnConncetCompleted;
client_socket.ConnectAsync(socketEventArg);
}
void OnConncetCompleted(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
this.Dispatcher.BeginInvoke(new ShowMessagedelegate(ShowMessageBox), "Connceted Successfully!");
this.Dispatcher.BeginInvoke(new Enabledelegate(EnableControl), true);
}
else
{
this.Dispatcher.BeginInvoke(new ShowMessagedelegate(ShowMessageBox), e.SocketError.ToString());
this.Dispatcher.BeginInvoke(new Enabledelegate(EnableControl), false);
}
}
答案 0 :(得分:0)
也许,您在nodejs Tcp服务器的根目录下没有clientaccesspolicy.xml文件。阅读更多:http://msdn.microsoft.com/ru-ru/library/cc645032(v=vs.95).aspx
示例策略文件:
<?xml version="1.0" encoding ="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri="file:///" />
</allow-from>
<grant-to>
<socket-resource port="4530" protocol="tcp" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>