Cliente de chat Java que se conecta al servidor

I have created a chat server that works with telnet. Now, I am trying to write my own client. I need to be able to get an IP address from the user and a port number. I am trying to pass those variables through the ChatClient(). However, when I compile the following code, I receive the following error message:

ChatClient.java:24: cannot find symbol
symbol  : variable ip
location: class ChatClient
            new ChatClient(ip,port);
                           ^
ChatClient.java:24: cannot find symbol
symbol  : variable port
location: class ChatClient
            new ChatClient(ip,port);
                          ^
2 errors

ChatClient.java

public class ChatClient {
    PrintWriter output;
    BufferedReader input;
    Socket client;

    public ChatClient(int ip, int port) throws Exception {
        String line;
        input = new BufferedReader( new InputStreamReader( client.getInputStream()) ) ;
        output = new PrintWriter(client.getOutputStream(),true);
        output.println("Enter an ip address: ");
        line = input.readLine();
        output.println("Enter a port number: ");
        line = input.readLine();
    }

    public static void main(String ... args) {
        try {
            new ChatClient(ip,port);
        } catch(Exception ex) {
            out.println( "Error --> " + ex.getMessage());
        }

    } // end of main        

}

preguntado el 31 de enero de 12 a las 16:01

do a clean and recompile -

There are no ip or port variables in the scope of main method. -

2 Respuestas

Antes de hacer new ChatClient(ip,port) necesitas declarar int ip y int port variables in your code:

public static void main(String... args) {
    try {
        int ip = 0;
        int port = 8080;
        new ChatClient(ip,port);
    } catch(Exception ex) {
        ...
    }
}

BTW, if you are going to read IP address and port from console you can remove int ip, int port parámetros de ChatClientconstructor de.

Respondido el 31 de enero de 12 a las 20:01

Now it compiles, but it does not print out to the console. Would it be better to get the ip address and port number from the server side and then pass it to the client side? - user1179027

It's a different question, but take a look at this short tutorial: java-tips.org/java-se-tips/java.util/… - Yatskevich

yatskevich provided the answer to your compilation error, but your code has other problems.

What do you want the ChatClient constructor to do with the ip and port? It currently sends a prompt over a Socket's OutputStream and then waits for input from the Socket over its InputStream. It then ignores the input.

public ChatClient() throws Exception {
    String line;
    int ip, port;
    input = new BufferedReader( new InputStreamReader( client.getInputStream()) ) ;
    output = new PrintWriter(client.getOutputStream(),true);

    output.println("Enter an ip address: ");
    line = input.readLine();
    if ( line == null ) {
        //EOF - connection closed
        throw new EOFException( "EOF encountered before ip address input." );
    }
    try {
        ip = Integer.parseInteger( line );
    } catch (NumberFormatException nan) {
        //Invalid input
        //    log the error and throw the exception or use a default value.
    }

    output.println("Enter a port number: ");
    line = input.readLine();
    if ( line == null ) {
        //EOF - connection closed
        throw new EOFException( "EOF encountered before port input." );
    }
    try {
        port = Integer.parseInteger( line );
    } catch (NumberFormatException nan) {
        //Invalid input
        //    log the error and throw the exception or use a default value.
    }
}

Now you have read the port and ip address from the Socket.

Where did the Socket come from? I suspect that what you really want is to:

  1. Get the ip address and port from the command-line arguments.
  2. Open a Socket to the specified address and port.
  3. Create a Client instance with the newly created Socket instance.

Respondido el 31 de enero de 12 a las 22:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.