[fpc-pascal]winsock telnet proxy question #1

heather wilson heather.wilson at coastgames.com
Sun Mar 10 22:31:48 CET 2002


ok, after a lot of homework and some help from this list, i have got my
telnet server working correctly - it can accept multiple client connections
and pass communication to and from the clients.  (i have only tested it
locally, but that is all i need.)  in order to make my proxy complete, the
SAME program has to connect to another telnet server and pass the
information to the other clients.  so my program needs to be a server AND a
client.

i tried to write a stand-alone client just to get the feel of how to write
functional client code, and i have come to an impasse.  i have a telnet
server running on my pc on port 23, and i would like to connect to that
server using my client.  so far it compiles fine, and runs fine, but
gethostbyname ('localhost') returns nil.  here's my code (the error handling
is infantile, i know):

__________________________________
program client;

uses
  winsock, crt, strings;

var
  wsadata : twsadata;
  sockfd : tsocket;
  server : phostent;
  serveraddr : tsockaddr;
  n: integer;
  name : pchar;
  buffer : array[0..256] of char;

function buildaddr(family: smallint; port: u_short; addr: u_long):
tsockaddr;
var
  a: tsockaddr;
begin
  a.sin_family:=family;
  a.sin_port:=htons(port);
  a.sin_addr.s_addr:=addr;
  buildaddr:=a;
end;

begin
  getmem(name,sizeof('localhost')+1);
  strcopy(name,'localhost');

  writeln('retrieving socket....');
  sockfd := socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd = INVALID_SOCKET) then halt;

  writeln('getting host name....',name);
  server := gethostbyname(name);
  if server=nil then halt;  // PROGRAM HALTS HERE

  writeln('building address....');
  serveraddr:=buildaddr(AF_INET, 23, u_long(server^.h_addr));

  writeln('connecting socket....');
  if (connect(sockfd,serveraddr,sizeof(serveraddr))<0) then halt;

  n:=recv(sockfd,buffer,255,0);
  writeln(buffer);

  closesocket(sockfd);
  freemem(name);
  wsacleanup;
end.
__________________________________

why doesn't it find the host for 'localhost' - isn't that a correct host
name?  also, any other style and/or coding advice not related to
gethostbyname would be appreciated.

thanks for the help,
ron wilson.





More information about the fpc-pascal mailing list