您的位置:程序门 -> delphi -> 网络通信/分布式开发



delphi中如何远程通讯?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


delphi中如何远程通讯?
发表于:2008-02-22 19:29:37 楼主
delphi中如何远程通讯?
如a.exe在苏州市发送一个字符串给在南京市的b.exe,在delphi中如何实现,不知怎么下手。
发表于:2008-02-22 23:09:041楼 得分:0
有很多控件可以实现的。
你可以研究一下tserversocket/tclientsocket控件(在delphi6的internet页下,delphi7需要自行安装)
发表于:2008-02-22 23:37:462楼 得分:0
要实现这个功能,方法很多,能够用的组件也很多,
但是涉及到的问题也是很多的,
选择   udp   还是   tcp/ip、穿透防火墙、穿透网管、是否需要服务器..........
建议楼主先看看这方面的资料再决定如何下手。
以下是   indy   官方发布,用   udp   实现通讯的   demo,楼主参考一下吧。
1.客户端
{-----------------------------------------------------------------------------
  demo   name:   udp   client
  author:         <unknown   -   please   contact   me   to   take   credit!   -   allen   o'neill>
  copyright:   indy   pit   crew
  purpose:
  history:
  date:             27/10/2002   01:00:36
  checked   with   indy   version:   9.0   -   allen   o'neill   -   springboard   technologies   ltd     -   http://www.springboardtechnologies.com
-----------------------------------------------------------------------------
  notes:

  simple   udp   client   demo

}


unit   udpclientmain;

interface

uses
    windows,   messages,   graphics,   controls,   forms,   dialogs,   idwinsock2,       stdctrls,
    sysutils,   classes,   idbasecomponent,   idantifreezebase,   idantifreeze,
    idcomponent,   idudpbase,   idudpclient,   idstack;

type
    tudpmainform   =   class(tform)
        sourcegroupbox:   tgroupbox;
        hostnamelabel:   tlabel;
        hostaddresslabel:   tlabel;
        hostname:   tlabel;
        hostaddress:   tlabel;
        udpantifreeze:   tidantifreeze;
        portlabel:   tlabel;
        port:   tlabel;
        destinationlabel:   tlabel;
        destinationaddress:   tlabel;
        buffersizelabel:   tlabel;
        buffersize:   tlabel;
        udpmemo:   tmemo;
        sendbutton:   tbutton;
        udpclient:   tidudpclient;
        procedure   formcreate(sender:   tobject);
        procedure   sendbuttonclick(sender:   tobject);
    private
        {   private   declarations   }
    public
        {   public   declarations   }
    end;

var
    udpmainform:   tudpmainform;

implementation

const
    hostnamelength   =   80;
    recievetimeout   =   5000;   //   milliseconds

{$r   *.dfm}

procedure   tudpmainform.formcreate(sender:   tobject);
begin
    randomize;   //   remove   if   you   want   reproducible   results.
    hostname.caption   :=   udpclient.localname;
    hostaddress.caption   :=   gstack.localaddress;
    port.caption   :=   inttostr(udpclient.port);
    destinationaddress.caption   :=   udpclient.host;
    buffersize.caption   :=   inttostr(udpclient.buffersize);
    udpclient.receivetimeout   :=   recievetimeout;
end;

procedure   tudpmainform.sendbuttonclick(sender:   tobject);
var
    messageid:   integer;
    thismessage:   string;
    receivedstring:   string;
begin
    messageid   :=   random(maxint);
    thismessage   :=   'message:   '   +   inttostr(messageid);
    udpmemo.lines.add('sending   '   +   thismessage);
    udpclient.send(thismessage);
    receivedstring   :=   udpclient.receivestring();
    if   receivedstring   =   ''   then
        udpmemo.lines.add('no   response   received   from   the   server   after   '   +   inttostr(udpclient.receivetimeout)   +   '   millseconds.')
    else
        udpmemo.lines.add('received:   '   +   receivedstring)
end;

end.


2.服务器端
{-----------------------------------------------------------------------------
  demo   name:   udp   server
  author:         <unknown   -   please   contact   me   to   take   credit!   -   allen   o'neill>
  copyright:   indy   pit   crew
  purpose:
  history:
  date:             27/10/2002   01:00:36
  checked   with   indy   version:   9.0   -   allen   o'neill   -   springboard   technologies   ltd     -   http://www.springboardtechnologies.com
-----------------------------------------------------------------------------
  notes:

  simple   udp   server   demo

}

unit   udpservermain;

interface

uses
    windows,   messages,   graphics,   controls,   forms,   dialogs,   idwinsock2,   stdctrls,
    sysutils,   classes,   idbasecomponent,   idantifreezebase,   idantifreeze,
    idcomponent,   idudpbase,   idudpclient,   idstack,   idudpserver,   idsockethandle;


type
    tudpmainform   =   class(tform)
        sourcegroupbox:   tgroupbox;
        hostnamelabel:   tlabel;
        hostaddresslabel:   tlabel;
        hostname:   tlabel;
        hostaddress:   tlabel;
        udpserver:   tidudpserver;
        udpantifreeze:   tidantifreeze;
        portlabel:   tlabel;
        port:   tlabel;
        buffersizelabel:   tlabel;
        buffersize:   tlabel;
        udpmemo:   tmemo;
        procedure   formcreate(sender:   tobject);
        procedure   udpserverudpread(sender:   tobject;   adata:   tstream;   abinding:   tidsockethandle);
    private
        {   private   declarations   }
    public
        {   public   declarations   }
    end;

var
    udpmainform:   tudpmainform;

implementation

const
    hostnamelength   =   80;

{$r   *.dfm}

procedure   tudpmainform.formcreate(sender:   tobject);
begin
    hostname.caption   :=   udpserver.localname;
    hostaddress.caption   :=   gstack.localaddress;
    port.caption   :=   inttostr(udpserver.defaultport);
    buffersize.caption   :=   inttostr(udpserver.buffersize);
    udpserver.active   :=   true;
end;

procedure   tudpmainform.udpserverudpread(sender:   tobject;   adata:   tstream;   abinding:   tidsockethandle);
var
    datastringstream:   tstringstream;
    s:   string;
begin
    datastringstream   :=   tstringstream.create('');
    try
        datastringstream.copyfrom(adata,   adata.size);
        udpmemo.lines.add('received   "'   +   datastringstream.datastring   +   '"   from   '   +   abinding.peerip   +   '   on   port   '   +   inttostr(abinding.peerport));
        s   :=   'replied   from   '   +   udpserver.localname   +   '   to   "'   +   datastringstream.datastring   +   '"';
        abinding.sendto(abinding.peerip,   abinding.peerport,   s[1],   length(s));
    finally
        datastringstream.free;
    end;
end;


end.
发表于:2008-02-23 08:43:203楼 得分:0
delphi   7   下有indy   等一系统组件看一下它们的demo
发表于:2008-02-23 13:57:394楼 得分:0
用服务器中转,这样省事些!不用穿透防火墙.
发表于:2008-02-25 14:03:085楼 得分:0
通过协议(tcp/udp)建立连接,用socket编程,发送信息
发表于:2008-02-26 14:21:326楼 得分:0
indy组件比较好


快速检索

最新资讯
热门点击