1

The code below should allow the user to enter a URL and have it return the ip address of that website but it's not working.

The application is a console application. I had it working at one time but I don't know why it won't work now.

Here is the error i am getting when the users enters a website to get the ip address from

IOException:  java.net.SocketException: Connection reset

HERE IS MY CLIENT CODE

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    public static void main(String[] args) {

        String hostname = "localhost";
        int port = 6052;

        if (args.length > 0) {
            hostname = args[0];
        }

        Socket clientSocket = null;
        PrintWriter os = null;
        BufferedReader is = null;


        try {
            clientSocket = new Socket(hostname, port);
            os = new PrintWriter(clientSocket.getOutputStream(), true);
            is = new BufferedReader(new    InputStreamReader(clientSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + hostname);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + hostname);
        }


        if (clientSocket == null || os == null || is == null) {
            System.err.println("Something is  really wrong. ");
            return;
        }

        try {
            if (args.length != 2) {

                System.out.print("Enter a www web address (must have www!) ");
                BufferedReader br = new BufferedReader(new InputSreamReader(Sy.in))
                String keyboardInput = br.readLine();
                os.println(keyboardInput);

            } else {
                os.println(args[1]);
            }
            String responseLine = is.readLine();
            System.out.println("The IP address of " + args[1] + "is" + responseLine);


        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

HERE IS MY SERVER CODE

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String args[]) {
        int port = 6052;
        Server server = new Server(port);
        server.startServer();
    }

    ServerSocket echoServer = null;
    Socket clientSocket = null;
    int numConnections = 0;
    int port;

    public Server(int port) {
        this.port = port;
    }

    public void stopServer() {
        System.out.println("Server working hold on a min.");
        System.exit(0);
    }

    public void startServer() {
        try {
            echoServer = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Server is now started and is waiting for Clients.");


        while (true) {
            try {
                clientSocket = echoServer.accept();
                numConnections++;
                new Thread(new ServerConnection(clientSocket, numConnections,
                        this)).start();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
}




 class ServerConnection implements Runnable {
 private static BufferedReader is;
 private static PrintStream os;
 private static Socket clientSocket;
 private static int id;
 private static Server server;

 public ServerConnection(Socket clientSocket, int id, Server server) {
this.clientSocket = clientSocket;
this.id = id;
this.server = server;
System.out.println( "Connection " + id + " established with: " + clientSocket );
try {
    is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    os = new PrintStream(clientSocket.getOutputStream());
} catch (IOException e) {
    System.out.println(e);
}
}




public void run() {
    String line;
try {
    boolean serverStop = false;

            line = is.readLine();
    System.out.println( "Received " + line + " from Connection " + id + "." );

                 InetAddress hostAddress = InetAddress.getByName(line);
                 String IPaddress = hostAddress.getHostAddress();
                 os.println(IPaddress);

          is.close();
        os.close();
     clientSocket.close();
} catch (IOException e) {
    e.printStackTrace();
}
}
}
22
  • 1
    "it's not working" please provide some specific information, like the errors you are getting or anything that can help answer your question Commented Mar 22, 2013 at 16:51
  • 1
    ...also, I do not mind editing, but a little spell checking and proper code formatting would help Commented Mar 22, 2013 at 16:52
  • i added what error im getting Commented Mar 22, 2013 at 16:57
  • i get this error when the user enters a website to get the ip address from. then i get this error IOException: java.net.SocketException: Connection reset Commented Mar 22, 2013 at 16:58
  • 1
    Man, I don't wanna be repetitive, but what are the args you put when you get the error? Write the command you call Client, for example $ java Client this_is_argument_1 this_is_argument_2. Commented Mar 22, 2013 at 17:28

1 Answer 1

1

With no arguments, host will be localhost, user will be propted for a website. ArrayOutOfBoundsException because you didn't check the arguments.

With one argument, it is the host. Passing a site will not work because the site won't work as expected.

Running with two arguments, it works if the first argument is localhost.

Sign up to request clarification or add additional context in comments.

4 Comments

Check the arguments only in beginning of the program. Make the first argument be the hostname and the second the website. If one or both are not present, handle it properly.
ok so i ran my code in eclipse this is every thing that happen , first "Server is now started and is waiting for Clients." sencond Enter a www web address (must have www!) then users enters www.google.com then i get this error IOException: java.net.SocketException: Connection reset
Run it from terminal. One terminal for each class (Server then Client).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.