TLTCP components problem lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- zdenal027 · 2011-04-14 14:47 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: [Select]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; ---------------------------------------------------------------------- Almindor · 2011-04-14 16:36 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 · 2011-04-15 07:18 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 · 2011-04-15 08:20 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 · 2011-04-15 08:31 Zdravím 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. ---------------------------------------------------------------------- JD · 2011-04-15 10:49 @Almindor2 Are you the same Almindor that wrote a 2D game in FreePascal a while back? ---------------------------------------------------------------------- Almindor · 2011-04-15 19:10 @zdenal: Pozrem a uvidim Zajtra by som na to mal mat cast Quote from: JD on April 15, 2011, 12:49:10 pm@Almindor2 Are you the same Almindor that wrote a 2D game in FreePascal a while back? Yep, that's me, I lost my old "Almindor" login with my old ISP's email I had. ---------------------------------------------------------------------- Jishaxe · 2011-04-15 21:29 Quote from: Almindor2 on April 15, 2011, 09:10:54 pm@zdenal: Pozrem a uvidim Zajtra by som na to mal mat cast Quote from: JD on April 15, 2011, 12:49:10 pm@Almindor2 Are you the same Almindor that wrote a 2D game in FreePascal a while back? Yep, that's me, I lost my old "Almindor" login with my old ISP's email I had. A cooler name would have been "AlmindorTheSecond" or "UltraAlmindor" Wait, can't you just change your forum nickname to Almindor? Mine used to be Joshuzzz but now it's Jishaxe 'cause I changed it? ---------------------------------------------------------------------- Almindor · 2011-04-15 22:11 Quote from: Jishaxe on April 15, 2011, 11:29:42 pmA cooler name would have been "AlmindorTheSecond" or "UltraAlmindor" Wait, can't you just change your forum nickname to Almindor? Mine used to be Joshuzzz but now it's Jishaxe 'cause I changed it? Yeah you're right I didn't realize it can be changed from the login name. ---------------------------------------------------------------------- Jishaxe · 2011-04-15 22:13 ^^ ---------------------------------------------------------------------- JD · 2011-04-16 09:04 Quote from: Almindor on April 15, 2011, 09:10:54 pm@zdenal: Pozrem a uvidim Zajtra by som na to mal mat cast Quote from: JD on April 15, 2011, 12:49:10 pm@Almindor2 Are you the same Almindor that wrote a 2D game in FreePascal a while back? Yep, that's me, I lost my old "Almindor" login with my old ISP's email I had. I see. By the way, whatever happened to that game of yours? Are you still developing it? ---------------------------------------------------------------------- Almindor · 2011-04-16 09:17 Quote from: JD on April 16, 2011, 11:04:38 amI see. By the way, whatever happened to that game of yours? Are you still developing it? It's "shelved" atm. The latest version works (multiplayer etc.) and is generally stable, but I have other projects (like lNet) to take care of now. Not enough time for it. ---------------------------------------------------------------------- Almindor · 2011-04-16 10:08 Quote from: zdenal027 on April 15, 2011, 10:31:56 amZdravím 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. V Linuxe mi to fakci OK. Ak si zapnem server na oboch portoch a dam connect, krasne sa oba clienti pripoja, ten TCPSock posle aj nejake info a neuschyna mi to. Jedine co som tam nasiel bol problem s INIStorage kde si nemal zadefinovany port1 pricom ho citas (padlo mi to pri StrToInt ale to som poriesil). Este to skusim vo Windle. Mozno je problem niekde inde ako v pripojeni, napr. ten tvoj server mozno nieco posle co sa zatentuje tam (OnReceive?) ale to si ja neviem zopakovat kedze neviem co ti posle. Skus si zatial OnReceive dat len aby vypisal texty pre obe spojenia ak ti to neuschne tak bude problem tam. ---------------------------------------------------------------------- zdenal027 · 2011-04-21 13:59 Ahoj, díky za snahu. Tu chybu s INIStorage jsem si opravil, dík. Když vypisuju jen data co přijdou, je všechno ok. Problém vznikne asi někde při vykreslování došlého jpegu. Ještě k tomu zatuhnutí - nevytuhne to úplně - zatuhne video, přestane se cokoliv vypisovat do listboxů, ale dá se hýbat trackbarama a když kliknu na Send TCP paket, tak se paket opravdu odešle. Tlačítka se tváří že nereagují, ale kliknutím na Send... se ty data fakt odešlou. Jsem z toho volaký zmetený :-) Kdyžtak můžu poskytnout data co tomu chodí, jestli bys to měl jak vyzkoušet.... ---------------------------------------------------------------------- eny · 2011-04-21 14:20 Quote from: zdenal027 on April 15, 2011, 09:18:23 amThanks 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. Maybe the 2 components cannot run in the same (main) thread. It might help if you put one (or both) in a separate thread. ---------------------------------------------------------------------- zdenal027 · 2011-04-22 08:36 Tak už to funguje. Problém byl ve funkci co dělá vykreslování jpegu. Z pokusných důvodů jsem  zakomentoval dva řádky a ejhle... :-) Code: [Select]procedure TMainForm.DisplayFrame(stream: TStream); begin   try     stream.Position:=0;     VideoFrame.Picture.Jpeg.LoadFromStream(stream);   except     ListBox2.Items.Add('error while decoding frame!');     exit;   end;   //VideoFrame.Repaint;   //Application.ProcessMessages;   ... VideoFrame je typu TImage. ---------------------------------------------------------------------- zdenal027 · 2011-04-22 08:41 Quote from: eny on April 21, 2011, 04:20:06 pmMaybe the 2 components cannot run in the same (main) thread. It might help if you put one (or both) in a separate thread. Hello, thanks for advice, I was ready to try it, but another solution appears :-) It was caused by function which loads jpeg from stream to TImage. I commented two lines at the end (VideoFrame.Repaint; and Application.ProcessMessages;) and now it works fine. Strange...