There's freeware available through VPRO (public access tv in the netherlands), written by Rico Jansen and Daniel Ockeloen that does this. The code is available for NetServer and NetClient. http://www.vpro.nl/htbin/scan/www/object/VPRO/JAVA/BETA/OBJECT
or do I need to be running TCP/IP? I tried running a small ServerSocket example I found, and I got an exception "Invalid address" when I tried to start up the listener."
You need to use TCP/IP, but it requires no setup. In your applications use "localhost" as the hostname, or TCP/IP- address 127.0.0.1. These are the predefined values for a local connection. If that should fail, install the networking stuff and choose MS Loopback adapter as your network card. This is a dummy driver for local loopback. Hope this helps!
(Submitted by Ben MacKenzie)
There is no difference between a datagram server socket and a datagram client socket - so just create a regular socket (note this is not the case if the socket is TCP based which is why a separate class for server sockets exists). There doesn't seem to be a recvfrom so you'll have to get a stream with getInputStream and then use the regular streams operations.
(Submitted by Amit Chaudhary)// this applet will give the hostname and ip address of the user // viewing the applet import java.applet.*; public class easyHostname extends Applet { TextArea ta1 = new TextArea(4, 40); public void resize() { resize(300,300); } public easyHostname() { setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); add("North", p1); p1.add(ta1); InetAddress localhost = null; try { localhost = InetAddress.getLocalHost(); } catch(UnknownHostException e) { showStatus("Can not determine host"); } ta1.setText("hostname = " + localhost.getHostName() + "\n"); ta1.appendText("address = " + localhost.toString() ); } }
Check out this peice of code:(Submitted by Don Drake)
http://www2.connectsoft.com/~amit/java/urname.java.
Contact amit@maverick.corus.com for more information.
The following class will get the IP address and hostname for the machine it is running on.(Submitted by David Johnson)-Don Drake
http://www.mcs.com/~drake
import java.net.*; public class netInfo { InetAddress i; String Hostname; String ipAddress; public netInfo() { try { i=InetAddress.getLocalHost(); Hostname=i.toString().substring(0,i.toString().indexOf('/')); ipAddress=i.toString().substring(i.toString().indexOf('/')+1); } catch (UnknownHostException e) { System.out.println("UnknownHostException!!!!!!"); } } public netInfo(String Name) { try { i=InetAddress.getByName(Name); Hostname=i.toString().substring(0,i.toString().indexOf('/')); ipAddress=i.toString().substring(i.toString().indexOf('/')+1); } catch (UnknownHostException e) { System.out.println("ERROR:"+ Name + "is an unknown Host"); } } public String getHostName() { return Hostname; } public String getIPAddress() { return ipAddress; } }
The local hostname and ip address are available to the Applet on the client side using methods getLocalHost() and getAddress() in the class java.net.InetAddress. For example,
import java.net.*; ... // Get the inet address object InetAddress ia = InetAddress.getLocalHost(); // Convert this to a network-byte-order byte array byte[] nboa = ia.getAddress(); // will have 4 elements // e.g for 127.0.0.1 nboa[0] would contain 127To find out on the server side, just put a server side include statement like the following into the top of the html page:
<!--#exec cmd="/htdocs/includes/javahost" -->where javahost is the name of a CGI script in C, PERL, a shell script, or whatever else. Whenever the server runs a CGI script it automatically dumps 19 variables into the environment for you to retreive and do with what you want. These variables include lots of useful information including the ip address of the person retrieving your file. The enviorment variable containing this string is "REMOTE_ADDR". You can then control output to that client (perhaps based on what browser they are using, another usefull CGI variable "HTTP_USER_AGENT") and/or do whatever else with it that you want. Make sure your httpd server is set up to handle server side includes.
"Certain sites, like Netscape have 20 machines which all look like home.netscape.com but actually have different IP addresses."
The folowing code will show the hostname and address of the local machine (the machine running the browser). On clicking the GetHost button, you can get the hostname of the machine that served the applet, the number of hosts that have the same name and the name + IP addresses of all the machines that have the same hostname.import java.util.*; import java.net.*; import java.awt.*; import java.applet.*; public class easyHostname extends Applet { TextArea ta1 = new TextArea(8, 40); public void resize() { resize(300,300); } public easyHostname() { setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); add("North", p1); p1.add(new Button("GetHost")); p1.add(ta1); InetAddress localhost = null; try { localhost = InetAddress.getLocalHost(); } catch(UnknownHostException e) { showStatus("Can not determine host"); } ta1.appendText("hostname = " + localhost.getHostName() + "\n"); ta1.appendText("address = " + localhost.toString() ); } public boolean action(Event e, Object o) { String host1 = null; if ("GetHost".equals(e.arg)) { ta1.appendText("\n" + "Hostname: " + getCodeBase().getHost()); host1 = getCodeBase().getHost(); try { InetAddress multihosts[] = InetAddress.getAllByName(host1); int i, j = multihosts.length; ta1.appendText("\n" + j); for (i = 0;i < j;i++) ta1.appendText("\n" + multihosts[i]); } catch(UnknownHostException e2) { showStatus("Can not determine host2"); } } return true; } }
// This is a bare bones Server class. Better error handling could // be implemented but I think this should give you a better feel for // what is going on. // March 10, 1996 // Soren M. Burkhart // Soren.M.Burkhart@ac.com import java.io.*; import java.net.*; public class MyServer extends Thread { protected int _port; // Server port number protected ServerSocket _server_socket; // Socket listening for conn. public MyServer(int port) { _port = port; // bind to the port to listen for connections try { _server_socket = new ServerSocket(port); } catch (IOException err) { System.err.println("ERR: Cannot create Server Socket \n" + err); System.exit(1); } this.start(); } // The server just runs forever and listen for new connections // Once a connection is made spawn off to another process public void run() { try { while(true) { SvrConnection scon = new SvrConnection(_server_socket.accept()); } } catch (IOException err) { System.err.println("ERR: Waiting for connection" + err); System.exit(1); } } } // This handles all communication with the connection accepted by the server class SvrConnection extends Thread { protected DataInputStream _cin; // Handles client input protected PrintStream _cout; // Sends Output to the client protected Socket _client; // Holds the clie public SvrConnection(Socket client) { _client = client; System.out.println("Connected to " + client.getInetAddress() + " on Port:" + client.getPort()); try { _cin = new DataInputStream(client.getInputStream()); _cout = new PrintStream(client.getOutputStream()); } catch (IOException err) { System.err.println("Error creating io streams for client\n" + err); // Attempt to close the socket try { System.err.println("Closing socket " + client); client.close(); } catch (IOException err2) { System.err.println("Error closing socket \n" + err2); } return; } // Start Thread using connection this.start(); } // This is where you can customize what your connection does // this just implements a simple echo of input public void run() { String line; try { // Loop until client disconnects while((line = _cin.readLine()) != null) { _cout.println(line); } } catch (IOException err) { System.err.println("ERR: " + err); } finally { try { _client.close(); } catch (IOException err2) { System.err.println("ERR: Closing socket " + _client + "\n" + err2); } } } }