继一篇的Flex与Dotnet 之 WebService这一篇主要是写一个例子基于socket的Flex与Dotnet通讯。
很简单,两段代码。
C#代码Socket服务器
using
System;
using
System.IO;
using
System.Net;
using
System.Net.Sockets;
using
System.Text;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
MyTcpListener
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
class MyTcpListener
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TcpListener server = null;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
server = new TcpListener(localAddr, port);
server.Start();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Byte[] bytes = new Byte[256];
String data = null;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (true)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.Write("等待连接");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("连接中");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
data = null;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NetworkStream stream = client.GetStream();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int i;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
data = System.Text.Encoding.GetEncoding("gb2312").GetString(bytes, 0, i);
Console.WriteLine("收: {0}", data);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
data = data.ToUpper();
byte[] msg = System.Text.Encoding.GetEncoding("gb2312").GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("发: {0}", data);
}
client.Close();
}
}
catch (SocketException e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("异常: {0}", e);
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
server.Stop();
}
Console.WriteLine("回车继续");
Console.Read();
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
Flex代码客户端
<?
xml version="1.0" encoding="utf-8"
?>
<
mx:Application
xmlns:mx
="http://www.adobe.com/2006/mxml"
layout
="absolute"
>
<
mx:Script
>
<!
[CDATA[
import SocketTest.CustomSocket;
private var socket:Socket;
private function StartTest():void
{
socket = new Socket("127.0.0.1",13000);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
}
private function CloseTest():void
{
if(socket!=null && socket.connected)
{
trace("socket.connected:"+socket.connected);
socket.close();
trace("socket.connected:"+socket.connected);
}
}
private function connectHandler(event:Event):void
{
trace("connectHandler: " + event);
socket.writeMultiByte(SendStr.text,"GB2312");
socket.flush();
}
private function socketDataHandler(event:ProgressEvent):void
{
trace("socketDataHandler: " + event);
var str:String=socket.readMultiByte(socket.bytesAvailable,"GB2312");
ReceiveStr.text = str;
}
]]
>
</
mx:Script
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
mx:Text
text
="发送"
y
="11"
x
="18"
fontWeight
="bold"
color
="#ffffff"
/>
<
mx:TextArea
id
="ReceiveStr"
x
="53"
y
="68"
/>
<
mx:Text
text
="接收"
y
="69"
x
="18"
fontWeight
="bold"
color
="#ffffff"
/>
<
mx:TextArea
id
="SendStr"
y
="10"
x
="53"
/>
<
mx:Button
label
="Close"
click
="CloseTest()"
x
="126"
y
="120"
/>
<
mx:Button
label
="Start"
click
="StartTest()"
x
="53"
y
="120"
/>
</
mx:Application
>
很简单,但是做为Flex与Dotnet交互还是很常用的。