ServerSocket的代码
Unit main;InterfaceUses
  Windows, SysUtils, Messages, Classes, Forms, ScktComp, Controls, StdCtrls,
  Menus, Mask, Spin, ComCtrls, ExtCtrls;Const
  CM_IncCount = WM_USER + 1;Type
  TForm1 = Class(TForm)
    ServerSocket: TServerSocket;
    MainMenu: TMainMenu;
    File1: TMenuItem;
    ActiveItem: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    CacheEdit: TSpinEdit;
    Label2: TLabel;
    PortEdit: TSpinEdit;
    Label3: TLabel;
    ThreadCount: TEdit;
    Panel2: TPanel;
    ListBox1: TListBox;
    Panel3: TPanel;
    StatusBar1: TStatusBar;
    CharCount: TLabel;
    Memo1: TMemo;
    Procedure ServerSocketGetThread(Sender: TObject;
      ClientSocket: TServerClientWinSocket;
      Var SocketThread: TServerClientThread);
    Procedure FormCreate(Sender: TObject);
    Procedure FormClose(Sender: TObject; Var Action: TCloseAction);
    Procedure Exit1Click(Sender: TObject);
    Procedure PortEditChange(Sender: TObject);
    Procedure ActiveItemClick(Sender: TObject);
    Procedure ServerSocketThreadEnd(Sender: TObject;
      Thread: TServerClientThread);
    Procedure ServerSocketThreadStart(Sender: TObject;
      Thread: TServerClientThread);
    Procedure CacheEditChange(Sender: TObject);
  protected
    Procedure CMIncCount(Var Msg: TMessage); message CM_IncCount;
  public
  End;{ TFileServerThread }  TFileServerThread = Class(TServerClientThread)
  public
    Procedure ClientExecute; override;
  End;Var
  Form1: TForm1;Implementationuses main;{$R *.DFM}{ TFileServerThread }Procedure TFileServerThread.ClientExecute;
Var
  Data: Array[0..1023] Of char;
  RecText: String;
  SocketStream: TWinSocketStream;
