1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java socket

  1. Apr 1, 2006 #1

    0rthodontist

    User Avatar
    Science Advisor

    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: Apr 1, 2006
  2. jcsd
  3. Apr 1, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use InetAddress.getByName().

    - Warren
     
  4. Apr 1, 2006 #3

    0rthodontist

    User Avatar
    Science Advisor

    But I'm designing the program so the user inputs an IP address, not a name. Will getByName parse an IP address?
     
    Last edited: Apr 1, 2006
  5. Apr 1, 2006 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Perhaps you should trying reading the documentation for the method. After all, that's what I did.

    - Warren
     
  6. Apr 1, 2006 #5

    0rthodontist

    User Avatar
    Science Advisor

    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: Apr 1, 2006
  7. Apr 1, 2006 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use a finalizer.

    - Warren
     
  8. Apr 2, 2006 #7

    0rthodontist

    User Avatar
    Science Advisor

    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: Apr 2, 2006
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java socket
  1. Socket confirmation (Replies: 0)

  2. Java program (Replies: 3)

  3. Java prob. (Replies: 1)

  4. HeapSort Java (Replies: 3)

Loading...