[fpc-pascal]not connect socket help

Thomas Schatzl tom_at_work at yline.com
Mon Jan 14 12:47:48 CET 2002


Subject: [fpc-pascal]not connect socket help


> hello list
>
>
> I been looking at the example pfinger and to modify it,
>
>  but not leaves.
>  send to give me an example of as connecting me through a socket al
>  port 3490 and the address 192.168.1.1

Here's a snip from a (Win32-)program of mine (should work on Unices as well,
not tested there). Use Delphi mode to compile:

type
    UInt32 = DWord;
    UInt16 = Word;
const
    INVALID_SOCKET = -1; // dunno if this is already defined by Unices
var
    hoststring : String; // your "192.168.1.1"
    sock_fd : TSocket; // socket (file) handle
    he : PHostEnt; // host entry
    port : UInt16; // port, e.g. 3490
    their_addr : TSockAddrIn; // target address

...
 port := 3490;
 hoststring := '192.168.1.1';
 he := gethostbyname(PChar(hoststring));
 if (he = nil) then begin
    // prints given error message+last error number, closes socket if open
and exits program
  sockshutdown('gethostbyname()');
 end;
 Writeln('Retrieving socket....');
 sock_fd := Socket(AF_INET, SOCK_STREAM, 0);
 if (sock_fd = INVALID_SOCKET) then begin
  sockshutdown('socket()');
 end;
 fillchar(their_addr, sizeof(their_addr), 0);
 with their_addr do begin
  sIn_family := AF_INET;
  sIn_port := htons(StrToInt(port));
  sIn_addr.s_addr := Uint32((@he^.h_addr^[0])^);
 end;

 Writeln('Connecting to socket....');
 if (connect(sock_fd, their_addr, sizeof(their_addr)) <> 0) then begin
  sockshutdown('connect()');
 end;
  // at this point the socket is connected to the machine
...
 close(sock_fd);

Hth (a little at least),
  Thomas





More information about the fpc-pascal mailing list