求delphi7.0  下socket客户端对客户端一对一聊天代码,只用serversocket和clientsocket 实现,,客户端多个都要和其中一个聊天时,要排队,得到允许才可聊天。有代码的可发我邮箱:[email protected]

解决方案 »

  1.   

    好像在DELPHI盒子里面有看到过,自己去找找吧。。
      

  2.   

    demo裡也應該有實現的效果吧?
      

  3.   

    zlchat是一个网页视频会议软件,使用它你可以与远方的亲朋好友视频聊天,也可以用于公司远程视频会议,远程视频教学。zlchat 没有使用任何activex插件,不用安装客户端,打开网页就可能使用。 1.不限制房间,用户数 2.每个房间最高可达50人,一台服务器最高可支持1000人同时在线 3.普通ADSL可以同时看10路用户视频 4.费版本永远不收费,不过期 5.完全跨平台,支持windows,linux,unix服务器。 
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdIPMCastServer, IdBaseComponent, IdComponent, IdIPMCastBase,
      FunLIB,IdIPMCastClient,IdSocketHandle,  StdCtrls, ExtCtrls, IdTCPServer,
      IdTCPConnection, IdTCPClient;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Memo2: TMemo;
        ListBox1: TListBox;
        Button1: TButton;
        Button2: TButton;
        IdIPMCastClient1: TIdIPMCastClient;
        IdIPMCastServer1: TIdIPMCastServer;
        Label1: TLabel;
        Label2: TLabel;
        Button3: TButton;
        Image1: TImage;
        IdTCPClient1: TIdTCPClient;
        IdTCPServer1: TIdTCPServer;
        Image2: TImage;
        procedure Button2Click(Sender: TObject);
        procedure IdIPMCastClient1IPMCastRead(Sender: TObject; AData: TStream;
          ABinding: TIdSocketHandle);
        procedure Button1Click(Sender: TObject);
        procedure CastMyIP;
        procedure FormCreate(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure IdTCPServer1Execute(AThread: TIdPeerThread);
      private
        { Private declarations }
      public
        LocalIP:String;
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
    begin
      IdIPMCastServer1.Send('1');
    end;procedure TForm1.IdIPMCastClient1IPMCastRead(Sender: TObject;
      AData: TStream; ABinding: TIdSocketHandle);
    var
      IP:String;
      Port:Integer;
      Bf:TStringStream;
      ProgId,k:Integer;
      MsgTxt,DestIP:String;
    begin
      //对方传入机器IP地址 及 端口号
      IP :=ABinding.PeerIP;
      Port := ABinding.PeerPort;
      bf := TStringStream.Create('');
      bf.CopyFrom(AData,0);  if bf.DataString<>'' then
      begin
        ProgId:=StrToInt(copy(bf.DataString,1,1));
        case ProgId of
        1: begin
        //请求IP回应
             CastMyIP;
           end;
        2: begin
        //接收
            k := pos('*',bf.DataString);
            DestIP := copy(bf.DataString,2,k-2);  //取目标IP
            if DestIP=LocalIP then  //判断信息是否是发给自己IP的
            begin
              msgTxt := copy(bf.DataString,2,length(bf.DataString)-1);
              Memo2.Lines.Add(IP+':'+msgTxt);
            end;
           end;     //在列表中添加接收到的IP地址
         3 : begin
             for k:=0 to ListBox1.Items.Count-1 do
               if ListBox1.Items[k]=IP then exit;
             ListBox1.Items.Add(IP);
           end;    end;
      end;end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      //发送信息 自定义信息格式 "2+IP+*+信息体"
      IdIPMCastServer1.Send('2'+ListBox1.Items[ListBox1.ItemIndex]+'*'+Memo1.Text);
    end;procedure TForm1.CastMyIP;
    begin
      //在局域网中公告自己的IP地址
      IdIPMCastServer1.Send('3');
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      LocalIP := GetLocalIP;
      CastMyIP;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
      bf,bmpBf:TMemoryStream;
      StrBf:TStringStream;
    begin
      Image1.Picture.Bitmap.LoadFromFile('c:\111.bmp');
      bmpBf := TMemoryStream.Create;
      Image1.Picture.Bitmap.SaveToStream(bmpBf);
      IdTCPClient1.Host := ListBox1.Items[ListBox1.ItemIndex];
      if IdTCPClient1.Connected then IdTCPCLient1.Disconnect;
      IdTCPClient1.Connect(1000);
      IdTCPCLient1.WriteStream(bmpBf,true,true,bmpBf.Size);
      idTCPClient1.Disconnect;
    end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
      bf :TMemoryStream;
    begin
      bf := TMemoryStream.Create;
      AThread.Connection.ReadStream(bf,-1);
      bf.Position := 0;
      Image2.Picture.Bitmap.LoadFromStream(bf);
    end;end.
      

  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:= &apos;Client: &apos;+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( &apos;连接到主机: &apos;+Socket.RemoteAddress+ &apos;   成功 &apos;); 
        ii:=ListView1.ItemIndex; 
            Richedit1.Lines.Add(DateToStr(Date)+ &apos;   &apos;+TimeToStr(time)+ &apos;== > > &apos;+ 
            ListView1.Items[ii].Caption+ &apos;加入进来! &apos;);     //显示什么日期时间某某加入进来 
        user   :=   listview1.Items[ii].Caption   +   &apos;^ &apos;   + 
            listview1.Items[ii].SubItems[0]   +   &apos;^ &apos; 
            +   listview1.Items[ii].SubItems[1]   +   &apos;^ &apos; 
            +   listview1.Items[ii].SubItems[2]; 
        for   jj   :=   0   to   listview1.Items.Count   -   1   do 
        begin 
            serversocket1.Socket.Connections[jj].SendText( &apos;增加用户@#!^ &apos;   + 
                user   +   &apos;^ &apos;);   //发送增加用户某某给其他各个客户端 
        end; 
        for   jj   :=   0   to   listview1.Items.Count   -   2   do 
        begin 
            user   :=   listview1.Items[jj].Caption   +   &apos;^ &apos;   + 
                listview1.Items[jj].SubItems[0]   +   &apos;^ &apos; 
                +   listview1.Items[jj].SubItems[1]   +   &apos;^ &apos; 
                +   listview1.Items[jj].SubItems[2]; 
            serversocket1.Socket.Connections[ii].SendText( &apos;增加用户@#!^ &apos;   + 
                user   +   &apos;^ &apos;);     
        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+ &apos;^ &apos;+ListView1.Items[ii].subitems[0]+ &apos;^ &apos;+ 
                        ListView1.Items[ii].SubItems[1]+ &apos;^ &apos;+ListView1.Items[ii].subitems[2]; 
                        ServerSocket1.Socket.Connections[jj].SendText( &apos;删除用户@#!^ &apos;+User+ &apos;^ &apos;); 
                end; 
                    Richedit1.Lines.Add(DateToStr(Date)+ &apos;   &apos;+TimeToStr(time)+ &apos;== > > &apos;+ 
                    ListView1.Items[ii].Caption+ &apos;走了! &apos;); 
                    ListView1.Items.Item[ii].Delete(); 
                    Display(Socket.RemoteHost+ &apos;走了 &apos;);//显示某某在什么时间走了 
                end; 
            end; 
        if   ListView1.Items.Count=0   then   begin 
            Display( &apos;获取信息...... &apos;);//如果用户列表为空则显示正在获取信息 
        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, &apos;^ &apos;,1)= &apos;交谈@#! &apos;   then   begin   //根据收到的字符串显示某某在交谈 
    for   jj:=0   to   ListView1.Items.Count   -1   do   begin 
    user:=getmaskstring(tmpstr, &apos;^ &apos;,2); 
    ServerSocket1.Socket.Connections[jj].SendText( &apos;交谈@#!^ &apos;+User+ &apos;^ &apos;); 
    end; 
    Richedit1.Lines.Add(getmaskstring(tmpstr, &apos;^ &apos;,2));//显示交谈客户内容 
    end; 
    if   getmaskstring(tmpstr, &apos;^ &apos;,1)= &apos;昵称@#! &apos;   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, &apos;^ &apos;,2) < >ListView1.Items[ii].Caption   then 
    begin 
        Richedit1.Lines.Add(DateToStr(Date)+ &apos;   &apos;+TimeToStr(time)+ &apos;== > > &apos;+ 
        ListView1.Items[ii].Caption+ &apos;   更名为   &apos;+ 
        getmaskstring(tmpstr, &apos;^ &apos;,2)); 
          ListView1.Items[ii].Caption:=getmaskstring(tmpstr, &apos;^ &apos;,2);//显示更名为某某 
    for   jj:=0   to   ListView1.Items.Count   -1   do   begin 
    user:=ListView1.Items[ii].Caption+ &apos;^ &apos;+ListView1.Items[ii].subitems[0]+ &apos;^ &apos;+ ListView1.Items[ii].SubItems[1]+ &apos;^ &apos;+ListView1.Items[ii].subitems[2]; 
          ServerSocket1.Socket.Connections[jj].SendText( &apos;昵称@#!^ &apos;+User+ &apos;^ &apos;);//通知各个客户端某用户改名后的昵称,以便在客户端更新昵称 
    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, &apos;^ &apos;,1); 
    //Richedit1.Lines.Add(tmpstr); 
    if   commHead   = &apos;昵称@#! &apos;then   //如果客户端收到的是昵称 
    begin 
      for   ii:=0   to   ListView1.Items.Count   -1   do 
      begin 
          if   getmaskstring(tmpstr, &apos;^ &apos;,5)=ListView1.Items[ii].SubItems[2] 
      then 
          begin 
          exist:=true; 
          if   getmaskstring(tmpstr, &apos;^ &apos;,2) < >ListView1.Items[ii].Caption   then 
          begin 
          Richedit1.Lines.Add(DateToStr(Date)+ &apos;   &apos;+TimeToStr(time)+ &apos;== > > &apos;+ 
          ListView1.Items[ii].Caption+ &apos;   更名为   &apos;+ 
          getmaskstring(tmpstr, &apos;^ &apos;,2)); 
          ListView1.Items[ii].Caption:=getmaskstring(tmpstr, &apos;^ &apos;,2);//更改客户端客户的昵称                     end; 
                  end; 
            end; 
      end; 
      if   commHead= &apos;交谈@#! &apos;then//如果收到的是交谈信息,则显示交谈信息 
      begin 
      Richedit1.Lines.Add(getmaskstring(tmpstr, &apos;^ &apos;,2)); 
      end; 
      if   commHead= &apos;增加用户@#! &apos;then   //如果收到的是增加用户的信息,个在在线用户列表中添加该用户 
      begin 
      for   ii:=0   to   ListView1.Items.Count-1   do 
      begin 
      if   getmaskstring(tmpstr, &apos;^ &apos;,5)=ListView1.Items[ii].SubItems[2]   then 
      exist:=true; 
      end; 
      if   exist=false   then   begin 
      newitem:=ListView1.Items.add(); 
      newitem.Caption:=getmaskstring(tmpstr, &apos;^ &apos;,2); 
      newitem.SubItems.Add(getmaskstring(tmpstr, &apos;^ &apos;,3)); 
      newitem.SubItems.Add(getmaskstring(tmpstr, &apos;^ &apos;,4)); 
      newitem.SubItems.Add(getmaskstring(tmpstr, &apos;^ &apos;,5)); 
          Richedit1.Lines.Add(DateToStr(Date)+ &apos;   &apos;+TimeToStr(time)+ &apos;== > > &apos;+ 
          getmaskstring(tmpstr, &apos;^ &apos;,2)+ &apos;加入进来! &apos;);//显示某人在什么时间加入进来   end; 
      end; 
      if   commHead= &apos;删除用户@#! &apos;then       //如果收到的删除用户信息,则在在线列表中删除该用户昵称 
      begin 
      for   ii:=0   to   ListView1.Items.Count-1   do 
      begin 
      if   getmaskstring(tmpstr, &apos;^ &apos;,5)=ListView1.Items[ii].SubItems[2]   then 
      begin 
      Listview1.Items.Delete(ii); 
          Richedit1.Lines.Add(DateToStr(Date)+ &apos;   &apos;+TimeToStr(time)+ &apos;== > > &apos;+ 
          getmaskstring(tmpstr, &apos;^ &apos;,2)+ &apos;走了! &apos;);       //显示什么时间某某用户走了       end; 
        end; 
      end; 
    end;