flex和delphi做的服务器进行通讯
用flex做前台页面,后台使用delphi处理数据,之间用socket连接。。。delphi使用indy的tcpserver。的确很好玩。哈哈。可以不知道那些网页游戏是否是这样做成的。。。先贴个效果图,再贴代码。。很简单的说。

服务端代码:
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPBase, IdUDPServer, IdBaseComponent, IdComponent,
IdTCPServer;
type
TfFlexServerTest = class(TForm)
Memo1: TMemo;
Button1: TButton;
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure log(logInfo:string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fFlexServerTest: TfFlexServerTest;
implementation
{$R *.dfm}
procedure TfFlexServerTest.log(logInfo:string);
begin
Memo1.Lines.Add(logInfo);
end;
procedure TfFlexServerTest.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
log('有人来连接');
end;
procedure TfFlexServerTest.IdTCPServer1Execute(AThread: TIdPeerThread);
var
s:string;
begin
s:=AThread.Connection.ReadLn;
s:=UTF8Decode(s);
log(s);
end;
procedure TfFlexServerTest.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
log('有人离开');
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPBase, IdUDPServer, IdBaseComponent, IdComponent,
IdTCPServer;
type
TfFlexServerTest = class(TForm)
Memo1: TMemo;
Button1: TButton;
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure log(logInfo:string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fFlexServerTest: TfFlexServerTest;
implementation
{$R *.dfm}
procedure TfFlexServerTest.log(logInfo:string);
begin
Memo1.Lines.Add(logInfo);
end;
procedure TfFlexServerTest.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
log('有人来连接');
end;
procedure TfFlexServerTest.IdTCPServer1Execute(AThread: TIdPeerThread);
var
s:string;
begin
s:=AThread.Connection.ReadLn;
s:=UTF8Decode(s);
log(s);
end;
procedure TfFlexServerTest.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
log('有人离开');
end;
end.
客户端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#000000, #FFFFFF]" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.events.MouseEvent;
private var YTsocket:Socket=new Socket();
internal function initApp():void
{
YTsocket.connect("127.0.0.1",1985);
btn1.addEventListener(MouseEvent.CLICK,onClick);
}
internal function onClick(event:MouseEvent):void
{
var msg:ByteArray=new ByteArray();
msg.writeUTFBytes(txt.text+"\n");
//YTsocket.writeBytes(msg);
YTsocket.writeUTFBytes(msg+"\n");
YTsocket.flush();
}
]]>
</mx:Script>
<mx:Panel width="250" height="200" layout="absolute" title="消息发送方" fontSize="15" backgroundAlpha="0.15" alpha="1.0" horizontalCenter="-11" verticalCenter="5">
<mx:Button x="82" y="111" label="发送" id="btn1" fontSize="13"/>
<mx:TextArea height="72" id="txt" horizontalCenter="0" verticalCenter="-31" color="#FA040A"/>
</mx:Panel>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#000000, #FFFFFF]" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.events.MouseEvent;
private var YTsocket:Socket=new Socket();
internal function initApp():void
{
YTsocket.connect("127.0.0.1",1985);
btn1.addEventListener(MouseEvent.CLICK,onClick);
}
internal function onClick(event:MouseEvent):void
{
var msg:ByteArray=new ByteArray();
msg.writeUTFBytes(txt.text+"\n");
//YTsocket.writeBytes(msg);
YTsocket.writeUTFBytes(msg+"\n");
YTsocket.flush();
}
]]>
</mx:Script>
<mx:Panel width="250" height="200" layout="absolute" title="消息发送方" fontSize="15" backgroundAlpha="0.15" alpha="1.0" horizontalCenter="-11" verticalCenter="5">
<mx:Button x="82" y="111" label="发送" id="btn1" fontSize="13"/>
<mx:TextArea height="72" id="txt" horizontalCenter="0" verticalCenter="-31" color="#FA040A"/>
</mx:Panel>
</mx:Application>

浙公网安备 33010602011771号