MFC winsock question
Herbert W Stiel -- herb+@CMU.EDU
Friday, January 19, 1996
I'd like to implement a ping like function. It doesn't have to be ping
in the formal sense - I just need to see if a computer on the Internet
is listening on a specific port. I would like my program to try to
connect on the specific port of the remote computer and if it can't to
do one thing and if it can to do another.
I would like to stick with the CArchive and CSocket approach and not
deal with WSA stuff if possible (and besides, it seems Microsoft is
pushing this approach). The problem I'm having doing it this way is
that while it is trying to connect, the user of the program can't do
anything else. I would like this to work in the background. Would
creating another thread for this work in this respect? Another problem
I'm having trouble resolving is if the remote computer's address (as
entered by the user) is invalid. The CSocket->Connect() approach just
sits there for a long time and then tells the user that it can't
connect. I would like this to be in the background, at least, and if
possible, implelment some kind of quick look up mechanism to see if the
user entered address exists. I would appreciate sample code if possible.
Thank you very much.
Herb
Doug Boone -- dboone@zeus.clr.com
Monday, January 22, 1996
>I would like to stick with the CArchive and CSocket approach and not
>deal with WSA stuff if possible (and besides, it seems Microsoft is
>pushing this approach). The problem I'm having doing it this way is
>that while it is trying to connect, the user of the program can't do
>anything else. I would like this to work in the background. Would
>creating another thread for this work in this respect?
Using another thread would help, but if you're on an operating system that
handles threads then it should be multitasking cleanly enough that the delay
isn't a problem already. Depends on what you want the user to be able to do
while they're waiting.
A different approach would be to create a special datagram socket on both
the client and server and let them play "ping" with each other. The client
just fires off a SendTo() and then expects to get a corresponding datagram
back from the server within some time period. This is not a real
"connection" so its not reliable, but its very inexpensive.
>Another problem
>I'm having trouble resolving is if the remote computer's address (as
>entered by the user) is invalid. The CSocket->Connect() approach just
>sits there for a long time and then tells the user that it can't
>connect. I would like this to be in the background, at least, and if
>possible, implelment some kind of quick look up mechanism to see if the
>user entered address exists. I would appreciate sample code if possible.
How about gethostbyaddr()?
Walter Gillett -- walter@mathworks.com
Tuesday, January 23, 1996
[Mini-digest: 5 responses]
Herbert W Stiel wrote:
> I would like to stick with the CArchive and CSocket approach and not
> deal with WSA stuff if possible (and besides, it seems Microsoft is
> pushing this approach). The problem I'm having doing it this way is
> that while it is trying to connect, the user of the program can't do
> anything else. I would like this to work in the background. Would
> creating another thread for this work in this respect?
You should be able to use multithreading -- the documentation says that
it works with sockets. Another possibility is to use CAsyncSocket with
asynchronous calls.
> Another problem
> I'm having trouble resolving is if the remote computer's address (as
> entered by the user) is invalid.
There are WSA calls that will check the address for you asynchronously,
e.g., WSAAsyncGetHostByName for a host name, or you could run the Connect
in a separate thread if you prefer to avoid WSA.
-Walter
--
Walter Gillett email: walter@mathworks.com
The Mathworks, Inc. http://www.mathworks.com
(508) 647-7344 info: info@mathworks.com
-----From: Marty Fried
At 02:35 PM 1/19/96 -0500, you wrote:
> [cut - I've had no experience with ping-like comm]
>I would like to stick with the CArchive and CSocket approach and not
>deal with WSA stuff if possible (and besides, it seems Microsoft is
>pushing this approach). The problem I'm having doing it this way is
>that while it is trying to connect, the user of the program can't do
>anything else. I would like this to work in the background. Would
>creating another thread for this work in this respect? Another problem
>I'm having trouble resolving is if the remote computer's address (as
>entered by the user) is invalid. The CSocket->Connect() approach just
>sits there for a long time and then tells the user that it can't
>connect. I would like this to be in the background, at least, and if
>possible, implelment some kind of quick look up mechanism to see if the
>user entered address exists. I would appreciate sample code if possible.
I haven't used the MFC or WSA stuff, just the blocking calls, but I came
up with some things that work, including the connect problem.
For connect, I created a function like MyConnect that takes a timeout value,
and whatever other data is needed. It creates a worker thread, suspended,
passing the thread a pointer to some data to use. Then it calculates a
quitting time from the timeout value, calls resume thread, and in a while loop,
WaitForSingleObject on the thread handle with a timeout value of a second or
less - however long you want it to be unresponsive. Then, assuming it doesn't
connect, I check for overall timeout, check for escape key having been pressed,
then continue back with WaitForSingleObject. The loop is ended when either the
connect is successful, user presses escape key, or timeout occurs. It seems that
closing the socket causes the connect function to immediately end.
For sending and receiving, I do something similar, but I have permanent threads
that I wake up with events when I want to send something.
I'd be interested in any other opinions on whether this is a reasonable way to do
things.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Marty Fried (mfried@linex.com)
Marin County, California
-----From: Brad Wilson
Doug Boone wrote:
> >I would like to stick with the CArchive and CSocket approach and not
> >deal with WSA stuff if possible (and besides, it seems Microsoft is
> >pushing this approach). The problem I'm having doing it this way is
> >that while it is trying to connect, the user of the program can't do
> >anything else. I would like this to work in the background. Would
> >creating another thread for this work in this respect?
>
> Using another thread would help, but if you're on an operating system that
> handles threads then it should be multitasking cleanly enough that the delay
> isn't a problem already. Depends on what you want the user to be able to do
> while they're waiting.
You could also try using CAsyncSocket. CSocket uses blocking operations.
--
class CBradWilson : public CWorldWatchProgrammingTeam {
public:
void GetInetAddr ( CString& s ) { s = "bradw@exptech.com"; }
void GetE164Addr ( CString& s ) { s = "+1 (810) 620-9803"; }
void GetURL ( CString& s ) { s = "http://www.exptech.com"; }
void GetDisclaimer( CString& s ) { s = "All I say is fact :-p"; }
};
// QOTW: "Don't think of yourself as the least intelligent creature in this
// room ... if you consider the entire planet, you're smarter than
// literally hundreds of people." - Dogbert to Dilbert
-----From: Herbert W Stiel
I can't seem to use gethostbyaddr. I have a CString that represents the
address the user enters. Now, I want to check if this address actually
exists and I need to do this quickly.
It seems I would have to do it like this, but this fails for some reason:
gethostbyaddr( (const char*)inet_addr( user_entered_CString ), 4, PF_INET );
Any suggestions? Or is it impossible to check really quickly if the
address exists (i.e. I should let it try to connect and wait however
long it takes before it decides it can't connect)?
Thanks for your help,
Herb
-----From: Herbert W Stiel
How does one change the amount of time Windows tries to connect to a
remote host before it gives up? I'd like to shorten this setting if
possible.
(I'm using the CSocket/CArchive approach and I'd prefer to keep it that
way if I can)
Thanks,
Herb
| Вернуться в корень Архива
|