Link to home
Start Free TrialLog in
Avatar of nocturn4l
nocturn4l

asked on

Basic programming of a Java Server & Client - Question about the Host Name

I have a programming project to write a pretty basic java server and client to just read and write to each other and was just looking at the tutorial online from java located here:

http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html

It's pretty self explanatory and i think i get everything.. but I do have a question about the host name though which i'll quote from the site here:

"When you start the client program, the server should already be running and listening to the port, waiting for a client to request a connection. So, the first thing the client program does is to open a socket that is connected to the server running on the hostname and port specified:

    kkSocket = new Socket("taranis", 4444);
    out = new PrintWriter(kkSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(
                                kkSocket.getInputStream()));

When creating its socket, KnockKnockClient uses the host name taranis, the name of a hypothetical machine on our network. When you type in and run this program, change the host name to the name of a machine on your network. This is the machine on which you will run the KnockKnockServer. "

How am I supposed to find the "name of a machine on my network"???

some info: I'm doing everything through my universities network (i connect through PuTTy) which I believe uses Linux.. (i can find out for sure if it's important).. and i'm using my home PC which is running windows XP (not sure if that's relevant at all)
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
isn't that name the computer name, which you can check from My Computer --> properties --> computer name?
It can also be replaced by IP address

a faq link
http://www.davidreilly.com/java/java_network_programming/


Avatar of nocturn4l
nocturn4l

ASKER

thanks for the replies, programming now so i'll come back later to give points / say it's not working -.-
okay i'm running the "already written sample" program from that site.. but when i run it.. it's catching the IOException error.

so it says "Couldn't get I/O forthe connection to: taranis"

does this mean it's not working?  I don't understand what's wrong...
/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket("taranis", 7);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("localhost");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: taranis.");
            System.exit(1);
        }

	BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
	String userInput;

	while ((userInput = stdIn.readLine()) != null) {
	    out.println(userInput);
	    System.out.println("echo: " + in.readLine());
	}

	out.close();
	in.close();
	stdIn.close();
	echoSocket.close();
    }
}

Open in new window

echoSocket = new Socket("taranis", 7);

i changed that to

echoSocket = new Socket("localhost", 7);

and echoSocket = new Socket("127.0.0.1", 7);

got the same result
how are you starting the server?
in particular the ServerSocket port needs to match port used by client
Remove your System.out.println statements in the catch blocks and print the stacktrace.
hm okay didn't realize i had to "start" the server.. thought it was jus communicating with the echo port that is already open

i'll read up more and get back to this if i have more problems, thanks objects
java EchoClient

Exception in thread "main" java.lang.NullPointerException
        at EchoClient.main(EchoClient.java:61)
it actually sits there doing nothing then i press enter then i get that ^above message
Okay I tried the server/client example and it worked just fine with "localhost" thanks for the help guys