我用 TClientSocket 和 TServerSocket 写了一个小程序,
虽然简单,但总觉得不是很舒服,我想自己用 API 来写,
谁能给个简单的例子?

解决方案 »

  1.   

    用API连接并发送IP包的例子。
    Function TryServerPort(IP:String;Port:integer):Boolean;
    var
      Sock : TSocket;
      buffer : array[0..8] of char;
      ErrorCode : integer;
      Server : TSockaddr;
    begin
      Result := False;
      Server.sin_family:=AF_INET;
      Server.sin_port:=htons(Port);
      Server.sin_addr.S_addr:=inet_addr(pchar(IP));
      Sock:=Socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
      ErrorCode:=connect(Sock,Server,sizeof(Server));
      if ErrorCode<>invalid_socket then begin
        Buffer := 'QUIT'#13#10;
        Send(Sock,Buffer,Length('QUIT'+#13#10),0);
        Result := True;
      end;
      CloseSocket(Sock);
    end;Procedure TryPort(Server:String);
    var
      WSA:TWSAData;
      i : integer;
    begin
      WSAStartup(MakeWord(1,1),WSA);
      if MinPort + MaxPort <>0 then
        begin
          for i:=MinPort to MaxPort do begin
            if TryServerPort(Server,i) then
              OutMemo.Lines.Add(Server + ':'+inttostr(PortArray[i]));
          end;
        end
      else begin
        for i:=0 to Length(PortArray)-1 do begin
          if TryServerPort(Server,PortArray[i]) then
            OutMemo.Lines.Add(Server + ':'+inttostr(PortArray[i]));
        end;
      end;
      WSACleanup();
    end;
      

  2.   

    kiboisme,谢谢你,我去试试!
      

  3.   

    kiboisme,
      好象不行啊,
      MinPort,MaxPort,PortArray 分别是在哪里定义的?您能不能给一个可以编译通过的可以发送、接收的例子?-------------------------------------
    谢谢你的关注,
      

  4.   

    去看《windows网络编程》,不过是用C++解释的,所以delphi没前途,关键是把socket上的信息发布到窗口消息上,关键函数int WSAAsyncSelect(
      SOCKET s,           
      HWND hWnd,          
      unsigned int wMsg,  
      long lEvent         
    );
    去看msdn