| View previous topic :: View next topic |
| Author |
Message |
mat Guest
|
Posted: Fri Feb 09, 2007 8:09 am Post subject: socket issue |
|
|
hello,im using a socketconnection component to connect to a
datasnap server.i do socketconnection->connected=true; the code is in a
separate thread and i have some application->processmessages in order to get
some visual
feedback (e.g. a messagebox before it starts connected and a messagebox if
it connects).
the code is of course in a try catch .
the issue is the following:
when i try to connect to an existing pc it does what its supposed to do
almost immediately,
if the datasnap server is up it connects ,if its not up (e.g. if im not
running the scktsrv.exe)it throws an exception.
when i try to connect to an not existing pc (wrong ip) the system freezes
and waits for a rather
big timeout.
if i try to connect to a firewalled pc (which blocks such incomming) it
freezes again and waits for
a rather big timeout again.
is this something i can fix?the waiting timeout is very big,and the program
freezes (although its in its own
thread).
i thought of first pinging the remote pc to see if its up and then try to
connect to it but that would introduce
another waiting time(the ping waiting time)
what do people usually do to deal with this kind of problems?
thks |
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Fri Feb 09, 2007 11:48 pm Post subject: Re: socket issue |
|
|
mat wrote:
| Quote: | hello,im using a socketconnection component to connect to a
|
I would point you to Remy's previous answer:
From: "Remy Lebeau \(TeamB\)"
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: socketconnection-thread issue
Date: Wed, 10 Jan 2007 00:23:36 -0800
Message-ID: <45a4a254$1 (AT) newsgroups (DOT) borland.com>
Google it or click the Message-ID |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Feb 10, 2007 12:35 am Post subject: Re: socket issue |
|
|
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45cbd7c8$1 (AT) newsgroups (DOT) borland.com...
| Quote: | hello,im using a socketconnection component
|
Which component specifically?
| Quote: | the code is in a separate thread and i have some
application->processmessages
in order to get some visual feedback (e.g. a messagebox before
it starts connected and a messagebox if it connects).
|
You should not be calling Application->ProcessMessages() in a worker
thread. Only call it in the main thread. Besides, you don't need it
anyway. You can call the Win32 API MessageBox() directly in a worker
thread without processing any messages at all, as it has its own
message processing internally.
| Quote: | if its not up (e.g. if im not running the scktsrv.exe)it throws an
exception. |
As it should be.
| Quote: | when i try to connect to an not existing pc (wrong ip) the system
freezes and waits for a rather big timeout.
|
Again, as it should be. If the IP cannot be reached immediately, it
can take a long time for the OS to figure out whether the IP is
actually valid or not.
| Quote: | if i try to connect to a firewalled pc (which blocks such incomming)
it freezes again and waits for a rather big timeout again.
|
See above. If the firewall is blocking communications, then it is
likely also blocking the packets that the OS needs to determine
whether the machine is present or not at the IP.
| Quote: | is this something i can fix?
|
The only thing you can do is implement your own timeout on the
connection manually. You did not say whether the socket was
asynchronous or synchronous. It makes a big difference in how to
implement such a timeout.
| Quote: | the waiting timeout is very big
|
Such is the nature of sockets, especially on Windows. Microsoft likes
to use large timeouts that cannot be changed.
| Quote: | the program freezes (although its in its own thread).
|
The program cannot be freezing unless you are making your main thread
wait on the socket thread while it is waiting on the socket.
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
|
| Back to top |
|
 |
mat Guest
|
Posted: Sat Feb 10, 2007 11:41 pm Post subject: Re: socket issue |
|
|
im using the tscoketconnection component from the datasnap tool categorie to
connect to
a datasnap server(builder 2006).
| Quote: | You should not be calling Application->ProcessMessages() in a worker
thread.
|
but if i dont call this in the thread when i want to show something in a
label in the main
thread(form) then it doesnt show up,eg:(all this in my separate thread):
MainForm->Label1->Caption="connecting...";
Application->ProcessMessages();
DataModule->SocketConnection1->Connected=true;
if i dont put the processmessages it doesnt show the label.
concerning the sychronous-asychronous mode,i dont know how to change this in
the
component im using.
maybe im using the thread in a wrong way in the first place.i have created a
thread to do
the connecting but this new thread accesses a component(socket
connection)from
a datamodule.is this the reason that it forces the program to freeze?
eg:
Mythread::Connect()
{
DataModule->SocketConnection->Connected=true;
} |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 12, 2007 3:36 am Post subject: Re: socket issue |
|
|
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45ce03ae$1 (AT) newsgroups (DOT) borland.com...
| Quote: | if i dont call this in the thread when i want to show something in
a label in the main thread(form) then it doesnt show up
|
You are not supposed to be updating the controls of the main thread
from within the context of a worker thread to begin with. For a
worker thread to safely access the UI, it should be using the
TThread::Synchronize() method for that. This is clearly stated at the
top of every unit that the IDE generates when you choose the "File |
New | Thread" wizard. For example:
void __fastcall TWorkerThread::Execute()
{
//...
Synchronize(UpdateUI);
//...
}
void __fastcall TWorkerThread::UpdateUI()
{
MainForm->Label1->Caption = "connecting...";
DataModule->SocketConnection1->Connected = true;
}
| Quote: | concerning the sychronous-asychronous mode,i dont know how
to change this in the component im using.
|
You still haven't said which component you are actually using. I
asked you that question in my last reply.
| Quote: | i have created a thread to do the connecting but this new thread
accesses a component(socket connection)from a datamodule.is
this the reason that it forces the program to freeze?
|
No.
Gambit |
|
| Back to top |
|
 |
