首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

问个关于tcp/ip 的线程有关问题。ClientSocket的

2012-02-22 
问个关于tcp/ip 的线程问题。ClientSocket的问个关于tcp/ip的线程问题。ClientSocket的我想在接受的地方也使

问个关于tcp/ip 的线程问题。ClientSocket的
问个关于tcp/ip   的线程问题。ClientSocket的     我想在接受的地方也使用线程     ,但是不好使     不知道为什么   ,请大家帮忙看下,下面的代码能正常使用   ,就是接受数据的地方   我想不使用主线程
unit   Unit1;

interface

uses
    Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,
    Dialogs,   ScktComp,   StdCtrls;

type
    TForm1   =   class(TForm)
        Memo1:   TMemo;
        procedure   MySocketRead(Sender:   TObject;Socket:   TCustomWinSocket);
        procedure   MySocketError(Sender:   TObject;
    Socket:   TCustomWinSocket;   ErrorEvent:   TErrorEvent;
    var   ErrorCode:   Integer);
        procedure   FormCreate(Sender:   TObject);
    private
        {   Private   declarations   }
    public
        {   Public   declarations   }
    end;


      //定义线程
    TclentThread   =   class(TThread)
      protected
          procedure   Execute;   override;

      end;
var
    Form1:   TForm1;
    client:Tclientsocket;
    SocketThread:TclentThread;
implementation

{$R   *.dfm}
procedure   TclentThread.Execute;

begin

    sleep(2000);
    if   not   client.Active   then
        begin
            form1.Memo1.Lines.Add( '连不上 ');
            client.Open;
        end;
        client.Socket.SendText( 'ddd ');
    ///////////////////////
    execute;
end;
procedure   TForm1.MySocketError(Sender:   TObject;
    Socket:   TCustomWinSocket;   ErrorEvent:   TErrorEvent;
    var   ErrorCode:   Integer);
begin
      //   StatusBar1.Panels[1].Text   :=   '系统联机错误 ';
        Socket.Close;
        errorcode:=0;
end;


procedure   TForm1.MySocketRead(Sender:   TObject;
    Socket:   TCustomWinSocket);
begin
    form1.memo1.Lines.Add( 'ffff '+socket.ReceiveText);
end;

procedure   TForm1.FormCreate(Sender:   TObject);
begin
      client:=Tclientsocket.Create(form1);
      client.Address:= '127.0.0.1 ';
      client.Port:=10040;
      client.Open;
      client.OnRead:=MySocketRead;
      client.OnError:=MySocketError;
      SocketThread   :=   TclentThread.Create(False);
end;

end.

[解决办法]
form1.Memo1.Lines.Add( '连不上 '); //这一句就线程序不安全了吧!
[解决办法]
你的线程操作中没有任何的循环,仅是一个OPEN连接,这根本就不需要用线程.


------解决方案--------------------


主要费时的地方是接收到数据后对数据的处理.

如果你的数据处理很慢或很多用户发来数据可以考虑用线程.


[解决办法]
用indy吧,不要用ClientSocket

热点排行