Comp Sci Creating an Inet4Address from a String for a SocketConnection

  • Thread starter Thread starter 0rthodontist
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
To create a SocketAddress from an IP address string, use InetSocketAddress instead of InetAddress, as InetAddress cannot be directly used as a SocketAddress. The method InetAddress.getByName() can parse an IP address string effectively, despite initial confusion about its capabilities. When closing an applet with open connections, implementing a finalizer can help manage unexpected terminations. For integrating a GUI into an existing multi-threaded application, consider making the GUI a singleton or using a global access point to allow other classes to interact with it without modifying their constructors. This approach can streamline communication between the GUI and other components of the program.
0rthodontist
Science Advisor
Messages
1,229
Reaction score
0
How do I make a SocketAddress that I can pass to a Socket so that I can connect to a computer whose IP address I have as a string? I think that want I want is an Inet4Address but Inet4Address has no constructor. SocketAddress says it is meant to be "subclassed" by a protocol-specific address type, but I don't know what they mean. Is there a factory class that will do this for me?

Edit: I made my own method to parse it into a byte[] but now it won't accept an InetAddress as a SocketAddress. An InetAddress is a SocketAddress though. And it won't let me cast it. What's the deal?

Edit: Never mind, an InetSocketAddress is a SocketAddress, an InetAddress isn't. I still would like to know if there is a built-in way to make an InetAddress from a string though. In Inet4Address it seems to suggest that there is, since it specifies the format of such a string.
 
Last edited:
Physics news on Phys.org
Use InetAddress.getByName().

- Warren
 
But I'm designing the program so the user inputs an IP address, not a name. Will getByName parse an IP address?
 
Last edited:
Perhaps you should trying reading the documentation for the method. After all, that's what I did.

- Warren
 
Oh, duh! Sorry. I was looking at the documentation but only at the short description, not at the longer explanation below. Thanks for your help.

Ah, here is a further problem: when I close my applet if I have a connection open it always generates an error on the console. Is there any way to make the applet close its own connections when terminated unexpectedly?
 
Last edited:
Use a finalizer.

- Warren
 
Thanks for your help so far. I have another question:

I'm adding a GUI to an existing program, written by someone else. This program has a bunch of different classes and spawns a new thread for each user who connects. What I'd like to do is have the GUI be 1 class, and let any other class call a method in the GUI (e.g. add a user name to the user list display) without me having to alter their constructors to explicitly pass them the GUI. I basically want the GUI to somehow be "global" over several classes. Is this possible? One klunky way I can think of is that I could set up the GUI as its own thread with a port and ServerSocket, and other classes communicate to it via the port.
 
Last edited:

Similar threads

Replies
2
Views
2K
Replies
12
Views
2K
Replies
8
Views
2K
Replies
2
Views
1K
Replies
1
Views
3K
Back
Top