mat Guest
|
Posted: Mon Feb 12, 2007 6:44 am Post subject: Re: socket issue |
|
|
Thks about the answers,i said that im using
the tscoketconnection component from the datasnap tool categorie
this tool categories has:
tdcomconnection
tsocketconnection
tsimpleobjectbroker etc.. |
|
| Back to top |
|
 |
mat Guest
|
Posted: Mon Feb 12, 2007 7:09 am Post subject: Re: socket issue |
|
|
void __fastcall TAuthThread::Connect()
{
AuthDataModule->SocketConnection1->Connected=true;
}
void __fastcall TAuthThread::MyFUNCTION()
{
//do something
Synchronize(Connect);
}
this produces error:could not find a match for
tthread:synchronize(void)
i guess i wrote it wrong,how must i fix this?
i checked this->connect() but creates the same error |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 12, 2007 8:50 am Post subject: Re: socket issue |
|
|
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45cfbe36$1 (AT) newsgroups (DOT) borland.com...
| Quote: | this produces error:could not find a match for
tthread:synchronize(void)
|
I assume, then, that you are using BDS 2006? Always say the version
you are using when asking for help.
You will have to prefix the method name with '&' when calling
Synchronize(), ie:
Synchronize(&Connect);
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 12, 2007 9:10 am Post subject: Re: socket issue |
|
|
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45cfe6b7 (AT) newsgroups (DOT) borland.com...
| Quote: | i tryed that but again when i try to connect to a non existing
ip the program freezes
|
This time I would expect it to, as you are now connecting in the
context of the main thread. You weren't doing that before.
| Quote: | concerning the aychronous-sychronous mode is there anything
that i can do?
|
No. TSocketConnection is a synchronous component.
Gambit |
|
| Back to top |
|
 |
mat Guest
|
Posted: Mon Feb 12, 2007 9:10 am Post subject: Re: socket issue |
|
|
i tryed that but again when i try to connect to a non existing ip the
program freezes
concerning the aychronous-sychronous mode is there anything that i can do?
im using the tscoketconnection component from the datasnap tool categorie
borland 2006
thks |
|
| Back to top |
|
 |
mat Guest
|
Posted: Mon Feb 12, 2007 9:10 am Post subject: Re: socket issue |
|
|
so i guess nothing can be done?any suggestion?
:)
thks
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:45cfefd8$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45cfe6b7 (AT) newsgroups (DOT) borland.com...
i tryed that but again when i try to connect to a non existing
ip the program freezes
This time I would expect it to, as you are now connecting in the
context of the main thread. You weren't doing that before.
concerning the aychronous-sychronous mode is there anything
that i can do?
No. TSocketConnection is a synchronous component.
Gambit
|
|
|
| Back to top |
|
 |
mat Guest
|
Posted: Mon Feb 12, 2007 9:10 am Post subject: Re: socket issue |
|
|
if i try to ping the pc before connecting will it be a good idea?
i put an IdIcmpClient component i write simple:
AnsiString buffer;
unsigned short a;
IdIcmpClient1->Ping(buffer,a);
Application->ProcessMessages();
and in its onreply even i have
Memo1->Lines->Add(AnsiString(AReplyStatus.MsRoundTripTime) +" from
"+AReplyStatus.FromIpAddress);
i noticed that if i try ping an ip that possible doesnt exist i get a 1000+
from 0.0.0.0
or if i write a url that doesnt exist i get an exception in the timeout that
i have declared.
is this a good (i mean programming way good) way to understand if an ip
exists before
making the socketconnection->connected=true?
thks
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45cbd7c8$1 (AT) newsgroups (DOT) borland.com...
| Quote: | hello,im using a socketconnection component to connect to a
datasnap server.i do socketconnection->connected=true; the code is in a
separate thread and i have some application->processmessages in order to
get some visual
feedback (e.g. a messagebox before it starts connected and a messagebox if
it connects).
the code is of course in a try catch .
the issue is the following:
when i try to connect to an existing pc it does what its supposed to do
almost immediately,
if the datasnap server is up it connects ,if its not up (e.g. if im not
running the scktsrv.exe)it throws an exception.
when i try to connect to an not existing pc (wrong ip) the system freezes
and waits for a rather
big timeout.
if i try to connect to a firewalled pc (which blocks such incomming) it
freezes again and waits for
a rather big timeout again.
is this something i can fix?the waiting timeout is very big,and the
program freezes (although its in its own
thread).
i thought of first pinging the remote pc to see if its up and then try to
connect to it but that would introduce
another waiting time(the ping waiting time)
what do people usually do to deal with this kind of problems?
thks
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 12, 2007 9:10 am Post subject: Re: socket issue |
|
|
"mat" <mat (AT) hotmail (DOT) com> wrote in message
news:45d00321 (AT) newsgroups (DOT) borland.com...
| Quote: | if i try to ping the pc before connecting will it be a good idea?
|
No. Asside from the added overhead of the ping itself, that still
won't tell you whether the server can actually be connected to or not.
| Quote: | in its onreply even i have
Memo1->Lines->Add(AnsiString(AReplyStatus.MsRoundTripTime) +" from
"+AReplyStatus.FromIpAddress);
i noticed that if i try ping an ip that possible doesnt exist i get
a 1000+
from 0.0.0.0
|
The correct thing to do is to first look at the
AReplyStatus.ReplyStatusType member, as that tells you whether the
ping was successful or not. The rest of the members may or may not be
valid, depending on the type of reply received.
Gambit |
|
| Back to top |
|
 |
|