Question

my client/server works perfectly for one message, then no matter what's next it says it's blank.

I believe the problem resolves in here or my commands class:

package MyServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class Main {
public static String line;
public static void main(String[] args){
    while(true){
        try {              
            //Creates a socket to receive commands from!
            Socket socket  = new Socket("localhost", 7586);

            //Uses that socket to create a Reader to read the commands!
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));    

            //Waits for Lines to be sent and then executes them!
            while(true){
                line = in.readLine();

                if(line != null){
                   Commands.ReceiveCommand();
                }else {
                    break;
                }
            }        
        } catch (UnknownHostException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }

}


}    

or in my commands:

package MyServer;

import javax.swing.JOptionPane;

public class Commands {

static String command = (Main.line).toString(); //<--This was the problem, just had to move it into the method below.
public static void ReceiveCommand(){
    if(command.equals("test")){
        JOptionPane.showMessageDialog(null,"works","command: " + command,JOptionPane.WARNING_MESSAGE);
        //System.out.println("WORKEDS MOFO");
        command = "";
    }else{
        JOptionPane.showMessageDialog(null,"not recognized","command: " + command,JOptionPane.WARNING_MESSAGE);
        //System.out.println("no bueno");
        //System.out.println("line is " + command);
        command = "";

    }

}

}

Edit: For some reason when debugging, command is just blank no matter what after it's been used once, so it might be in my main server class:

package MyClient;

import java.util.List;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Main {
//Sets the Port
final static int PORT = 7591;

//Creates a list of Connected Clients
static List<Socket> connectedClients = new ArrayList<Socket>();

public static void main(String[] args){
    //Creates a Thread to Send Messages to connectedClients
    new Thread(new messageThread()).start();

    try {
        //Creates the ServerSocket
        ServerSocket serverSocket = new ServerSocket(PORT);

        while(true){
            //Waits for a Connection and Accepts it...
            Socket clientSocket = serverSocket.accept();
            System.out.println("A Client has connected!");
            //Adds it to the List
            connectedClients.add(clientSocket);
        }



    } catch (IOException e) {
        e.printStackTrace();
    }
} }

and the messageThread:

package MyClient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class messageThread implements Runnable {    
public void run() {
    while(true){
        System.out.println(">>");
        Scanner in = new Scanner(System.in);
        String command = in.nextLine();

        for(Socket clientToSendCommand : Main.connectedClients){
            try {
                PrintWriter commandWriter = new PrintWriter(clientToSendCommand.getOutputStream());
                commandWriter.println(command);
                commandWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
}
Was it helpful?

Solution

This dosen't work, because th line

static String command = (Main.line).toString();

in the Commands-class is executed exactly once, when the Commands-class is first referenced.

When the second command is send, the class was already referenced, so this line is not executed again.

To solve this put the line inside the method, or - much better - pass it as a parameter to the method.


P.S.: Have you mixed up the packages? The class with the ServerSocket should be the server and thus be in the MyServer package. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top