Z
zzppallas
I have a Socket Server and a client, but the server will not
always online, so the client will try to connect to server at a proper
inteval, when the server is available, the client will connect to it
auto.
I design a thread to do this work, every 1 second, it check
Socket.Connected, if it becomes False, the thread invoke asynchrous
BeginConnect, WHEN I TEST THE CLIENT WITHOUT SERVER RUN,
that is to say, EndConnect will never connect to the server. BUG'S
APEAR, after 2 to 3 minutes,
System throw following exception:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System"
StackTrace:
at System.Net.Sockets.Socket.ConnectCallback()
at System.Net.Sockets.Socket.RegisteredWaitCallback(Object
state, Boolean timedOut)
at
System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object
state, Boolean timedOut)
Any one can help me to treat the problem?
BTW, Development Evn.
..NET Framework 2.0
Windows2003 SP1
VS2005
Test Code As Follow;
private void Form1_Load(object sender, EventArgs e)
{
while (true)
{
try
{
// if i must switch to Socket.Connect(...)?
FSocket.BeginConnect("127.0.0.1", 6500, new
AsyncCallback(ConnectCallback), this);
}
catch
{
}
System.Threading.Thread.Sleep(1000); // Sleep(100)
will see the exception more quick
}
}
static void ConnectCallback(IAsyncResult ar)
{
try
{
Form1 This = (Form1)ar.AsyncState;
This.FSocket.EndConnect(ar);
}
catch (SocketException)
{
}
}
always online, so the client will try to connect to server at a proper
inteval, when the server is available, the client will connect to it
auto.
I design a thread to do this work, every 1 second, it check
Socket.Connected, if it becomes False, the thread invoke asynchrous
BeginConnect, WHEN I TEST THE CLIENT WITHOUT SERVER RUN,
that is to say, EndConnect will never connect to the server. BUG'S
APEAR, after 2 to 3 minutes,
System throw following exception:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System"
StackTrace:
at System.Net.Sockets.Socket.ConnectCallback()
at System.Net.Sockets.Socket.RegisteredWaitCallback(Object
state, Boolean timedOut)
at
System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object
state, Boolean timedOut)
Any one can help me to treat the problem?
BTW, Development Evn.
..NET Framework 2.0
Windows2003 SP1
VS2005
Test Code As Follow;
private void Form1_Load(object sender, EventArgs e)
{
while (true)
{
try
{
// if i must switch to Socket.Connect(...)?
FSocket.BeginConnect("127.0.0.1", 6500, new
AsyncCallback(ConnectCallback), this);
}
catch
{
}
System.Threading.Thread.Sleep(1000); // Sleep(100)
will see the exception more quick
}
}
static void ConnectCallback(IAsyncResult ar)
{
try
{
Form1 This = (Form1)ar.AsyncState;
This.FSocket.EndConnect(ar);
}
catch (SocketException)
{
}
}