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
connectHostCall back Started
connectHostCall back 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.Collecti ons.Generic;
using System.Text;
using System.Threadin g;
using System.Net;
using System.Net.Sock ets;
using System.IO;
namespace ASYNC
{
class Program
{
static void Main(string[] args)
{
ConnectionClass client = new ConnectionClass ();
Thread firstThread = new Thread(new
ThreadStart(cli ent.connectHost ));
firstThread.Nam e = "Client";
firstThread.Sta rt();
Thread.Sleep(10 000);
}
}
public class ConnectionClass
{
private static Socket socketHost;
public ConnectionClass ()
{
socketHost = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
}
private void connectHostCall back(IAsyncResu lt asyncResult)
{
Console.WriteLi ne("connectHost Callback Started");
Socket sock = (Socket)asyncRe sult.AsyncState ;
try
{
sock.EndConnect (asyncResult);
Console.WriteLi ne("connectHost Callback Socket
Connected");
}
catch
{
Console.WriteLi ne("connectHost Callback Socket Failed
to Connect");
}
}
public void connectHost()
{
Console.WriteLi ne("connectHos t Started");
IPEndPoint remoteServerEnd Point = new
IPEndPoint(IPAd dress.Parse("19 2.168.1.10"), 8888);
AsyncCallback asyncConnectHos t = new
AsyncCallback(c onnectHostCallb ack);
IAsyncResult statusConnect =
socketHost.Begi nConnect(remote ServerEndPoint, asyncConnectHos t,
socketHost);
Console.WriteLi ne("connectHos t Complete");
}
public class StateObject
{
public Socket stateSocket = null;
public const int BufferSize = 1500;
public byte[] buffer = new byte[BufferSize];
}
}
}