Forum > Networking and Web Programming
TLTCP components problem
zdenal027:
Hello,
I'm trying to write program which use two TLTCP components - I need to connect to two servers with different ports. When enable only one TLTCP component (comment out TCP*.Connect(...) of second one), it behaves well. But when both are enabled, program hangs after while (about 5 seconds, so some packets are received and send). Simplified code inserted below. BTW, in the listbox I can see "connecting...", "connected", but "connection accepted" never appears... Is is ok? Thanks for any help!
Best regards,
Zdenek Materna
--- Code: ---procedure TMainForm.ConnectClick(Sender: TObject);
begin
Stream1.SetSize(0);
StatusBar.SimpleText := 'connecting...';
ListBox1.Items.Add('connecting...');
TCPSock.Connect(Host.Text, Port.Value);
TCPcom.Connect(Host.Text, Port1.Value);
end;
procedure TMainForm.TCPcomAccept(aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPcom: connection accepted on server');
end;
procedure TMainForm.TCPcomConnect(aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPcom connected');
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
mes : AnsiString;
begin
mes := ...
ListBox1.Items.Add('sending : ' + mes);
TCPcom.SendMessage(mes);
end;
procedure TMainForm.TCPcomDisconnect(aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPcom: disconnect');
end;
procedure TMainForm.TCPcomError(const msg: string; aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPcomError: ' + msg);
end;
procedure TMainForm.TCPcomReceive(aSocket: TLSocket);
var
buff: string;
recv: integer;
hor: integer;
vert: integer;
begin
recv := aSocket.GetMessage(buff);
ListBox1.Items.Add('received ' + IntToStr(recv) + ' bytes: ' + buff);
SScanf(buff,'[PH:%d PV:%d]',[@hor,@vert]);
vertSkutPoz.Position:=vert;
horSkutPoz.Position:=hor;
end;
procedure TMainForm.TCPSock1Accept(aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPSock: connection accepted on server');
end;
procedure TMainForm.TCPSockAccept(aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPSock: connection accepted on server');
end;
procedure TMainForm.TCPSockConnect(aSocket: TLSocket);
begin
StatusBar.SimpleText := 'sending request...';
aSocket.SendMessage('GET /?action=stream' + #10+ #10);
end;
procedure TMainForm.TCPSockDisconnect(aSocket: TLSocket);
begin
ListBox1.Items.Add('TCPSock: disconnect');
end;
procedure TMainForm.TCPSockError(const msg: string; aSocket: TLSocket);
begin
StatusBar.SimpleText := 'connect failed';
ListBox1.Items.Add('TCPSock error: ' + msg);
end;
procedure TMainForm.TCPSockReceive(aSocket: TLSocket);
var
res, i, j : Integer;
begin
res := aSocket.Get( buffer[0], sizeof(buffer) );
if res = 0 then exit;
.....
......
.....
end;
--- End code ---
Almindor:
OnAccept events are server-side not client side (TLTcpComponent can be both a client and a server, depends on if you use .Connect or .Listen).
So you will never see the Accept event if you're connecting.
I'm not sure why it hangs for you, if you comment the other connection out, it works.. for both? (e.g: you uncomment one and then the other and it always works this way).
Btw. are you Czech?
zdenal027:
Thanks for explanation.
Exactly - when I comment out one, second runs ok. For both. I don't know if "hang" is right description - it seems that program lives - gui isn't responding, but when I click on button which sends TCP paket, paket is received on server... Strange.
Yes, I'm Czech :-)
Almindor:
Tak to zdar :)
No neviem co je presne problem, ak mozes posli mi celu aplikaciu (zdrojaky len) ja to skusim na echo serveri, bude to len nejaka somarina.
zdenal027:
Zdravím :D
Nejde o nic tajnýho, tak jsme to hodil na leteckou poštu: http://leteckaposta.cz/129955109
Dík moc. Taky si myslím, že to bude nějaká chujovina.
Navigation
[0] Message Index
[#] Next page