推荐你用Indy,直接有函数SendString,给你看我用Indy传送文件的例子
使用Indy的Stream方式在网络中间传递文件 
       Indy的使用TCP协议的控件都是基于TIdConnection的,而TIdConnection有一个方法就是使用stream来发送和接收数据,下面的例子我们建立了一个服务器程序和一个客户端程序,当客户端程序连接到服务器之后,服务器把预选选择的文件的名字发送给客户程序,然后把文件的内容发送给客户端,客户端程序收到数据之后按照开始收到的文件名保存为一个文件。 
       我们新建一个Application,在窗体上放入一个Memo,三个按钮,分别是"Select File"、"Start Server"和"Clear",再放入一个OpenFileDialog组件,这就是服务器端的窗体设计。我们再新建一个Application,放入一个ListView,一个Edit和一个按钮"Connect",这就是客户端的窗体设计。 
       下面是服务器端的代码: 
        
*************************************************************** 

* Project  : Server 
* Unit Name: ServerMain 
* Purpose  : Demonstrates basic use of IdTCPServer 
* Date  : 16/01/2001  -  03:19:36 
* History  : 

****************************************************************} unit ServerMain; interface uses 
 SysUtils, Classes, graphics, controls, FORMs, dialogs, 
 IdBaseComponent, IdComponent, IdTCPServer, StdCtrls, ExtCtrls, 
 IdAntiFreezeBase, IdAntiFreeze, Buttons; type 
 TfrmServer = class(TFORM) 
   TCPServer: TIdTCPServer; 
   Panel1: TPanel; 
   Memo1: TMemo; 
   IdAntiFreeze1: TIdAntiFreeze; 
   SpeedButton1: TSpeedButton; 
   SpeedButton2: TSpeedButton; 
   OpenDialog: TOpenDialog; 
   SpeedButton3: TSpeedButton; 
   procedure TCPServerExecute(AThread: TIdPeerThread); 
   procedure SpeedButton1Click(Sender: TObject); 
   procedure SpeedButton2Click(Sender: TObject); 
   procedure SpeedButton3Click(Sender: TObject); 
 private 
 public 
 end; var 
 frmServer: TfrmServer; 
 SendFileName: string; implementation 
{$R *.DFM} procedure TfrmServer.TCPServerExecute(AThread: TIdPeerThread); 
var 
 SendFile: TFileStream; begin 
 with AThread.Connection do 
 begin 
   Memo1.Lines.Add('Sending file '+SendFileName);//在Memo里添加要发送的文件名称    WriteLn(SendFileName);//把文件名发送给客户端    SendFile := TFileStream.Create(SendFileName, fmOpenRead);//创建一个TFileStream,打开我们要发送的文件    WriteStream(SendFile);//使用流模式把文件发送到客户端    Memo1.Lines.Add('Total ' + IntToStr(SendFile.Size) + ' Bytes sent');//显示总共发送的字节数 
   Disconnect;//断开连接    SendFile.Free;//释放流对象  end; 
end; procedure TfrmServer.SpeedButton1Click(Sender: TObject); 
begin 
 Memo1.Lines.Clear; 
end; procedure TfrmServer.SpeedButton2Click(Sender: TObject); 
begin 
 if OpenDialog.Execute then//执行文件选择标准对话框,选择要发送的文件  begin 
   SendFileName := OpenDialog.Filename;//文件名赋值给SendFileName变量  end; 
end; procedure TfrmServer.SpeedButton3Click(Sender: TObject); 
begin 
 TCPServer.Active := True;//启动服务器  SpeedButton3.Enabled := False; 
end; end.         
      整个流程是先点击"Select File"选择一个文件,然后点击"Start Server"启动服务器,等待客户端的连接,当有客户端程序连接,就先发送文件名称,然后发送文件内容,以后如果要发送新的文件,只需要重新选择文件即可。下面是客户端实现的代码: 
      
