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];
}
}
}
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];
}
}
}