Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TcpServer doesn't work as described

456 views
Skip to first unread message

Andre Becker

unread,
Apr 24, 2003, 11:44:27 AM4/24/03
to
Hi,

in D7 I tried to use TcpClient and TcpServer components to establish a
socket connection. (Some time ago I used the D5 Socket comps and succeeded).

The manual and online help tell about TcpServer events like OnError,
OnReceive and OnSend. But they do not appear in object inspector. I
published them manually, without any success.

Seems that the TcpClient finds the server (it shows an error if the server
isn't running or active). But sending data via Sendln doesn't fire any event
on the TcpServer.

Perhaps its a big misunderstanding on my side, perhaps its an error in the
manual and online help, or what is it?

Thx.

André

unread,
Apr 28, 2003, 7:45:49 AM4/28/03
to
Hi there,

I have EXACTLY the same problem. The delphi-demo program "NetChat" isn't
much help either.
I also tried to create the events OnReceive, etc. my self, but without
succes.

The only place where you can get the data from client.SendLn is when the
server "accepts". One of the parameters from "accept" is "ClientSocket". To
read the data use: ClientSocket.Receiveln.

I am using the TClientSocket and TServerSocket (from D5) again. That works
OK but I can not use my code on Linux.

Greetings...


"Andre Becker" <nos...@gmx.de> schreef in bericht
news:b890mt$p0v$07$1...@news.t-online.com...

Andre Becker

unread,
Apr 28, 2003, 8:03:59 AM4/28/03
to
Today I tried again, but I cannot make them work! I am planning to convert
my apps to Kylix so I need the new components.

I again read the manual and online help, think to have understood the theory
behind the socket components, but am unable to make the connect properly! It
depends on the BlockingMode, sometimes I get a OnAccept event, but e.g.
stopping the server does not result in an OnDisconnect event on the client.
With the old socket components, this worked!

When the client is NonBlocking and the server is ThreadBlocking, I get an
OnAccept event and a new connection is established by the server, using a
new port, just as described. But on the client side, I get an error no.
10035 and afterwards no. 10056.

I just don't know what to do. Where can I find a list of winsock error
codes?

"André" <and...@foreach.nl> schrieb im Newsbeitrag
news:3ead1475$0$89809$edd6...@news.versatel.net...

André

unread,
Apr 28, 2003, 9:35:23 AM4/28/03
to
You write that you set the serverside to "bmThreadBlocking" and the client
to "bmNonBlocking".
I used for the client the "bmBlocking", then there is no error but it can
not receive data, only sending data.

// Here's a list with socketerrors:
Function TranslateSocketError(nError : Integer) : String;
Begin
Case nError of
10004: Result := 'Interrupted function call';
10013: Result := 'Permission denied';
10014: Result := 'Bad address';
10022: Result := 'Invalid argument';
10024: Result := 'Too many open files';
10035: Result := 'Resource temporarily unavailable';
10036: Result := 'Operation now in progress';
10037: Result := 'Operation already in progress';
10038: Result := 'Socket operation on non-socket';
10039: Result := 'Destination address required';
10040: Result := 'Message too long';
10041: Result := 'Protocol wrong type for socket';
10042: Result := 'Bad protocol option';
10043: Result := 'Protocol not supported';
10044: Result := 'Socket type not supported';
10045: Result := 'Operation not supported';
10046: Result := 'Protocol family not supported';
10047: Result := 'Address family not supported by protocol family';
10048: Result := 'Address already in use';
10049: Result := 'Cannot assign requested address';
10050: Result := 'Network is down';
10051: Result := 'Network is unreachable';
10052: Result := 'Network dropped connection on reset';
10053: Result := 'Software caused connection abort';
10054: Result := 'Connection reset by peer';
10055: Result := 'No buffer space available';
10056: Result := 'Socket is already connected';
10057: Result := 'Socket is not connected';
10058: Result := 'Cannot send after socket shutdown';
10060: Result := 'Connection timed out';
10061: Result := 'Connection refused';
10064: Result := 'Host is down';
10065: Result := 'No route to host';
10067: Result := 'Too many processes';
10091: Result := 'Network subsystem is unavailable';
10092: Result := 'WINSOCK.DLL version out of range';
10093: Result := 'Successful WSAStartup not yet performed';
10094: Result := 'Graceful shutdown in progress';
11001: Result := 'Host not found';
11002: Result := 'Non-authoritative host not found';
11003: Result := 'This is a non-recoverable error';
11004: Result := 'Valid name, no data record of requested type';
else
Result := 'Unknown error';
end;
end;

"Andre Becker" <nos...@gmx.de> schreef in bericht

news:b8j5li$1bj$00$1...@news.t-online.com...

David Montgomery

unread,
Apr 28, 2003, 2:39:03 PM4/28/03
to
"Andre Becker" <nos...@gmx.de> wrote in message
news:b890mt$p0v$07$1...@news.t-online.com...

> Perhaps its a big misunderstanding on my side, perhaps its an error in the
> manual and online help, or what is it?

1) First create a TClientSocketThread descendent class
2) Put a TTCPServer on a form
3) Set the OnGetThread event of the TTCPServer
4) Set the port of the TTCPServer component, and set Active := True
5) Connect from your favorite web browser