{*************************************************************** 

* Project  : Client 
* Unit Name: ClientMain 
* Purpose  : Demonstrates basic interaction of IdTCPClient with server 
* Date  : 16/01/2001  -  03:21:02 
* History  : 

****************************************************************} unit ClientMain; interface uses 
{$IFDEF Linux} 
 QFORMs, QGraphics, QControls, QDialogs, QStdCtrls, QExtCtrls, 
{$ELSE} 
 windows, messages, graphics, controls, FORMs, dialogs, stdctrls, extctrls, 
{$ENDIF} 
 SysUtils, Classes, 
 IdBaseComponent, 
 IdComponent, IdTCPConnection, IdTCPClient, ComCtrls, IdAntiFreezeBase, 
 IdAntiFreeze, Buttons; type 
 TFORM2 = class(TFORM) 
   TCPClient: TIdTCPClient; 
   pnlTop: TPanel; 
   btnGo: TButton; 
   lstMain: TListBox; 
   Edit1: TEdit; 
   UpDown1: TUpDown; 
   Edit2: TEdit; 
   IdAntiFreeze1: TIdAntiFreeze; 
   SpeedButton1: TSpeedButton; 
   procedure btnGoClick(Sender: TObject); 
   procedure SpeedButton1Click(Sender: TObject); 
 private 
 public 
 end; var 
 FORM2: TFORM2; 
implementation 
{$IFDEF Linux}{$R *.xfm}{$ELSE}{$R *.DFM}{$ENDIF} 
// Any data received from the client is added as a text line in the ListBox 
procedure TFORM2.btnGoClick(Sender: TObject);//点击了Go按钮,连接到服务器 var 
 ReadFile: TMemoryStream; 
 RecevFileName: string; 
begin 
 TCPClient.Host := Edit2.Text;//服务器的地址为Edit2的内容(端口我们已经指定为8090,这个是在属性面板里面设置的  )  with TCPClient do 
 begin 
   Connect;//连接到服务器    while Connected do 
   begin 
     ReadFile := TMemoryStream.Create;//创建一个TMemoryStram对象      try 
       RecevFileName := ReadLn;//从服务器端获得文件名        lstMain.Items.Add('Receving file ' + RecevFileName);//显示到ListView中        ReadStream(ReadFile, -1, True);//以流模式从服务器端获得文件内容,参数分别是ReadFile表示流对象,-1表示一直读取直到对方断开,True表示在NT操作系统下提高性能,对9x无效        lstMain.items.Add('Total ' + IntToStr(ReadFile.Size) + ' Bytes recevied');//显示总共接收到多少数据        ReadFile.Seek(0, soFromBeginning);//定位流指针到开始        ReadFile.SaveToFile(ExtractFileName(RecevFileName));//把流中的内容保存到文件中去      finally 
       Disconnect;//断开连接        ReadFile.Free;//释放流对象      end; 
   end; 
 end; 
end; procedure TFORM2.SpeedButton1Click(Sender: TObject); 
begin 
 lstMain.Items.Clear; 
end; end. 
      
      
     注释已经写得很清楚了,大家自己看吧。     
 

