user3008528
user3008528

Reputation: 1

Client-Server application doesn't work properly

First af all, sorry for my english, it's a bit rusty.

My aim is to read and write in a text file on a remote computer. First of all, I developed a simple client-server program just to connect two computers. But I'm having trouble. If I try to connect to the server with the Client app (the one I've developed), the connection is refused. But it works properly if I do it with Telnet from Windows and Ubuntu command prompt. The client app throws the following exception (it's like when there isn't a server listening...):

java.net.ConnectException: Connection refused

The other point is that it has to be able to establish the connection in WAN network (that's my final objective), not just in LAN (what I said, here works fine with Telnet).

I leave here the code. Thanks so much for your attention and patience.

PD: the code is in Spanish but I think it's easy to understand.

Servidor.java

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class Servidor {
    //Constantes
    final int MAX_CLIENTES = 5;
    final int PUERTO = 2000;
    /************/

    //Métodos - constructor
    public Servidor(){
        ServerSocket skServidor;
        try {
            skServidor = new ServerSocket(PUERTO);
            System.out.println("Escucho el puerto: "+PUERTO);
            for(int numCli = 0; numCli < MAX_CLIENTES; numCli++){
                Socket skCliente = skServidor.accept();
                System.out.println("\tSirvo al cliente: "+skCliente.getRemoteSocketAddress());
                OutputStream aux = skCliente.getOutputStream();
                DataOutputStream flujo = new DataOutputStream(aux);
                flujo.writeUTF("\nHola cliente "+ skCliente.getRemoteSocketAddress());
                skCliente.close();
            }

        } catch(UnknownHostException e){
            System.out.println( e );
            System.out.println("Error en la conexión." );

        }
            catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e);
        }
    }

    public static void main(String[] args){
        new Servidor();
    }
}

Cliente.java

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class Cliente {
    //Constantes
    final int PUERTO = 2000;
    //final String HOST = "192.168.1.36";

    /************/

    //Métodos - constructor
    public Cliente(){

        try{
            InetAddress address = InetAddress.getLocalHost();
            Socket skCliente;
            System.out.println("Soy el cliente "+address);
            skCliente = new Socket(address, PUERTO);
            InputStream aux = skCliente.getInputStream();
            DataInputStream flujo = new DataInputStream(aux);
            System.out.println(flujo.readUTF());
            skCliente.close();

        } catch(UnknownHostException e){
            System.out.println( e );
            System.out.println("Error en la conexión." );

        } catch (Exception e) {
            System.out.println(e);

        }
    }

    //Cuerpo del programa
    public static void main(String[] args){
        new Cliente();


    }

}

Upvotes: 0

Views: 157

Answers (1)

user2591957
user2591957

Reputation:

I just compiled your code and it ran fine. This is what I got for output:

Server Soy el cliente NameLaptop/MyIPAddress

Hola clente /MyIPAddress:52023

And for Client

Escucho el puerto: 2000 Sirvo al cliente: /MyIPAddress:52023

I don't think there is a problem with your code..This might be a problem with the permissions set on your PC

Upvotes: 1

Related Questions