- This topic has 4 replies, 4 voices, and was last updated 11 years, 8 months ago by Anonymous.
-
AuthorPosts
-
March 20, 2013 at 15:28 #587AnonymousInactive
Hi, I would like to start by saying thank you, you guys have done a great job. I am pretty new at programming and so far I have gone pretty far with my first client/server program thanks to your work.
I don’t quite understand how to use the handlers for connections that are first establish and then closed. Is there any chance I could get an example in C#. I know I am probably asking for something super simple but I am still trying to get a grasp on programming.
March 20, 2013 at 15:54 #589MarcFKeymasterHeya IZZO, welcome to our forums and many thanks for your interest in our network library.
Ok, so the general idea of establish and close handlers is that you can trigger some code when a connection is established or closed.
If we define the following two methods:
static void ClientConnected(Connection connection) { Console.WriteLine("A new client connected - " + connection.ToString()); } static void ClientDisconnected(Connection connection) { Console.WriteLine("A client has disconnected - " + connection.ToString()); }
all they do is write some text to a console window. You could perform whatever you want.
We can then tell NetworkComms what to do with these, either on a global level, so that the methods are executed for all connections:
NetworkComms.AppendGlobalConnectionEstablishHandler(ClientConnected); NetworkComms.AppendGlobalConnectionCloseHandler(ClientDisconnected);
or on a connection specific level:
Connection newConnection = TCPConnection.GetConnection(new ConnectionInfo("127.0.0.1", 10000)); newConnection.AppendShutdownHandler(ClientDisconnected);
There is no connection specific establish handler method because it would be superfluous.
Hope that’s what you were after, if not please let me know which bit you are unsure about.
Marc
April 25, 2013 at 17:30 #783AnonymousInactiveHi Developers,
I have the following in my code.
NetworkComms.AppendGlobalConnectionEstablishHandler(onConnection); NetworkComms.AppendGlobalConnectionCloseHandler(onConnectionClose); _tcpInfo = new ConnectionInfo(endpoint); client = TCPConnection.GetConnection(_tcpInfo);
However, when the client establishes the connection with the server, the onConnection method is never fired on the client side when the connection with the server is successful.
On the server side, I have similar code and the method fires.
However, I need my client to know that it was successful connecting to the server and proceed.
Any assistance would be appreciated.
April 25, 2013 at 17:57 #784AnonymousInactiveheya gregj,
Thanks for the post. I agree the connection establish handler should probably be called on the client side. We’ll create a bug report and try to ensure this gets fixed in the next release.
In the mean time if the method
client = TCPConnection.GetConnection(_tcpInfo);
returns without exceptions you can safely assume the connection has been succesfully established. This should allow you to use the following as a temporary workaround:
client = TCPConnection.GetConnection(_tcpInfo); //If we get to this point without an exception trigger the connection handler manually onConnection(client);
Regards,
Marc
April 25, 2013 at 18:34 #785AnonymousInactiveThanks Marc,
That is basically what I’ve been doing, would just prefer a confirmation event.
-Greg
-
AuthorPosts
- You must be logged in to reply to this topic.