解决方案 »

  1.   

    用nmudp只要在ip层上通信是很简单的,只要send、read即可了ExampleTo recreate this example, you will need to create a new blank Delphi application.Place 2 TMemos, a TButton, and a TNMUDP on the form.Memo1: Window for receiving data
    Memo2: Status window
    Button1: Sends UDP Data
    NMUDP1: client and server for sending and receiving dataInsert the following code into Button1's OnClick event:procedure TForm1.Button1Click(Sender: TObject);
    var
      C: Array [1..3] of Char;
    begin
      C := 'cat';
      NMUDP1.RemoteHost := '127.0.0.1';
      NMUDP1.ReportLevel := Status_Basic;
      NMUDP1.LocalPort := 6668;
      NMUDP1.RemotePort := 6668;
      NMUDP1.SendBuffer(C, 3);
    end;When Button1 is clicked, C (a variable of an array of characters, 3 to be exact) is filled with the value cat. The RemoteHost property is set to 127.0.0.1, which is the IP address for local host. This could just as easily be the host name or IP address of a remote computer as well. The ReportLevel property is set to Status_Basic, to provide only basic status messages in the OnStatus event. The LocalPort property is set to 6668, so that any data sent to the computer running this application on port 6668 will be received by this component. The remote port property is also set to 6668, for sending data to port 6668 of 127.0.0.1 (basically, sending data to itself). The buffer C is now sent using the SendBuffer method.Insert the followint code into NMUDP1's OnBufferInvalid event:procedure TForm1.NMUDP1BufferInvalid(var handled: Boolean; var Buff: array of Char; var length: Integer);
    begin
      ShowMessage('Buffer Invalid: Buffer contains no data');
    end;When the OnInvalidBuffer event is called, a message is displayed to the user informing them that the buffer being sent is invalid because it contains no data. This error could be corrected by modifying the Buff parameter so it contains the data to be sent, and the length parameter to contain the length of the Buffer. The handled property would then have to be set to TRUE to allow the component to send the data again.Insert the following code into NMUDP1's OnDataReceived event:procedure TForm1.NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
    var
      C: array [1..3] of Char;
      I: Integer;
    begin
      if NumberBytes <= 3 then
      begin
        NMUDP1.ReadBuffer(C, I);
        Memo1.Lines.Add(C+': received '+IntToStr(I)+' bytes from '+FromIP+' on port '+IntToStr(Port));
      end
      else
        Memo1.Lines.Add(IntToStr(I)+' bytes incoming, buffer too small');
    end;When data is received by NMUDP1, if the NumberBytes parameter is 3 or less (3 or les bytes), the data is read into an array of characters (C) by the ReadBuffer method and displayed in Memo1, along with how many bytes were actually read, the IP address of the computer sending the data (FromIP), and the port the data was sent from (
    Port parameter). If there are more than 3 bytes, Memo1 is updated to inform the user that the incoming data was too large for the supplied buffer.Insert the following code into NMUDP1's OnDataSend event:procedure TForm1.NMUDP1DataSend(Sender: TObject);
    begin
      Memo2.Lines.Add('Data sent');
    end;When data has been successfully sent by pressing Button1, the OnDataSend event is called, and adds a status line stating the data was sent to Memo2.
    Insert the following code into NMUDP1's OnStatus event:procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
    begin
      Memo2.Lines.Add(status);
    end;When a status message is received, the OnStatus event adds the current status string to Memo2.
    Insert the following code into NMUDP1's OnInvalidHost event:procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
    var
      S: String;begin
      S := NMUDP1.RemoteHost;
      if InputQuery('Invalid host', 'Specify valid hostname: ', S) then
      begin
        NMUDP1.RemoteHost := S;
        handled := TRUE;
      end;
    end;When the host name specified to send data to is an invalid host name or IP address, the OnInvalidHost event is called. In this instance, the InputQuery function gives the user the opportunity to correct the invalid name. If the user clicks the Ok button, the host name entered is set as the host to send data to, and the handled parameter is set to true, which allows the component to attempt the action again. If the user clicks the Cancel button, the host is not changed, and handled remains false, raising an exception.Example Description:
    This simple example sends data to itself using a single TNMUDP component that acts as both a client and a server. When Button1 gets clicked, the data is sent to the local machine. The data is then received by the same component, and manipulated accordingly.
      

  2.   

    用Tserversocket+tclientsocket
    delphi安装目录\demos\internet\chat下有例子。
      

  3.   

    用Tserversocket+tclientsocket
    delphi安装目录\demos\internet\chat下有例子。
      

  4.   

    用SOCKET中的CLIENTSOCKET和SERVERSOCKET,SENDTEXT和RECEIVETEXT
      

  5.   

    我使用的是mailslots
    这个是局域网一对多的广播
    你可以下一个控件
    网上有很多