想写一个类实现客户端向服务器发送/接收消息,其中包括客户端的主机IP,主机名,发送/接收消息的时间等信息.
现在定义了两个procedure: SendMsg(AMsg: string); ReceiveMsg(AMsg: string);
想用TIdTcpClient/Server或TTcpClient/Server来实现,但我刚刚接触delphi,感觉一头雾水的,不知道怎么用这些,还请高手指点.别见笑!呵呵

解决方案 »

  1.   

    给你一段代码,传输文件的。
    服务端:
    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
        iFileHandle:integer;
        iFileLen,cnt:integer;
        buf:array[0..4096] of byte;
    begin
        iFileHandle:=FileOpen('E:\setup\qq2004sp1.exe',fmOpenRead);
        iFileLen:=FileSeek(iFileHandle,0,2);
        FileSeek(iFileHandle,0,0);
        AThread.Connection.WriteInteger(iFileLen);
        while true do
        begin
            cnt:=FileRead(iFileHandle,buf,4096);
            AThread.Connection.WriteBuffer(buf,cnt);
            if cnt<4096 then
                break;
        end;
        FileClose(iFileHandle);
    end;客户端:
    procedure TForm1.Button1Click(Sender: TObject);
    var
        rbyte:array[0..4096] of byte;
        sFile:TFileStream;
        iFileSize:integer;
    begin
        try
            IdTcpClient1.Connect(5000);
        except
            exit;
        end;    iFileSize:=IdTCPClient1.ReadInteger;    sFile:=TFileStream.Create('e:\bb.tmp',fmCreate);
        While iFileSize>4096 do
        begin
            IdTCPClient1.ReadBuffer(rbyte,4096);// .ReadBuffer(rbyte,iLen);
            sFile.Write(rByte,4096);
            inc(iFileSize,-4096);
        end;
        IdTCPClient1.ReadBuffer(rbyte,iFileSize);// .ReadBuffer(rbyte,iLen);
        sFile.Write(rByte,iFileSize);
        sFile.Free;
        ShowMessage('file get ok!');
    end;
      

  2.   

    我的想法是:
    当向server传送消息时,SendMessage(AMsg:String)这个方法,在一个参数AMsg的情况下,能把那些IP,Name等传给server吗?要能的话,该怎样去实现呢?是不是要多几个参数出来?
    说明:这些方法不是我定义的,挺茫然。我很菜,所以要向高手请教,不胜感激!!!
      

  3.   

    如果用一个参数,你定义个格式就可以了。
    譬如下面的格式:
    ***.***.***.**;name;info
    接收信息的时候,分析一下就能够得到信息了。不过没必要的。接收的时候能够得到源地址的。
      

  4.   

    wjw1224(风影) ( ) 信誉:100 这个没什么可解释的,只要知道要用到哪几个函数就行了。多看看帮助。
      

  5.   

    to gzmhero(hihihi) :
        问一下,怎么在接收的时候得到源地址IP?