最近在做项目时,遇到一个比较诡异的需求。要求在客户端页面与客户端软件进行通信。网上发现了一个好东西,通过javascipt+flash能实现Socket的功能,具体看如下链接:http://www.maiyun.net/components/socket/index.html,有这么好的已经封装好的Socket类干嘛不用^^
以下为 Asp.net ,c#开发的通讯DEMO。
前台代码:
代码
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
< head runat ="server" >
< script type ="text/javascript" src ="flash.min.js" ></ script >
< script type ="text/javascript" src ="socket.min.js" ></ script >
< script type ="text/javascript" >
function CalltoServer() {
var Socket = new Maiyun_Socket();
Socket.Init(); // 初始化组件
Socket.OnLoad( function () { // 组件初始化成功后触发
Socket.Open( " 127.0.0.1 " , 9010 ); // 连接服务器
Socket.OnConnect( function () { // 连接成功后触发
alert( " 连接成功 " );
Socket.Send( " 来自前台WeB " ); // 发送消息
});
Socket.OnData( function (msg) { // 接收消息
alert(msg);
});
});
}
</ script >
</ head >
< body >
< input type ="button" value ="ClientMe" onclick ="CalltoServer()" />
</ body >
</ html >
< head runat ="server" >
< script type ="text/javascript" src ="flash.min.js" ></ script >
< script type ="text/javascript" src ="socket.min.js" ></ script >
< script type ="text/javascript" >
function CalltoServer() {
var Socket = new Maiyun_Socket();
Socket.Init(); // 初始化组件
Socket.OnLoad( function () { // 组件初始化成功后触发
Socket.Open( " 127.0.0.1 " , 9010 ); // 连接服务器
Socket.OnConnect( function () { // 连接成功后触发
alert( " 连接成功 " );
Socket.Send( " 来自前台WeB " ); // 发送消息
});
Socket.OnData( function (msg) { // 接收消息
alert(msg);
});
});
}
</ script >
</ head >
< body >
< input type ="button" value ="ClientMe" onclick ="CalltoServer()" />
</ body >
</ html >
后台代码:
代码
static
void
Main(
string
[] args)
{
string host = " 127.0.0.1 " ;
int port = 9010 ;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket sc = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sc.Bind(ipe);
sc.Listen( 5 );
while ( true )
{
Socket clientSocket = sc.Accept();
Console.WriteLine( " 建立连接: " + clientSocket.RemoteEndPoint.ToString());
// 接收数据
byte [] buff = new byte [ 1024 ]; // 缓冲区
int count = clientSocket.Receive(buff, buff.Length, 0 ); // 接收的字节
string recstr = Encoding.UTF8.GetString(buff, 0 , count); // byte[]转换成string
Console.WriteLine( " 接受到客户端数据: " + recstr);
// flash socket 安全验证
if (recstr.IndexOf( " <policy-file-request/> " ) >= 0 )
{
// 此处为C#语言的回复
byte [] data = System.Text.Encoding.UTF8.GetBytes( " <?xml version=\ " 1.0 \ " ?><cross-domain-policy><allow-access-from domain=\ " * \ " to-ports=\ " 9010 \ " /></cross-domain-policy>\0 " );
clientSocket.Send(data);
clientSocket.Close();
continue ;
}
// 发送数据
string context = " 我是服务器! " ;
byte [] sendStr = Encoding.UTF8.GetBytes(context);
clientSocket.Send(sendStr);
clientSocket.Close();
Console.WriteLine( " 向客户端发送: " + context);
}
}
{
string host = " 127.0.0.1 " ;
int port = 9010 ;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket sc = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sc.Bind(ipe);
sc.Listen( 5 );
while ( true )
{
Socket clientSocket = sc.Accept();
Console.WriteLine( " 建立连接: " + clientSocket.RemoteEndPoint.ToString());
// 接收数据
byte [] buff = new byte [ 1024 ]; // 缓冲区
int count = clientSocket.Receive(buff, buff.Length, 0 ); // 接收的字节
string recstr = Encoding.UTF8.GetString(buff, 0 , count); // byte[]转换成string
Console.WriteLine( " 接受到客户端数据: " + recstr);
// flash socket 安全验证
if (recstr.IndexOf( " <policy-file-request/> " ) >= 0 )
{
// 此处为C#语言的回复
byte [] data = System.Text.Encoding.UTF8.GetBytes( " <?xml version=\ " 1.0 \ " ?><cross-domain-policy><allow-access-from domain=\ " * \ " to-ports=\ " 9010 \ " /></cross-domain-policy>\0 " );
clientSocket.Send(data);
clientSocket.Close();
continue ;
}
// 发送数据
string context = " 我是服务器! " ;
byte [] sendStr = Encoding.UTF8.GetBytes(context);
clientSocket.Send(sendStr);
clientSocket.Close();
Console.WriteLine( " 向客户端发送: " + context);
}
}
flash 安全验证请查看如下文章:http://cloud21.javaeye.com/blog/617751
结果如下: