zoukankan
html css js c++ java
Flex Socket 与 C# 通信
Flex
<?
xml version="1.0" encoding="utf-8"
?>
<
mx:Application
xmlns:mx
="http://www.adobe.com/2006/mxml"
layout
="absolute"
>
<
mx:Script
>
<![CDATA[
import mx.controls.Alert;
/////////////////////////////////
//private member
private var xmlsocket:XMLSocket= new XMLSocket();
private var alert:Alert;
private var tmpStr:String;
/////////////////////////////////
//连接服务器
private function ConncetServer():void
{
xmlsocket.addEventListener(DataEvent.DATA,OnRecived);
xmlsocket.addEventListener(Event.CONNECT , onConnected);
this.xmlsocket.connect(this.txtIP.text,int(this.txtPort.text));
}
private function onConnected(evt:Event):void
{
xmlsocket.send(this.txtTrueName.text+" has connected");
this.txtContent.text=this.txtContent.text+"连接成功\n";
}
//发送信息
private function Send():void
{
this.xmlsocket.send(this.txtTrueName.text+"|"+this.txtSendContent.text+"\n");
}
//接收数据
private function OnRecived(event:DataEvent):void
{
trace("等待读取数据信息");
tmpStr = event.text;
//alert=Alert.show("收到新消息","消息");
this.txtContent.text=this.txtContent.text+"\n"+tmpStr;
}
]]>
</
mx:Script
>
<
mx:Panel
layout
="absolute"
right
="0"
left
="0"
top
="0"
bottom
="0"
horizontalAlign
="left"
>
<
mx:TextInput
cornerRadius
="20"
themeColor
="#0D79BE"
borderColor
="#070D04"
borderStyle
="inset"
borderThickness
="1"
id
="txtIP"
text
="192.168.1.55"
left
="88"
top
="23"
/>
<
mx:TextInput
cornerRadius
="20"
themeColor
="#0D79BE"
borderColor
="#070D04"
borderStyle
="inset"
borderThickness
="1"
width
="94"
id
="txtPort"
text
="9992"
left
="293"
top
="23"
/>
<
mx:TextInput
cornerRadius
="20"
themeColor
="#0D79BE"
borderColor
="#070D04"
borderStyle
="inset"
borderThickness
="1"
width
="94"
id
="txtTrueName"
text
="小虎"
left
="432"
top
="23"
/>
<
mx:Label
text
="服务器IP"
fontSize
="12"
left
="15"
top
="23"
/>
<
mx:Label
text
="姓名"
fontSize
="12"
left
="395"
top
="23"
/>
<
mx:Label
text
="端口"
fontSize
="12"
left
="256"
top
="23"
/>
<
mx:Button
label
="登录"
fontSize
="12"
id
="btnLogin"
left
="552"
top
="23"
click
="ConncetServer()"
/>
<
mx:Button
label
="发送"
fontSize
="12"
id
="btnSend"
left
="552"
top
="68"
click
="Send();"
/>
<
mx:TextInput
borderColor
="#04010C"
backgroundColor
="#EEDBFC"
width
="516"
borderStyle
="inset"
cornerRadius
="20"
alpha
="1.0"
id
="txtSendContent"
left
="10"
top
="70"
/>
<
mx:TextArea
borderColor
="#010A10"
id
="txtContent"
left
="10"
top
="110"
right
="10"
bottom
="10"
backgroundColor
="#DEEEF3"
cornerRadius
="12"
/>
</
mx:Panel
>
</
mx:Application
>
C#
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Net;
using
System.Net.Sockets;
using
System.Threading;
namespace
Server
{
public
partial
class
Form1 : Form
{
private
bool
ServiceStartFlag
=
false
;
private
Socket m_Socket;
NetworkStream networkStream;
Thread m_Thread;
public
delegate
void
HelpHandler(
string
str);
public
Form1()
{
InitializeComponent();
btnStart.Enabled
=
true
;
btnStop.Enabled
=
false
;
}
private
void
btnStart_Click(
object
sender, EventArgs e)
{
int
Port
=
int
.Parse(txtPort.Text);
m_Socket
=
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress LocalServerIP
=
GetIPAddress();
IPEndPoint LocalIPEndPoint
=
new
IPEndPoint(LocalServerIP, Port);
m_Socket.Bind(LocalIPEndPoint);
m_Socket.Listen(
600
);
m_Thread
=
new
Thread(
new
ThreadStart(AcceptClient));
m_Thread.IsBackground
=
true
;
m_Thread.Start();
}
private
void
AcceptClient()
{
ServiceStartFlag
=
true
;
while
(ServiceStartFlag)
{
try
{
Socket newSocket
=
m_Socket.Accept();
string
onemessge
=
"
<cross-domain-policy><allow-access-from domain=\
""
+
"
*
"
+
"
\
"
to-ports=\
""
+ txtPort.Text +
"
\
"
/></cross-domain-policy>\0
"
;
byte
[] tmpBytes
=
Encoding.UTF8.GetBytes(onemessge);
newSocket.Send(tmpBytes);
Thread newThread
=
new
Thread(
new
ParameterizedThreadStart(ReadMsg));
newThread.IsBackground
=
true
;
object
obj
=
newSocket;
newThread.Start(obj);
}
catch
(SocketException ex)
{
}
}
}
private
void
ReadMsg(
object
obj)
{
Socket socket
=
(Socket)obj;
HelpHandler rHandler
=
new
HelpHandler(ReceivedMsg);
byte
[] byteMessage
=
null
; ;
while
(ServiceStartFlag)
{
try
{
if
(socket.Connected)
{
byteMessage
=
new
byte
[
1000
];
int
len
=
socket.Receive(byteMessage);
if
(len
>
0
)
{
string
sTime
=
DateTime.Now.ToShortTimeString();
string
msg
=
sTime
+
"
:
"
+
"
Message from:
"
;
msg
+=
socket.RemoteEndPoint.ToString()
+
Encoding.UTF8.GetString(byteMessage);
BeginInvoke(rHandler,
new
object
[]
{ msg }
);
byteMessage
=
null
;
byte
[] tmpBytes
=
Encoding.UTF8.GetBytes(
"
Sended Sucessed!\0
"
);
socket.Send(tmpBytes);
}
}
}
catch
(SocketException ex)
{
BeginInvoke(rHandler,
new
object
[]
{ ex.ToString() }
);
}
}
}
private
void
ReceivedMsg(
string
str)
{
this
.listBox1.Items.Add(str);
}
private
void
btnStop_Click(
object
sender, EventArgs e)
{
}
private
IPAddress GetIPAddress()
{
IPHostEntry ieh
=
Dns.GetHostByName(Dns.GetHostName());
return
ieh.AddressList[
0
];
}
}
}
查看全文
相关阅读:
iPhone页面的常用调试方法
前端代码相关规范
使用BEM命名规范来组织CSS代码
安卓微信页面的调试
前端调试的那些手段
Webpack打包构建太慢了?试试几个方法
[前端] 记录工作中遇到的各种问题(Bug,总结,记录)
jqPlot图表插件学习之饼状图和环状图
jqPlot图表插件学习之阴阳烛图
jqPlot图表插件学习之数据节点高亮和光标提示
原文地址:https://www.cnblogs.com/skyblue/p/1523507.html
最新文章
不可思议的纯CSS导航栏下划线跟随效果
区块链 PoW 与 PoS 的纷争
不可思议的混合模式 background-blend-mode
两行 CSS 代码实现图片任意颜色赋色技术
【前端性能】Web 动画帧率(FPS)计算
【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序
盒子端 CSS 动画性能提升研究
你所不知道的 CSS 滤镜技巧与细节
Oracle 12c 建表空间语句
Oracle 12c 操作 CDB PDB
热门文章
Oracle 12c Lnux 启动脚本
JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
深入理解JVM—JVM内存模型
EL
CSS div
jdbc oracle clob
Oracle 12c pdb自动启动
oracle 绑定变量
趁webpack5还没出,先升级成webpack4吧
前端代码乱糟糟?是时候引入代码质量检查工具了
Copyright © 2011-2022 走看看