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
];
}
}
}
查看全文
相关阅读:
xcode swift对应版本
VM12安装OS X10.11步骤及说明
Java开发Webservice的组件
xml文件格式说明
android开发中在界面上实现曲线图的几个开源项目
C#中SQL Server数据库连接池使用及连接字符串部分关键字使用说明
c#对数据库访问完应关闭连接
I2C VHDL程序
数码管一些列功能的verilog实现
PWM控制灯亮暗的verilog实现
原文地址:https://www.cnblogs.com/skyblue/p/1523507.html
最新文章
js 原生 ajax
win下python3安装mysql
php 数字转换为中文
input框设置必填数字
php获取代码总行数
WPF开发日记—解决拖动行为附加到元素上的延迟
结合ItemsControl在Canvas中动态添加控件的最MVVM的方式
我对WPF知识点的理解和梳理笔记
WPF为控件扩展的附加属性不起作用需要注意的地方
Android Studio 1.1的安装和遇到的坑
热门文章
css中float left与float right的使用说明
解决PIL “decoder jpeg not available” 和 “ImportError: The _imaging C module is not installed”的问题
DSOfile,修改windows系统文件摘要
AxWindowsMediaPlayer的详细用法
ssh密钥登录(两种方法)
windows 2008 R2 64位系统,找到Microsoft Excel 应用程序
IOS APP通过Archive上架APPStore的流程
虚拟机安装Linux注意事项
单平台软件开发多系统APP
ios-charts(swift2.3) 引入到自建工程(xcode7.3.1,swift2.3, OC)中
Copyright © 2011-2022 走看看