HighTechTalks DotNet Forums  

Socket.getLocalAddress() Fails

Dotnet VJSharp microsoft.public.dotnet.vjsharp


Discuss Socket.getLocalAddress() Fails in the Dotnet VJSharp forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Pete Loveall
 
Posts: n/a

Default Socket.getLocalAddress() Fails - 11-13-2004 , 12:03 PM






I have a Java server application that opens a ServerSocket with just the
port number -> new ServerSocket(12345, 50, null); I have also tested this
with ServerSocket(12345, 50); In Java, when a Socket is returned from the
accept() method (call it s for clarity), a call to s.getLocalAddress()
returns the IP address of where the connection is made on the server. In
other words, if the connection from the client is to 127.0.0.1, then
s.getLocalAddress() returns an InetAddress set to 127.0.0.1

J#, however, returns an undefined setting (according to the debugger) and
causes a NullException if that value is used. I am using J# 1.1.4322.

I believe that this is the only thing that is keeping me from distributing a
..NET version of this application. Due to the architecture of the
application and its heavy use in the Java form, I cannot eliminate these
calls. I note that the converter to C# flags getLocalAddress() as
unsupported in the .NET architecture. I hope that Microsoft might have the
developer where-with-all to determine how to properly return the local IP
address as both the MS and Sun JVM does.

Pete Loveall
AME Corp.



Reply With Quote
  #2  
Old   
Pete Loveall
 
Posts: n/a

Default Re: Socket.getLocalAddress() Fails - 11-15-2004 , 07:09 AM






You miss understood my statement. See below...

"Lars-Inge Tønnessen [VJ# MVP]" <http://emailme.larsinge.com> wrote in
message news:%23kcUMFuyEHA.1300 (AT) TK2MSFTNGP14 (DOT) phx.gbl...
Quote:
It is working for me (.net v1.1). Do you have a firewall blocking it, or
VPN connections etc?


// The Server:
public class Class1
{
public Class1()
{
try
{
System.Console.WriteLine("Starting the server on port 12345");
java.net.ServerSocket servSock = new java.net.ServerSocket( 12345, 50,
null );
servSock.accept();

System.Console.WriteLine( "->" + servSock.getLocalPort() );
Replace the above 3 lines with...

java.net.Socket s = servSock.accept();
System.out.println(s.getLocalAddress());

Quote:
System.Console.WriteLine("Ending the server");
}
catch ( Exception e )
{
e.printStackTrace();
}
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}



// The Client
The client is not involved in this bug report. For test purposes, you can
use a simple telnet client.

As stated in my original post, the problem with Socket.getLocalAddress()
occurs with the socket returned by the ServerSocket.accept() in the server
code.

Pete Loveall
AME Corp.




Reply With Quote
  #3  
Old   
Pete Loveall
 
Posts: n/a

Default Re: Socket.getLocalAddress() Fails - 11-15-2004 , 10:12 AM



I am running on WinXP Pro SP2 and on W2k3 Server. The following Class1
(with import java.net.*; in the opening) prints "null" without the quotes to
the console after connecting with telnet as you did.

public Class1()
{
try
{
ServerSocket s = new ServerSocket( 12345, 50, null );
Socket ss = s.accept();
System.out.println( ss.getLocalAddress() );
}
catch ( Exception e )
{
e.printStackTrace();
}
}

"Lars-Inge Tønnessen [VJ# MVP]" <http://emailme.larsinge.com> wrote in
message news:%23dkwtrxyEHA.2656 (AT) TK2MSFTNGP14 (DOT) phx.gbl...
Quote:
More info please. This is still working:

// SERVER
public class Class1
{
public Class1()
{
try
{
java.net.ServerSocket s = new java.net.ServerSocket( 12345 );
java.net.Socket ss = s.accept();
System.Console.WriteLine( ss.getLocalAddress() );
System.Console.WriteLine( ss.getLocalAddress().getLocalHost() );
}
catch ( Exception e )
{
e.printStackTrace();
}
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}



Client:
c:...\bin\Debug>telnet localhost 12345


Server output:
c:...\bin\Debug>J_SocketBug.exe
xxxxxx.xxx-xxx.net/193.9x.xx.xx


Regards,
Lars-Inge Tønnessen





Reply With Quote
  #4  
Old   
Pete Loveall
 
Posts: n/a

Default Re: Socket.getLocalAddress() Fails - 11-15-2004 , 10:25 AM



System.Console.WriteLine(ss.getLocalAddress().getL ocalHost());

This returns a local IP because getLocalHost() is a static method and so
ss.getLocalAddress() is not evaluated. In fact, since you were telneting to
localhost, you should get 127.0.0.1 returned from ss.getLocalAddress(), not
null. System.Console.WriteLine() prints a blank line for a null value (this
is why you didn't see any problem). I recommend using System.out.println()
since that will print the word null if the object passed to it is null.

Thanks for taking a look at this.

Pete Loveall
AME Corp.

"Lars-Inge Tønnessen [VJ# MVP]" <http://emailme.larsinge.com> wrote in
message news:%23dkwtrxyEHA.2656 (AT) TK2MSFTNGP14 (DOT) phx.gbl...
Quote:
More info please. This is still working:

// SERVER
public class Class1
{
public Class1()
{
try
{
java.net.ServerSocket s = new java.net.ServerSocket( 12345 );
java.net.Socket ss = s.accept();
System.Console.WriteLine( ss.getLocalAddress() );
System.Console.WriteLine( ss.getLocalAddress().getLocalHost() );
}
catch ( Exception e )
{
e.printStackTrace();
}
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}



Client:
c:...\bin\Debug>telnet localhost 12345


Server output:
c:...\bin\Debug>J_SocketBug.exe
xxxxxx.xxx-xxx.net/193.9x.xx.xx


Regards,
Lars-Inge Tønnessen





Reply With Quote
  #5  
Old   
Pete Loveall
 
Posts: n/a

Default Re: Socket.getLocalAddress() Fails - 11-15-2004 , 10:38 AM



One further test which also returns null is if you use an explicit
InetAddress in the ServerSocket definition (e.g. ServerSocket( 12345, 50,
InetAddress.getByName("localhost") )). I did print
InetAddress.getByName("localhost") just to make sure and it correctly
printed 127.0.0.1. However, ss.getLocalAddress() still returned null.

Pete Loveall
AME Corp.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.