Dungoo0
Dungoo0

Reputation: 3

My server stops working when reading .accept(), using Sockets

please excuse my writing errors... I'm using NetBeans to run a homemade server and a client, and it all works fine. As I said before, I'm using "Socket" on my client, and "ServerSocket" on my sv. I'm also using JDBC Mysql in the server. The problem starts when I generate both java files in their distributable folders and use them. The client sends information to the server (it starts with .getOutputStream() and .getInputStream(), then .flush() ), but the server doesn't receive any message. I tried seeing where it stops and it's in

clients[i] = new Client (server.accept(), i);

The crazy thing happens when I try executing my server from NetBeans and the client from my desktop... It works! So the server must be the problem. I'm also using an opened UDP port, and i'm looking for the IP of the server on 192.168.0.10 (which is my computer, in LAN).

I hope someone can help me, thanks in advance!


Here I paste my code, i'm sorry some variables are in spanish:

public ServidorMultiCliente() { super("Servidor MultiCliente"); initComponents();

    try{
        servidor = new ServerSocket( 500, maxNumberClients);
    }catch(IOException excepcion){
        excepcion.printStackTrace();
        System.exit(1);
    }

    serverWriteBox.append("Server iniciated, waiting for connections..."); }

I run these, from the Server:

public void ejecutar(){ clientsAcceptor.start(); messagesListener.start(); }

Where clientsAcceptor is:

private class aceptadorClientes extends Thread{

    public void run(){
        for( int i = 1; i < maxNumberClients; i++ ){
        try{
            clientes[i] = new Cliente (servidor.accept(), i); // **Here it stops**
            // It never reaches here... (without using NetBeans)
            clientes[i].start();
            clientes[i].aceptado = true;
        }catch(IOException excepcion){
            excepcion.printStackTrace();
        }
    }

That's how I accept clients in different threads. I make the same thing with messageListener, which is a new thread for every new client. It's in a loop, always listening. And here I paste my executable Client, which is different from the Cliente class I was using in ServidorMultiCliente:

public Cliente(){ }

public Cliente(String host){
    this.host = host;
    this.executeConnection();
}

public void executeConnection(){
    int connect = 0;
    try {
        cliente = new Socket(InetAddress.getByName(host), 500);
        conectar = 1;
    } catch (IOException ex) {
        conectar = 0;
        this.ejecutarConexion();
    }

    if(conectar == 1){
        obtainFlows();
    }
}

private void obtainFlows(){

    try{
        output= new ObjectOutputStream( cliente.getOutputStream());
        output.flush(); // Here I should be "connected"

        input = new ObjectInputStream(cliente.getInputStream());
    } catch(IOException ex){
        this.initiateDisconnection();
    }
    sendFlows("I'm connected!");
    new messageListener().start(); // This is a thread
}

Upvotes: 0

Views: 1081

Answers (1)

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11607

ServerSocket#accept is a blocking call. It listens to a port and returns a Socket when a client connects. You don't show very much of your server logic but it seems you put clients in an array so you obviously want to support more than one client. You don't show if your Client class starts a thread and returns immediatly.

You should have a server loop that just listens to a server socket and creates clients after it retrieved a client socket. Even if you do this in your Client constructor (I can't tell without the code) it is not a very good place for this and seriously hinders debugging.

If you don't start threads for your clients this would explain a server that "stops" (if "stops" means "blocks" and not "crashes"). See "Writing the Server Side of a Socket" in the Java Tutorial for a detailed explanation.

I can't think of why it behaves different when started via Netbeans. A little bit more of code context is needed.

Upvotes: 1

Related Questions