unit myServerThread

interface

uses
SysUtils, Classes, Sockets;

type
TTCPClientThread = Class(TClientSocketThread)
Private
FSendStream : TStringStream;
FHeaderList : TStringList;
ClientSocket : TCustomIpClient;
procedure SendResponse;
Protected
Procedure DoExecute(ClientSocket: TCustomIpClient); virtual;
Public
Procedure Execute; override;
End;

implementation

threadvar
ThreadObject: TClientSocketThread;

procedure TTCPClientThread.Execute;
begin
ThreadObject := Self;
while not Terminated do
begin
if Assigned(ServerSocketThread) and
Assigned(ServerSocketThread.ServerSocket) then
begin
ClientSocket := TCustomIpClient.Create(nil);
try
ServerSocketThread.ServerSocket.Accept(ClientSocket);
DoExecute(ClientSocket);
finally
ClientSocket.Free;
ClientSocket := nil;
end;
end;
if not Terminated then
Suspend;
end;
end;

procedure TTCPClientThread.DoExecute(ClientSocket: TCustomIpClient);
var
s: string;
begin
FHeaderlist := tstringlist.create;
try
// the request needs to end with a blank line sent!
// buffer the get/post data in FHeaderList
s := ClientSocket.Receiveln;
while s <> '' do
begin
FHeaderlist.Add(s);
s := '';
s := ClientSocket.Receiveln;
end;
if FHeaderlist.text = '' then exit;
// build your response here
FSendStream := TStringStream.Create('HTTP/1.0 200 OK' + #13#10 +
'Content-type: text/html' + #13#10 + '<HTML><body>My web page</body></html>'
+ #13#10);
try
SendResponse;
finally
FSendStream.Free;
end;
finally
fHeaderlist.free;
end;
end;

procedure TTCPClientThread.SendResponse;
Begin
FSendStream.Position := 0;
ClientSocket.SendStream(fSendStream);
ClientSocket.Close;
End;

end.


2) In the OnGetThread Event:

procedure TForm1.getThreadProc(Sender: TObject; var ClientSocketThread:
TClientSocketThread);
begin
ClientSocketThread := TTCPClientThread.Create(server.ServerSocketThread);
end;


Here is a TTCPClient sample:

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
I: Integer;
s: string;
begin
TcpClient1.RemoteHost := edtRemoteHost.Text;
TcpClient1.RemotePort := edtRemotePort.Text;
try
if TcpClient1.Connect then
for I := 0 to memSend.Lines.Count - 1 do
TcpClient1.Sendln(memSend.Lines[I]);
if TcpClient1.WaitForData(5000) then
showmessage('Rcv''d by Client:'+#13#10+tcpClient1.Receiveln('~'));
finally
sleep(3000);
TcpClient1.Disconnect;
end;
end;


0 new messages