请教如何得到TCustomWinSocket在ServerSocket->Socket->Connections[]中的数组索引?

csym 2003-12-11 09:47:34
要求不用循环判断是否相等,谢谢;
...全文
28 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
esiedull 2003-12-15
  • 打赏
  • 举报
回复
如果不用判断,你想得到你想要的索引,那如何检索呢?
Gprs远程监控单机演示unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ScktComp, ComCtrls;type TForm1 = class(TForm) ServerSocket1: TServerSocket; Button1: TButton; Timer1: TTimer; Label10: TLabel; StatusBar1: TStatusBar; Memo1: TMemo; Memo2: TMemo; Label11: TLabel; Label12: TLabel; Label16: TLabel; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; CheckBox1: TCheckBox; Edit2: TEdit; Label4: TLabel; Label5: TLabel; GroupBox1: TGroupBox; RadioButton1: TRadioButton; RadioButton2: TRadioButton; Button2: TButton; Button3: TButton; Button4: TButton; Label6: TLabel; Label7: TLabel; Timer2: TTimer; procedure ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket); procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); procedure CheckBox1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Timer2Timer(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; ip, Port : string;implementationprocedure TForm1.ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket);begin ip := Socket.RemoteAddress; Port := inttostr(Socket.RemotePort); StatusBar1.Panels[0].Text := ‘ 下位机DTU已成功连接!‘; StatusBar1.Panels[1].Text := ‘ 已连接IP及端口:‘+ip+‘:‘+port+‘ 时间:‘+TimeToStr(now);; Button1.Enabled := true;end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);var s, s_hex : string; i : integer;begin s := Socket.ReceiveText; memo2.Lines.Add(‘(TCP)来自: ‘ + Socket.RemoteAddress + ‘:‘+inttostr(Socket.RemotePort) +‘ 接收到:‘+inttostr(length(s))+‘(bytes) 数据如下:‘); if RadioButton1.Checked = true then begin for i:=1 to length(s) do s_hex := s_hex+inttohex(ord(s[i]),2)+ ‘ ‘; // memo2.Lines.Add(s_hex); end else memo2.Lines.Add(s);end;procedure TForm1.Button1Click(Sender: TObject);var i : integer; s_memo, s_hex: String;begin if (memo1.Text = ‘‘)then ShowMessage(‘发送数据不能为空!‘) else try begin if (RadioButton1.Checked = false) then begin for I := 0 to memo1.Lines.Count - 1 do ServerSocket1.Socket.Connections[0].SendText(memo1.lines[i]); //如果多台下位机同时与服务器相连,可选用不同的线程发送,既:Socket.Connections[0] end else begin try s_memo := StringReplace(memo1.text,‘ ‘,‘‘,[rfReplaceAll]); for i := 1 to length(s_memo) div 2 do s_hex := s_hex+char(strtoint(‘+copy(s_memo,(2*i-1),2))); ServerSocket1.Socket.Connections[0].SendText(s_hex); except ShowMessage(‘发送16进制(Hex编码)输入有错!‘); end; end; end; except ShowMessage(‘发送数据失败!‘); end;end;procedure TForm1.FormCreate(Sender: TObject);begin Button1.Enabled := false; ServerSocket1.Service := ServerSocket1.Socket.LocalAddress; ServerSocket1.Active := true; StatusBar1.Panels[0].Text := ‘ 上位机TCP侦听端口:5050‘; memo1.Clear; memo2.Clear; Label11.Caption := DateToStr(now);end;procedure TForm1.Button2Click(Sender: TObject);begin Button1.Enabled := false; ServerSocket1.Active := false; ServerSocket1.Service := ServerSocket1.Socket.LocalAddress; ServerSocket1.Port := StrToInt(edit1.Text); ServerSocket1.Active := true; StatusBar1.Panels[0].Text := ‘ 上位机TCP侦听端口:‘+edit1.Text;end;procedure TForm1.CheckBox1Click(Sender: TObject);begin if CheckBox1.Checked = true then begin Label4.Enabled := false; Label5.Enabled := false; edit2.Enabled := false; Timer1.Enabled := true; Timer1.Interval := strtoint(edit2.Text); end; if CheckBox1.Checked = false then begin edit2.Enabled := true; Timer1.Enabled := false; Label4.Enabled := true; Label5.Enabled := true; end;end;procedure TForm1.Timer1Timer(Sender: TObject);begin if (memo1.Text <> ‘‘) and (Button1.Enabled = true) then Button1.Click;end;procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);begin StatusBar1.Panels[0].Text := ‘ 下位机TCP连接已断开!‘; StatusBar1.Panels[1].Text := ‘‘;end;procedure TForm1.Button3Click(Sender: TObject);begin close;end;procedure TForm1.Button4Click(Sender: TObject);begin memo1.Clear; memo2.Clear;end;

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