Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-01

not connect socket help

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

J. Eduardo Garcia Torres

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

thanks for all

enzo J. Eduardo Garcia T. icq: 69885612 Messenger : Current Proyect : gcafe - http://gcafe.es.gnome.org web site: www.geocities.com/netenzo tel: +57 4 4150570 Medellin - Colombia - Sur America

Reply by email

Thomas Schatzl
to J. Eduardo Garcia Torres

Subject: [fpc-pascal]not connect socket help

Show quoted text 8 lines
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

Reply by email