very strange System.Net.Sockets.SocketException  
Author Message
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

What can be a possible cause

When my program running on a notebook connects the server, it gets the System.Net.Sockets.SocketException.

I try to ping the server by the domain name from the notebook. It's OK. So not related to domain name resolution or network stability problem.

I try to telnet to the server from the notebook directly and issue some commands. It's OK also. So not related to any firewall.

It just happens in a few notebooks in the same network.... what can be the possible cause

The following is the code.

if(ConnectSocketThread==null || !ConnectSocketThread.IsAlive)
{
ConnectSocketThread = new Thread( new ThreadStart(this.ConnectSocketThreadFunction) );
ConnectSocketThread.Name = "ConnectSocketThread";
ConnectSocketThread.Start();
}
ConnectSocketThread.Join(7000);

private void ConnectSocketThreadFunction()
{
while(true)
{
if(socket!=null && socket.Connected)
return;
else if(socket==null)
{
Thread.Sleep(30);
}
else
{
try
{
this.endPoint = new IPEndPoint(Dns.Resolve(Global.server).AddressList[0], Global.port);
this.socket.Connect(this.endPoint);
}
catch (Exception ex)
{
MessageBox.Show("ConnectSocketThread: (" + socket.Connected.ToString() + ")\n" + ex.ToString());
return;
}
}
}
}



.NET Development17  
 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

Dns.Resolve() is depricated in .Net 2.0 so if you are using it in .Net 2.0 try to change it to Dns.GetHostEntry() etc and see what happens.

Best Regards,



 
 
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

Thanks. But I think it should not be the cause. I'm using .Net 2.0 on my PC and doesnt have any problem with the code.

The network where the notebook having problem with the code connected is a Wireless LAN. I wonder if anything can go wrong with that Wireless LAN. Any experience to share

 

 

Dns.Resolve() is depricated in .Net 2.0 so if you are using it in .Net 2.0 try to change it to Dns.GetHostEntry() etc and see what happens.

Best Regards,


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

You should atlease give it a try. They have depricated it from the newer version of .Net. May be there are some problems like this on new Wifi Tech etc.

We dont have to spend anything more than changing 2-3 lines of code. Let's See what happens.

Best Regards,



 
 
ahmedilyas





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

can you give us the specific socketexception error message

 
 
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

OK

You should atlease give it a try. They have depricated it from the newer version of .Net. May be there are some problems like this on new Wifi Tech etc.

We dont have to spend anything more than changing 2-3 lines of code. Let's See what happens.

Best Regards,


 
 
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

I dont have the error message now but I remember it's kinda the text/explanation you see in the Object browser in VS2003 for System.Net.Sockets.SocketException.

 

 
can you give us the specific socketexception error message


 
 
ahmedilyas





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

please do post back the error message you get :-) (Message or InnerException)

 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

Also see what Ilyas is saying:

See something like this:

catch(SocketExceptopn se)

{

       string exceptionMessage = String.Fotmat("Message: {0} Socket Error: {1) SocketErrorCode{2}" se.Message, se.SocketError, se.SocketErrorCode);

}

This will help in identifying exact cause of the problem!

Best Regards,



 
 
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

Thanks!!! I will try and see if I can come back with anything.

Also see what Ilyas is saying:

See something like this:

catch(SocketExceptopn se)

{

string exceptionMessage = String.Fotmat("Message: {0} Socket Error: {1) SocketErrorCode{2}" se.Message, se.SocketError, se.SocketErrorCode);

}

This will help in identifying exact cause of the problem!

Best Regards,


 
 
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

I cannot find Dns.GetHostEntry()

I now use Dns.GetHostByName(server).AddressList[0];

is that OK

Dns.Resolve() is depricated in .Net 2.0 so if you are using it in .Net 2.0 try to change it to Dns.GetHostEntry() etc and see what happens.

Best Regards,


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

Why Not

See this:

http://msdn2.microsoft.com/en-us/library/system.net.dns_methods.aspx

Check both functions and see effect of both!

Best Regards,



 
 
Cyrus Chan





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

I changed the code as follows and that computer in the Wireless LAN can connect to the server. Thanks everybody!

try
{
if(this.ipAddress==null)
{
this.ipAddress = Dns.GetHostByName(Global.server).AddressList[0];
}
this.endPoint = new IPEndPoint(this.ipAddress, Global.port);
this.socket.Connect(this.endPoint);
}
catch (SocketException se)
{
string errorMesg = String.Format("Message: {0}\nNativeErrorCode: {1)\nErrorCode: {2}", se.Message, se.NativeErrorCode, se.ErrorCode);
MessageBox.Show("ConnectSocketThread1: " + errorMesg);
MessageBox.Show("ConnectSocketThread2: " + se.ToString());
return;
}
catch (Exception ex)
{
MessageBox.Show("ConnectSocketThread3: " + ex.ToString());
return;
}

Dns.Resolve() is depricated in .Net 2.0 so if you are using it in .Net 2.0 try to change it to Dns.GetHostEntry() etc and see what happens.

Best Regards,


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, very strange System.Net.Sockets.SocketException Top

Please Mark the Helpfull Post(s) as answered .

Best Regards,