接上:
procedure clientsock.Execute;
begin
    while not Terminated do
    begin
      Synchronize(Timerun);
      sleep(100);
    end;
end;constructor ClientSock.Create(FT:Boolean;{port,}IP:string;CN:integer);
var
  s:byte;
begin
  inherited Create(FT);
  Client:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
  if Client=INVALID_SOCKET then //创建错误
  begin
    F_main.Memo1.Lines.Add('为连接锁创建Socket错误!');
    Exit;
  end
  else
  begin
    //定义协议族
    ca.sin_family:=PF_INET;     //tcp
           //将端口号由主机字节顺序转换为网络字节顺序;调用htons
    ca.sin_port:=htons(StrToint('1024')); //端口号
           //调用inet_addr函数将点分制十进制IP地址转换成网络地址,
           //此过程隐含地实现了到网络字节的转换;
    Hostaddr:=inet_addr(PChar(trim(ip))); //ip地址
    if hostaddr=-1 then
    begin
      F_main.Memo1.Lines.Add('主机IP地址'+ip+'错误');
      closeSocket(Client);
      Exit;
    end
    else
    begin
           //转换成功对地址赋值
      ca.sin_addr.S_addr:=hostaddr;
           //连接由协议族、地址、端口三元组确定了的服务器端;
      if connect(Client,ca,sizeof(ca))<>0 then       //连接失败
      begin
        F_main.Memo1.Lines.Add('连接锁错误,锁IP:'+IP);
        CloseSocket(Client);
        exit;
      end
      else
      begin
        s:=38; //&
        Send(Client,s,1,0);
        F_main.Edits[N].Text :='发送握手信号';
      end;
    end;
  end;    N:=CN;
    FreeOnTerminate:=True;
    //Synchronize(Timerun);
    Execute;
end;

解决方案 »

  1.   

    再接:
    Function TF_Octem.sock_YN(Tt:integer;sock_ip:string;ofdids:string):String; //
    begin
       if onconnect[0]=0 then
       begin
           T[0]:=Tt;
           lock_ip[0]:=Sock_ip;
           Lock_id[0]:=ofdids;
           //F_main.Timer1.Enabled:=True;
           F_main.Edits[0].Text :='操作锁'+Lock_id[0];
           clientsock.Create(True,Sock_ip,0) ;
           onconnect[0]:=1;
           result:='1';
       end
       else
       if onconnect[1]=0 then
       begin
           T[1]:=Tt;
           lock_ip[1]:=Sock_ip;
           Lock_id[1]:=ofdids;
           F_main.Edits[1].Text :='操作锁'+Lock_id[1];
           clientsock.Create(True,Sock_ip,1) ;       onconnect[1]:=1;
           result:='2';
     
       end
       else
       begin
          result:='0';
       end;
    end;
    ..
    //执行
    procedure TF_octem.BitBtn1Click(Sender: TObject);
    var
     j,k:Integer;
     of_did,ofip,r:string;
    begin
      if  RadioButton5.Checked then     //12巡检
      begin
        memo1.Clear ;
        memo1.Lines.Add('开始执行巡检!');
        listBox1.Clear ;
         //取出IP地址
        try
          with  Query1 do
          begin
             close;
             Sql.Clear;
             Sql.Add('select ofdid,ofip,ofstate from ofdoors where ofstate=''1''');
             open;
          end;
          while not Query1.Eof do
          begin
            of_did:=Query1.FieldByName('ofdid').AsString;
            ofip:=Query1.FieldByName('ofip').AsString;
            listbox1.Items.Add(ofip+'          '+of_did);
            Query1.Next  ;   //判断是否有连接如果没有连接则还执行本次循环
          end;
        except
          showmessage('对不起,执行巡检时取IP地址错误!');
          Abort;
        end;    for j:=0 to listBox1.Items.Count-1  do
        begin
          of_did:=copy(Listbox1.Items[j],length(Listbox1.Items[j])-7,8);
          ofip:=trim(copy(Listbox1.Items[j],1,length(Listbox1.Items[j])-8));
          r:=Sock_YN(12,ofip,of_did);     //12是巡检
          if r='0' then                   //如果没有空闲通道时还有IP
          begin
            For k:=0 to j-1 do
            begin
              Listbox1.Items.Delete(0);
            end;
            Break;
          end
          else
          begin
            if j=listBox1.Items.Count-1 then    //如果一次通道够用或者全连不通
              For k:=0 to j do
              begin
                Listbox1.Items.Delete(0);
              end;
          end;
        end;
        Timer1.Enabled :=True;                     //启动巡检检测器判断是否结束
      end
      else
      begin
        if (Trim(Edit2.Text)='') or (length(Edit2.Text)<8)  then
        begin
           showmessage('对不起,没有输入房门编码或者输入的房门编码小于8');
           Abort;
        end
        else
           if  (not RadioButton1.Checked) and (not RadioButton2.Checked) and
                  (not RadioButton3.Checked) and (not RadioButton4.Checked) then
           begin
             showmessage('对不起,没有选择功能操作,请选择!');
             Abort;
           end
           else
           begin
             of_did:=Trim(Edit2.Text);         //取得房门编码
             ofip:=Select_ofip(of_did);        //取得该锁号的IP   lock_ip[1]
             if ofip='' then
             begin
               showmessage('对不起,锁号为:'+ofdid[1]+'的IP为空,请进入管理中心进行IP添加!');
               Abort;
             end
             else
             begin
             if  RadioButton1.Checked then           //8更新卡表
             begin
                 r:=Sock_YN(8,ofip,of_did) ;               //判断Winsock状态建立连接                               //更新卡表8
                 case StrToInt(r) of
                  0:
                  begin
                   memo1.Clear ;
                   memo1.Lines.Add('没有空闲通道');
                  end;
                  else
                  begin
                   memo1.Clear ;
                   memo1.Lines.Add('开始更新卡表');
                  end ;
                 end;
               end
               else
                 if  RadioButton2.Checked then          //9强制开锁
                 begin
                   memo1.Clear ;
                   memo1.Lines.Add('开始强制开锁');
                   r:=Sock_YN(9,ofip,of_did);      //判断Winsock状态建立连接
                 end
             end;
           end;
      end;
    end;