473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Closing Socket while not yet connected

Hi all,

I've run into a bit of a snag while dealing with asynchronous sockets. What I'm trying to do is set a timeout for which the server will listen for a connection from a client, at which point the object maintaining the server side connection will be disposed of through the use of a delegate.

The code is as follows:

Expand|Select|Wrap|Line Numbers
  1. public Bridge(string[] args, int inPort)
  2.         {
  3.             name = args[0];
  4.             surname = args[1];
  5.             password = args[2];
  6.  
  7.             // Sort out network related initialization
  8.             port = inPort;
  9.  
  10.             Logger.Log("Bridge #" + port.ToString() + ": Initializing.", Helpers.LogLevel.Info);
  11.             mainSock = new Socket(AddressFamily.InterNetwork,
  12.                                   SocketType.Stream,
  13.                                   ProtocolType.Tcp);
  14.             IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
  15.             mainSock.Bind(ipLocal);
  16.             mainSock.Listen(3);
  17.  
  18.             // Socket is initialized here
  19.             mainSock.BeginAccept(new AsyncCallback(OnRequest),null);
  20.  
  21.             timer = new Timer(new TimerCallback(OnTimeout), null, 5000, Timeout.Infinite);
  22.  
  23.             Logger.Log("Bridge #" + port.ToString() + ": Completed Initialization successfully; waiting for client request.", Helpers.LogLevel.Info);
  24.         }
  25.  
  26.         private void OnRequest(IAsyncResult result)
  27.         {
  28.             Logger.Log("Bridge #" + port.ToString() + ": Socket request received.", Helpers.LogLevel.Info);
  29.             timer.Dispose();
  30.  
  31.             // Error gets thrown here after OnTimeout is triggered
  32.             mainSock = mainSock.EndAccept(result);
  33.  
  34.             inClient = new Client((String[])Tools.ToArray(name, surname, password));
  35.  
  36.             // bind callback handlers for connections and disconnections of inClient
  37.             inClient.Network.OnLogin += new NetworkManager.LoginCallback(ConnectionHandler);
  38.             inClient.Network.OnDisconnected += new NetworkManager.DisconnectedCallback(DisconnectHandler);
  39.             inClient.Self.OnChat += new AgentManager.ChatCallback(ChatHandler);
  40.  
  41.             // wait for connection signal 
  42.         }
  43.  
  44.         private void OnTimeout(Object stateInfo)
  45.         {
  46.             Logger.Log("Bridge #" + port.ToString() + ": Connection timed out.", Helpers.LogLevel.Error);
  47.  
  48.             // Problem lies here
  49.             mainSock.Close();
  50.  
  51.             timer.Dispose();
  52.             OnBridgeTerm(port - portOffset);
  53.         }
OnBridgeTerm is an event delegate that signals its owner class to destroy it, but of course resources have to be dealt with beforehand. Unfortunately, the timeout is there to remove the socket in the situation that it has not yet established a connection, and for this I can't seem to find the right method to get rid of it.

When using Close, I find that no errors are thrown, however, once OnTimeout finishes executing, OnRequest is instantly triggered even though the client clearly has not sent a connection request at all (that code is disabled).

What is the proper way to dispose of this pending socket? Am I even setting up the connection properly to begin with?
Apr 1 '09 #1
0 1796

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
5
by: Morten | last post by:
How do I detect if a client socket is no longer connected to the listen tcp socket ? I have tried with (just an example): --------------------- Socket tcpSocket; ...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
3
by: Tom Opgenorth | last post by:
I'm experiencing a problem with sockets, and I'm really hoping someone can help me, please and thank you. I've written a TCP Server, which listens on a port for an incoming connection. When the...
2
by: Lenard Gunda | last post by:
Hi, I have the following problem when I am working with sockets in C#. I do not remember running into the same problem some while ago, when working with sockets from C++ (using native code, not...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
4
by: Funke | last post by:
Assume that in C#, I create a server socket (listener) and code to start new threads with each connection using BeginAccept(). After some time, I have three threads running, each with their own...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.