[fpc-pascal]Garbage on client socket

Fernando Lozano fsl at centroin.com.br
Wed Aug 28 23:04:57 CEST 2002


Hi there,

I wrote two small programs using the sockets unit from FPC 1.0.6
under Windows 95. One is a client that just sends one line of text
and reads the reply from the same socket. The other is a server that
eches back anything it receives from the client:

Here's the client:

//---------------------------

program cliente;

uses sockets;

procedure erro (msg: ansistring);
begin
    writeln (msg);
    halt(1);
end;

var
    addr : TInetSockAddr;
    s : longint;
    sin, sout : text;
    linha : ansistring;

begin
    addr.family:=AF_INET;
    { port 3000h in network order }
    addr.port := ((30) shl 8) or 0;
    { localhost : 127.0.0.1 in network order }
    addr.addr := (1 shl 24) or 127;
    s := socket (AF_INET, SOCK_STREAM, 0);
    if not connect (s, addr, sin, sout) then erro ('failed connect');
    rewrite (sout);
    reset (sin);
    writeln (sout, 'This is a test');
    flush (sout);
    readln (sin, linha);
    writeln ('Got:[', linha, ']');
    close (sin);
    close (sout);
end.

//-------------------

And here's the server:

//-------------------

program servidor;

uses sockets;

procedure erro (msg: ansistring);
begin
    writeln (msg);
    halt(1);
end;

var
    addr : TInetSockAddr;
    s : longint;
    sin, sout : text;
    linha : ansistring;

begin
    addr.family:=AF_INET;
    { port 3000h in network order }
    addr.port := ((30) shl 8) or 0;
    { localhost : 127.0.0.1 in network order }
    addr.addr := (1 shl 24) or 127;
    s := socket (AF_INET, SOCK_STREAM, 0);
    if not bind (s, addr, sizeof (addr)) then erro ('can't bind');
    if not listen (s, 1) then erro ('can't listen');
    if not accept (s, addr, sin, sout) then erro ('can't accept');
    rewrite (sout);
    reset (sin);
    while not eof (sin) do begin
	readln (sin, linha);
	writeln ('Got:', linha);
	writeln (sout, linha);
	flush (sout);
    end;
    close (sin);
    close (sout);
end.

//-------------------

And here's the output from the server:

//-------------------
H:\redes>servidor
Got:This is a test

H:\redes>
//-------------------

But look at the client output, full of garbage:

//-------------------
H:\redes>cliente
got:[


                           _Î                 îË@ X?@ ©?@ @@ X@@ 
   .


                   This is a test]

H:\redes>
//-------------------

I can't find if I did something wrong.


[]s, Fernando Lozano




More information about the fpc-pascal mailing list