Begin
  While Not Terminated And ClientSocket.Connected Do
  Try
    SocketStream := TWinSocketStream.Create(ClientSocket, 30000);
    Try
      FillChar(Data, SizeOf(Data), 0);
      If SocketStream.Read(Data, SizeOf(Data)) = 0 Then
      Begin
        // If we didn't get any data after xx seconds then close the connection
        ClientSocket.SendText('Timeout on Server'+#13#10);
        //Wait a little time to allow sending of text before disconnect
        sleep(1);
        ClientSocket.Close;
        Terminate;
      End;
      RecText := Data;
      If Length(RecText) > 2 Then
        Delete(RecText, Pos(#13#10, RecText), 2); // Delete #13#10
      If ClientSocket.Connected Then
      Begin
        ClientSocket.SendText(RecText);        Form1.Memo1.Lines.Insert(0,RecText);
      //  SendMessage(Form1.Listbox1.Handle, LB_ADDSTRING, 0, Integer(PChar(RecText)));
      //  PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0);
      End;
    Finally
      SocketStream.Free;
    End;
  Except
    HandleException;
  End;
End;Procedure TForm1.ServerSocketGetThread(Sender: TObject;
  ClientSocket: TServerClientWinSocket;
  Var SocketThread: TServerClientThread);
Begin
  // Create a new thread for connection
  SocketThread := TFileServerThread.Create(False, ClientSocket);
 // ClientSocket.SendText('Welcome to Server'+#13#10);
End;Procedure TForm1.FormCreate(Sender: TObject);
Begin
  CacheEdit.Value := ServerSocket.ThreadCacheSize;
  PortEdit.Value := ServerSocket.Port;
  CharCount.Caption := '0';
  ActiveItemClick(Nil);
End;Procedure TForm1.FormClose(Sender: TObject; Var Action: TCloseAction);
Begin
  ServerSocket.Close;
End;Procedure TForm1.CMIncCount(Var Msg: TMessage);
Begin
  CharCount.Caption := IntToStr(StrToInt(CharCount.Caption) + 1);
End;Procedure TForm1.Exit1Click(Sender: TObject);
Begin
  Close;
End;Procedure TForm1.PortEditChange(Sender: TObject);
Begin
  ServerSocket.Port := StrToInt(PortEdit.Text);
End;Procedure TForm1.ActiveItemClick(Sender: TObject);
Begin
  ServerSocket.Active := Not ServerSocket.Active;
  ActiveItem.Checked := ServerSocket.Active;
  If ServerSocket.Active Then
    StatusBar1.SimpleText := 'Active'
  Else
    StatusBar1.SimpleText := 'InActive';
End;Procedure TForm1.ServerSocketThreadEnd(Sender: TObject;
  Thread: TServerClientThread);
Begin
  ThreadCount.Text := IntToStr(StrToInt(ThreadCount.Text) - 1);
End;Procedure TForm1.ServerSocketThreadStart(Sender: TObject;
  Thread: TServerClientThread);
Begin
  ThreadCount.Text := IntToStr(StrToInt(ThreadCount.Text) + 1);
End;Procedure TForm1.CacheEditChange(Sender: TObject);
Begin
  ServerSocket.ThreadCacheSize := CacheEdit.Value;
End;End.我就是想在ClientSocket1的
procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
begin
     这里接收数据然后 通过“ServerSocketGetThread” 这里的发给和“ServerSocketGetThread“连接的用户  该怎么写,在
TFileServerThread.ClientExecute;
里 用”ClientSocket.SendText(RecText);
“ 能发给用户  在这里该怎样发呢 ,请大家帮忙
end;

解决方案 »

  1.   

    看看demo,不要把整个文件都发上来,看的头晕
      

  2.   

    看一下Demo,你问的问题没听明白。
      

  3.   

    让大家没有看明白。我这么说吧  有个(服务端)线程 接收很多客户端的信息。
    然后还有个(客户端)接收从别的地方传来的数据,我想把这些数据 转发给 这个(服务端)上的所有用户,该怎样写这个代码
    对于在线程本身来说 在线程里ClientSocket.SendText(“RecText”); 这样写就可以了,但是在线程外面 该怎么写,谢谢各位了
      

  4.   

    奥  你是不是想把客户端接收过来的数据发送给在线的客户端??
    如果这样,你服务端要做一个线程池来保存你的连接信息,包括Handle如:type
      TlistClient = class(TObject)
        Name        : String;
        PeerIP      : String;
        Ontime      : String;
        Thread      : Pointer;
      end;用一个TList来保存TlistClient 数据。
    myList:TList;发送的时候循环TList信息,查找要发送给哪个客户端发送代码:
    var
      iClient:TlistClient;
        iClient:= myList.Items[0];
    TIdPeerThread(iClient.Thread).Connection.WriteBuffer(DataBuff[0],DataLen,true);
    在Connect(AThread: TIdPeerThread)中
    iClient.Thread:=AThread;
    不知道你说的是不是这个意思。
      

  5.   

    服务器端:procedure TForm1.ServerSocket1Accept(Sender: TObject;
      Socket: TCustomWinSocket);
    var
      newitem:TListItem;
      ii,jj:Integer;
      user,usercur:string;
    begin
      newitem:=ListView1.Items.Insert(ListView1.Items.Count);
      newitem.Caption:='Client:'+IntToStr(ListView1.Items.Count);
      newitem.SubItems.Add(Socket.RemoteHost);
      newitem.SubItems.Add(Socket.RemoteAddress);
      newitem.SubItems.Add(IntToStr(Socket.RemotePort)); //将远程新登录客户显示到列表
      //ListView1.AlphaSort();
      ListView1.Items.Item[ListView1.Items.Count-1].Selected:=true;//选中添加的客户
      Display('连接到主机:'+Socket.RemoteAddress+' 成功');
      ii:=ListView1.ItemIndex;
        Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
        ListView1.Items[ii].Caption+'加入进来!');  //显示什么日期时间某某加入进来
      user := listview1.Items[ii].Caption + '^' +
        listview1.Items[ii].SubItems[0] + '^'
        + listview1.Items[ii].SubItems[1] + '^'
        + listview1.Items[ii].SubItems[2];
      for jj := 0 to listview1.Items.Count - 1 do
      begin
        serversocket1.Socket.Connections[jj].SendText('增加用户@#!^' +
          user + '^'); //发送增加用户某某给其他各个客户端
      end;
      for jj := 0 to listview1.Items.Count - 2 do
      begin
        user := listview1.Items[jj].Caption + '^' +
          listview1.Items[jj].SubItems[0] + '^'
          + listview1.Items[jj].SubItems[1] + '^'
          + listview1.Items[jj].SubItems[2];
        serversocket1.Socket.Connections[ii].SendText('增加用户@#!^' +
          user + '^');  
      end;
    end;procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    var
      ii,jj:integer;
      User:String;
    begin
      for ii:=0 to ListView1.Items.Count-1 do begin
        if ListView1.Items.Item[ii].SubItems.Strings[2]=IntToStr(Socket.RemotePort) then begin//当有用户离开时发送类似信息给各个客户端
          for jj:=0 to ListView1.Items.Count -1 do begin
             user:=ListView1.Items[ii].Caption+'^'+ListView1.Items[ii].subitems[0]+'^'+
              ListView1.Items[ii].SubItems[1]+'^'+ListView1.Items[ii].subitems[2];
              ServerSocket1.Socket.Connections[jj].SendText('删除用户@#!^'+User+'^');
          end;
            Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
            ListView1.Items[ii].Caption+'走了!');
            ListView1.Items.Item[ii].Delete();
            Display(Socket.RemoteHost+'走了');//显示某某在什么时间走了
          end;
        end;
      if ListView1.Items.Count=0 then begin
        Display('获取信息......');//如果用户列表为空则显示正在获取信息
      end;
    end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    var
      tmpstr,User:string;
      ii,jj:integer;
      ListItem:TListItem;
      exist:bool;
    begin
    exist:=False;
    tmpstr:=Socket.ReceiveText;
    if getmaskstring(tmpstr,'^',1)='交谈@#!' then begin //根据收到的字符串显示某某在交谈
    for jj:=0 to ListView1.Items.Count -1 do begin
    user:=getmaskstring(tmpstr,'^',2);
    ServerSocket1.Socket.Connections[jj].SendText('交谈@#!^'+User+'^');
    end;
    Richedit1.Lines.Add(getmaskstring(tmpstr,'^',2));//显示交谈客户内容
    end;
    if getmaskstring(tmpstr,'^',1)='昵称@#!' then begin//如果收到的是昵称而不是交谈信息,则说明用户发送过来的是更改昵称的信息
    for ii:=0 to ListView1.Items.Count-1 do begin
    if Socket.RemotePort=StrToInt(ListView1.Items[ii].SubItems[2])
    then begin
       exist:=true;
       if getmaskstring(tmpstr,'^',2)<>ListView1.Items[ii].Caption then
    begin
      Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
      ListView1.Items[ii].Caption+' 更名为 '+
      getmaskstring(tmpstr,'^',2));
       ListView1.Items[ii].Caption:=getmaskstring(tmpstr,'^',2);//显示更名为某某
    for jj:=0 to ListView1.Items.Count -1 do begin
    user:=ListView1.Items[ii].Caption+'^'+ListView1.Items[ii].subitems[0]+'^'+ListView1.Items[ii].SubItems[1]+'^'+ListView1.Items[ii].subitems[2];
       ServerSocket1.Socket.Connections[jj].SendText('昵称@#!^'+User+'^');//通知各个客户端某用户改名后的昵称,以便在客户端更新昵称
    end;
              end;
          end;
       end;
    end;
    end;客户端:procedure TForm1.ClientSocket1Read(Sender: TObject;
      Socket: TCustomWinSocket);
    var
      tmpstr:string;
      newItem:TListItem;
      ii:Integer;
      exist:bool;
      commHead:string;
    begin
    exist:=false;
    tmpstr:=Socket.ReceiveText;
    commHead:=getmaskstring(tmpstr,'^',1);
    //Richedit1.Lines.Add(tmpstr);
    if commHead ='昵称@#!'then //如果客户端收到的是昵称
    begin
     for ii:=0 to ListView1.Items.Count -1 do
     begin
       if getmaskstring(tmpstr,'^',5)=ListView1.Items[ii].SubItems[2]
     then
       begin
       exist:=true;
       if getmaskstring(tmpstr,'^',2)<>ListView1.Items[ii].Caption then
       begin
       Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
       ListView1.Items[ii].Caption+' 更名为 '+
       getmaskstring(tmpstr,'^',2));
       ListView1.Items[ii].Caption:=getmaskstring(tmpstr,'^',2);//更改客户端客户的昵称          end;
           end;
        end;
     end;
     if commHead='交谈@#!'then//如果收到的是交谈信息,则显示交谈信息
     begin
     Richedit1.Lines.Add(getmaskstring(tmpstr,'^',2));
     end;
     if commHead='增加用户@#!'then //如果收到的是增加用户的信息,个在在线用户列表中添加该用户
     begin
     for ii:=0 to ListView1.Items.Count-1 do
     begin
     if getmaskstring(tmpstr,'^',5)=ListView1.Items[ii].SubItems[2] then
     exist:=true;
     end;
     if exist=false then begin
     newitem:=ListView1.Items.add();
     newitem.Caption:=getmaskstring(tmpstr,'^',2);
     newitem.SubItems.Add(getmaskstring(tmpstr,'^',3));
     newitem.SubItems.Add(getmaskstring(tmpstr,'^',4));
     newitem.SubItems.Add(getmaskstring(tmpstr,'^',5));
       Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
       getmaskstring(tmpstr,'^',2)+'加入进来!');//显示某人在什么时间加入进来 end;
     end;
     if commHead='删除用户@#!'then   //如果收到的删除用户信息,则在在线列表中删除该用户昵称
     begin
     for ii:=0 to ListView1.Items.Count-1 do
     begin
     if getmaskstring(tmpstr,'^',5)=ListView1.Items[ii].SubItems[2] then
     begin
     Listview1.Items.Delete(ii);
       Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
       getmaskstring(tmpstr,'^',2)+'走了!');   //显示什么时间某某用户走了   end;
      end;
     end;
    end;昵称@#!^
    删除用户@#!
    交谈@#!程序中此类字符串附加在发送的字符串首部,当接收到此类字符串,不管是那一类,都作为发送数据的类型,在接收到字符串后,用getmaskstring()函数再除去首部,只显示接收到的部分,除去附加信息。实际上就相当于一个客户端和服务器端的协议。