Threaded TCP socket program incorrectly reporting establishment of connection

S

shofu_au

Hi Group,

A question about threads and asynchronous TCP sockets. In the
attached code, even if the host TCP server is not running my code
reports that the connection has been established.

With the Server NOT listening I get (Even actually the system powered
off)

connectHost Started
connectHost Complete
connectHostCallback Started
connectHostCallback Socket Connected

Why is the attached code reporting that the connection is establish
ed?

If I call a sligtly modified version of the same code in a single
class the connection status is reported correctly.

Thanks

Mark

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ASYNC
{
class Program
{
static void Main(string[] args)
{
ConnectionClass client = new ConnectionClass();
Thread firstThread = new Thread(new
ThreadStart(client.connectHost));
firstThread.Name = "Client";
firstThread.Start();
Thread.Sleep(10000);
}
}

public class ConnectionClass
{
private static Socket socketHost;

public ConnectionClass()
{
socketHost = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}

private void connectHostCallback(IAsyncResult asyncResult)
{
Console.WriteLine("connectHostCallback Started");
Socket sock = (Socket)asyncResult.AsyncState;

try
{
sock.EndConnect(asyncResult);
Console.WriteLine("connectHostCallback Socket
Connected");
}
catch
{
Console.WriteLine("connectHostCallback Socket Failed
to Connect");
}
}

public void connectHost()
{
Console.WriteLine("connectHost Started");
IPEndPoint remoteServerEndPoint = new
IPEndPoint(IPAddress.Parse("192.168.1.10"), 8888);
AsyncCallback asyncConnectHost = new
AsyncCallback(connectHostCallback);
IAsyncResult statusConnect =
socketHost.BeginConnect(remoteServerEndPoint, asyncConnectHost,
socketHost);
Console.WriteLine("connectHost Complete");
}

public class StateObject
{
public Socket stateSocket = null;
public const int BufferSize = 1500;
public byte[] buffer = new byte[BufferSize];
}
}
}
 
P

Peter Duniho

[...]
Why is the attached code reporting that the connection is establish
ed?

If I call a sligtly modified version of the same code in a single
class the connection status is reported correctly.

You say that a "slightly modified version of the same code" works fine.
Why did you not provide details as to what you changed in the code to make
it work? It seems to me that would be useful information.

That said, it appears to me that the issue you're having is that the
thread where you issue the async connection request exits before the
request has completed. When that happens, the operation completes
immediately and without error. I suspect that it wasn't moving the code
into a single class that caused your problem to go away, but rather not
creating the extra thread.

I don't understand why the AsyncResult appears to be tied to a thread, and
I don't understand why EndConnect doesn't throw an exception, given that
the socket isn't connected when you call it. But I suspect that's a .NET
thing that other, more experience .NET folks can comment on.

For your immediate needs, I would recommend not creating a new thread just
for the purpose of starting the connection request. It doesn't seem to
serve any purpose (the code in the thread executes nearly instantly) and
is the fundamental cause of the issue you're seeing.

Pete
 
S

shofu_au

Hi Group,

Yes it appears to be becuase the of the thread. Confusing for those
trying to understand and use threads in C# and .NET.

Please find attached the modified version I was talking about that
works without the thread.

Why does the above code report incorrect the connection succeed when
you start the connect request in a sepearte thread?

What mod for my interest would need to be make to have the above code
that is started via thread.start to report the same as the below code?

connectHost Started
connectHost Complete
connectHostCallback Started
connectHostCallback Socket Failed to Connect

Thanks

Mark


using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ASYNC
{
class Program
{
static void Main(string[] args)
{
ConnectionClass client = new ConnectionClass();
// Thread firstThread = new Thread(new
ThreadStart(client.connectHost));
// firstThread.Name = "Client";
// firstThread.Start();
client.connectHost();
Thread.Sleep(50000);
}
}

public class ConnectionClass
{
private static Socket socketHost;

public ConnectionClass()
{
socketHost = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}

private void connectHostCallback(IAsyncResult asyncResult)
{
Console.WriteLine("connectHostCallback Started");
Socket sock = (Socket)asyncResult.AsyncState;

try
{
sock.EndConnect(asyncResult);
Console.WriteLine("connectHostCallback Socket
Connected");
}
catch
{
Console.WriteLine("connectHostCallback Socket Failed
to Connect");
}
}

public void connectHost()
{
Console.WriteLine("connectHost Started");
IPEndPoint remoteServerEndPoint = new
IPEndPoint(IPAddress.Parse("192.168.1.10"), 8888);
AsyncCallback asyncConnectHost = new
AsyncCallback(connectHostCallback);
IAsyncResult statusConnect =
socketHost.BeginConnect(remoteServerEndPoint, asyncConnectHost,
socketHost);
Console.WriteLine("connectHost Complete");
}


public class StateObject
{
public Socket stateSocket = null;
public const int BufferSize = 1500;
public byte[] buffer = new byte[BufferSize];
}
}
}
 
P

Peter Duniho

[...]
Please find attached the modified version I was talking about that
works without the thread.

Where? You posted code, but it looks to me exactly like the first version
of the code you posted?
Why does the above code report incorrect the connection succeed when
you start the connect request in a sepearte thread?

I do not know. Obviously there's some sort of connection between the
AsyncResult and the thread that causes the async operation to complete as
soon as the thread exits. A cursory look at the .NET documentation didn't
show me anything that suggests why that would be the case and I can't
think of any reason off the top of my head that would justify that
behavior (after all, the callback is run on a different thread anyway, so
why should it matter whether the thread that originally started the
operation is still around or not?).
What mod for my interest would need to be make to have the above code
that is started via thread.start to report the same as the below code?

As I said, I see no reason to use a thread at all. You gain nothing, at
least in the example code you've provided.

However, if you feel that you really need to use a thread, then you need
the thread to stay alive until the operation completes. The most obvious
solution there is to simply not use an async operation in the first
place. Just make it a synchronous operation and allow the thread to wait
until the operation has completed.

If you insist on using a thread even when you don't need one, and on top
of that insist on using the async pattern even when you don't need to,
then you can synchronize the thread you create with the operation by
creating a waitable event (eg AutoResetEvent) and using the WaitOne()
method on that event in the thread you create to make the thread pause at
that point. Then in your callback, after you're done processing the async
result, call the Set() method on the waitable event to release the thread
you previously created.

For example:

...

/* Create the waitable event, initialized to unsignaled */
private AutoResetEvent _evtCallback = new AutoResetEvent(false);

private void connectHostCallback(IAsyncResult asyncResult)
{
... /* processing code goes first */

/* Set (signal) the event */
_evtCallback.Set();
}

public void connectHost()
{
... /* code to initiate async operation goes here */

/* Wait here until the event is signaled */
_evtCallback.WaitOne();

Console.WriteLine("connectHost Complete");
}

...

I reiterate: I do not see the need to create a new thread in the first
place. The only code that the thread executes happens basically
immediately and you only slow things down by creating a thread where the
code should run. Furthermore, if you insist on going to the effort to
create the thread, if you're going to just make the thread pause and wait
until the operation has completed then you should not bother making the
operation asynchronous in the first place; make it synchronous and let any
waiting that happens in the thread happen naturally by simply waiting for
the synchronous operation complete within the thread.

The implementation you've chosen appears to me to be about the least
efficient way you could have chosen.

Pete
 
S

shofu_au

Hi Pete,

Thanks for your reply. Your answer is exactly what I was lokking for
with the event information.

Yes I agree with your about the efficiency of the code, however that
was created to demonstrate the issue in a simple example.
I thought that was the easiest way to demonstrate the issue I was
observing.

Thanks. Off to read alot more about events.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top