Jump to content
  • Advertisement
Sign in to follow this  
Icebone1000

How to cancel an attempt to connect?

This topic is 2772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im creating a (very)simple class for handling the client side of network.. I created this ConnectTo method, it receives an IP, a Port and the max time to wait the connection...

What I did is:
set the socket to non-blocking
call select specifying the max time, and put the socket on the write set, and on the except set(for failure to connect)

in the case of failures simply returns false, if sucsefull returns true..the problem is if the time expired, meaning the attempt to connect is still in progress..I dont know the best way to handle this, I just can think of
cancelling the operation, so the user can call the method again..

Is the only way to do this closing the socket? This is a pain, since I will have to recreate it before exiting from the method(not really a problem, just kind ugly) ..

The other way I though would be creating a thread and let it running until it terminates by itself(blocking, no max time to wait params)..But Im not in the mood of doing that (I want to keep really simple for now)

Share this post


Link to post
Share on other sites
Advertisement
never create a thread to handle a single connection. That is rule # 456743 .. Yeah, that rule!

Second, you dont need to wait litertally. Just set a timer, and check in to see if a connection has been made. If it hasnt after some period of time, do what you want. I think you might be over thinking the problem. Just try something out right now and see how it goes :)

Share this post


Link to post
Share on other sites

never create a thread to handle a single connection. That is rule # 456743 .. Yeah, that rule!

Second, you dont need to wait litertally. Just set a timer, and check in to see if a connection has been made. If it hasnt after some period of time, do what you want. I think you might be over thinking the problem. Just try something out right now and see how it goes :)



Hey wait! I dont get it..
That sounds weird.. I mean, since its a client, I was thinking for sure it will have only one connection, the one with the server...This makes all you said confuse to me..
I mean, everything depends of the sussefullness of this connection, otherwise the networking..well there will be no networking..
So the program will just forwards if the connection is made, otherwise it quits or backs to single player or whatever...
Also its nice to have a system to cancel a connection attempt, not just wait it to finishes( BTW how much it takes until it return a failure? is it some harcoded default time?)

Share this post


Link to post
Share on other sites

Also its nice to have a system to cancel a connection attempt, not just wait it to finishes( BTW how much it takes until it return a failure? is it some harcoded default time?)


The only way to cancel outstanding I/O is to close the socket. Thus, your API can take one of two approaches:

1) Pass in the desired timeout to the "ConnectTo" function from the client application, and wait for so long in select(). If it fails, close the socket, and return failure. Re-trying connection would re-create a new socket.

2) Immediately return after calling connect(), and provide a "poll" function that the client calls. Each time in poll(), you check whether the connect has succeded or failed. Communicate to the client program either through a is_connected() polling function, or through a callback of some sort.

In general, most network APIs that have to also work with physics, graphics, and user input, end up with a poll() function that the client application needs to call every so often (such as once per frame).

Share this post


Link to post
Share on other sites
So basically, the networking api will be sort of a "socket status" driven api? Getting whats going on with the socket, and making a switch with it?

Share this post


Link to post
Share on other sites

So basically, the networking api will be sort of a "socket status" driven api? Getting whats going on with the socket, and making a switch with it?


Yes, that's one common way to do it. Polling the network, reading information when it is ready, and then acting based on the information received.


In the FAQ you will find a list of common networking libraries. Read them and look at their documentation.

Using one of those existing libraries is generally the best option if you plan on finishing games. But even if you choose to write your own, you should still read about the common solutions.

Even if you choose to write your own instead of using them, study of the popular libraries will help you. You will more easily see patterns of effective ways to implement networked systems.

Share this post


Link to post
Share on other sites

never create a thread to handle a single connection. That is rule # 456743 .. Yeah, that rule!


Elaborate on this.

You are saying that every simple client-server based application should block when trying to establish a connection to the server, cause high-ping and/or slow connections to freeze the application. Likewise, the server should use a single thread to handle all clients, so when one client blocks, all other clients' services are being denied.

I'd really like to hear the explanation behind your "rule# 456743"... yeah, that rule.

Share this post


Link to post
Share on other sites

You are saying that every simple client-server based application should block when trying to establish a connection to the server


No, he's not saying that. You can use threads from a thread pool, that are used for many different things, for example. However, I think he's saying that you should do a non-blocking connect if you just want to create a single connection -- just like the original post in this thread describes it!

Threads are expensive, and cause synchronization headaches. Threads screw with application architecture. Best practice is that first, you should look at asynchronous I/O and evented application models. Only when you go compute bound, should you create multiple threads, and spread your work across them. At that point, you should probably break all kinds of work into roughly equal-value "work units" and farm them out across a thread pool with one thread per CPU core.

Share this post


Link to post
Share on other sites
dont handle receiving when time expired? move to the state before connection (but this works only with one case)

i you use sockets try to do sth like closesocket();

client should knew too if there is a timeout i dont understand where you have problem,try to recoding app :)




i didnt check if windows sockets can attemp next connection after connect failure but i think it can connect again without closing socket




i think your app is freezing while connect how stupid your code is?

Share this post


Link to post
Share on other sites
We could do without calling each other's code "stupid", particularly when we cannot see the code in question.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!