Creating an Inet4Address from a String for a SocketConnection

  • Context: Comp Sci 
  • Thread starter Thread starter 0rthodontist
  • Start date Start date
  • Tags Tags
    String
Click For Summary

Discussion Overview

The discussion revolves around creating a SocketAddress from a string representation of an IP address for use in socket connections. Participants explore methods for converting strings to InetAddress and address issues related to managing connections in an applet environment. The scope includes technical explanations and programming challenges.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant inquires about creating a SocketAddress from a string IP address and expresses confusion regarding the lack of a constructor for Inet4Address.
  • Another participant suggests using InetAddress.getByName() as a solution for converting the string to an InetAddress.
  • A participant questions whether getByName() can parse an IP address, indicating a need for clarification on its functionality.
  • There is a mention of an error generated when closing an applet with an open connection, prompting a request for advice on managing connections during unexpected terminations.
  • A suggestion is made to use a finalizer to handle connection closure.
  • A participant raises a new question about integrating a GUI into an existing multi-threaded program without modifying the constructors of other classes, proposing a potential solution involving a separate thread and ServerSocket for communication.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to handle the conversion of IP addresses and managing applet connections. There is no consensus on the optimal method for integrating the GUI into the existing program structure.

Contextual Notes

Participants reference documentation and methods without fully resolving the implications of their suggestions. The discussion includes assumptions about the behavior of certain methods and the architecture of the existing 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 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K