Any1 Can Help me for the Socket.EndConnect Bug?

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)
{
}
}
 
P

Peter Duniho

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"

So, what variable is null? I suspect it's the Form1.FSocket member
variable, but if you're going to ask a question, you need to be specific
about the error.

You also did not post all of the code. In particular, you didn't post
the code that polls Socket.Connected, so that we can see what you do in
that case. I'm guessing that somewhere along the line, you set
Form1.FSocket to null, causing the exception.

Now, all that said: it's a bad idea to poll Socket.Connected at all.
You should simply treat the connection normally unless there is an error
when using the socket, and handle the lost connection at that point,
reattempting a connect. Socket.Connected isn't going to change state
until such an error occurs anyway, so you aren't gaining anything by
polling it.

Also, rather than calling BeginConnect() every second, you should only
reattempt a connection when the previous attempt has actually failed (it
will timeout for a TCP socket). So in the ConnectCallback() method, if
there is an exception calling EndConnect() (which indicates a failure to
connect), call BeginConnect() again there.

Pete
 
P

Pallas

So, what variable is null? I suspect it's the Form1.FSocket member
variable, but if you're going to ask a question, you need to be specific
about the error.

You also did not post all of the code. In particular, you didn't post
the code that polls Socket.Connected, so that we can see what you do in
that case. I'm guessing that somewhere along the line, you set
Form1.FSocket to null, causing the exception.

no where to set Form1.FSocket to null;
U can Assign a new Socket to it at constructor,and run the test
program,
first connects failed but the exception can be catched.
after 3 to 5minutes an UNHANDLED EXCEPTION will be thrown,and the
callstack will give u no useful message, only the Exception Window u
can extract exception message,
the exception throwed from another unnamed thread.
Now, all that said: it's a bad idea to poll Socket.Connected at all.
You should simply treat the connection normally unless there is an error
when using the socket, and handle the lost connection at that point,
reattempting a connect. Socket.Connected isn't going to change state
until such an error occurs anyway, so you aren't gaining anything by
polling it.

Also, rather than calling BeginConnect() every second, you should only
reattempt a connection when the previous attempt has actually failed (it
will timeout for a TCP socket). So in the ConnectCallback() method, if
there is an exception calling EndConnect() (which indicates a failure to
connect), call BeginConnect() again there.

Pete

It's a test program, and u can find this UNHANDLED EXCEPTION,
maybe it's a .netframework Socket bug.
 
P

Peter Duniho

Pallas said:
no where to set Form1.FSocket to null;

Well, something is getting set to null.
U can Assign a new Socket to it at constructor,and run the test
program,

Please stop abbreviating. If you can take the time to write anything,
you can take the time to write words correctly. Many people, myself
included, find it difficult to try to interpret "IM speak". You will
find the rate of response to your questions is proportional to the
quality of the text you write. If you can't be bothered to write in
plain language, other people won't bother to read what you wrote, never
mind try to help.

Now, that said. What test program? You haven't posted a test program.
first connects failed but the exception can be catched.
after 3 to 5minutes an UNHANDLED EXCEPTION will be thrown,and the
callstack will give u no useful message, only the Exception Window u
can extract exception message,
the exception throwed from another unnamed thread.

I doubt that it's true that there is no null reference anywhere in the
call stack, given that the exception occurred. In fact,
NullReferenceExceptions are usually very easy to figure out, because all
you have to do is find the variable that's null and shouldn't be.

Still, if you post no code that anyone can test with, it is impossible
for anyone else to make an attempt to help you (other than explaining a
better way to write the code you did post, which I've already done).
It's a test program, and u can find this UNHANDLED EXCEPTION,
maybe it's a .netframework Socket bug.

It's not a .NET Framework Socket bug. What the bug is, I can't say.
You haven't bothered to post code that anyone could use to test to
explain to you what the problem is. But I'm sure this is a bug in your
own code, especially given the code you've already posted.

If you are unwilling to post a concise-but-complete example of code that
reliably reproduces the problem, then you should at least try making the
changes I suggested to see if that fixes your problem. The code you
posted is very wrong.

Pete
 

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