KirkPatrick 28 Junior Poster

I have a Tcp Client (in Java) and a Tcp Server (in Groovy) and I am having problems getting the Client to communicate properly. I believe the issues involve my client not being able to send information to the server. (My assumption is the problem is somewhere near line 32 in my client)

My server works fine when accessed by telnet, just not with my java client.

Client:

public class Client {

    Socket requestSocket;
    String message;
    BufferedReader in;
    BufferedWriter out;

    Client() {


    }

    void connect(String host) {

        String command = "ip";

        try {
            requestSocket = new Socket(host, 2000);
            System.out.println("Connected to localhost in port 2000");

            in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
            out = new BufferedWriter(new OutputStreamWriter(requestSocket.getOutputStream()));

            while(true) {
                message = in.readLine();
                System.out.println("server>" + message);
               
                if(command != null) {
                    sendMessage(command);
                    command = null;
                }

            }
        }
        catch(UnknownHostException unknownHost) {
            System.err.println("You are trying to connect to an unknown host!");
        }
        catch(IOException ioException) {
            ioException.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
                requestSocket.close();
            }
            catch(IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    void sendMessage(String msg) {
        try {
            out.write(msg);
            out.flush();
            System.out.println("client>" + msg);
        }
        catch(IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public static void main(String args[]) {

        Client client = new Client();
        client.connect("localhost");


    }

}

Server:

//variables
def serialNo
def ipAddress
def machineName
def operatingSystem

server = new ServerSocket(2000)

println("Waiting for connection");

while(true) {
	server.accept() { socket ->
		socket.withStreams { input, output ->
				
			w = new BufferedWriter(new OutputStreamWriter(output))
			String message = "Connection was successful"

			r = new BufferedReader(new InputStreamReader(input))
			

			while(true) {
							
				if(message != null) {
				w.writeLine(message)
				w.flush()
				}

				String a = r.readLine()

				if(a=="dir") {
					
					serialNo = "cmd /c dir".execute().text
					message = serialNo
					w.writeLine(message)
					w.flush()
					message = null;
					
				} else if(a=="os") {
					getOperatingSystem();
				} else if(a=="machine") {
					getMachineName();
				} else if(a=="ip") {
					
					//get ip address and hostname without the getter
					def e = InetAddress.getLocalHost().getHostAddress()
					def f = InetAddress.getLocalHost().hostName
				
					message = e
					w.writeLine("Ip Address>" + message)
					w.flush()
					message = f
					w.writeLine("Host Name>" + message)
					w.flush()
					message = null;					
				} else if(a=="exit") {
					message = "Goodbye."
					sendMessage(message)
					message = null;
					w.close()
					r.close()
				} else {
					//if command is unknown let the user know
					message = "$a command unknown."
					sendMessage(message)
					println message
					message = null;
				}

			}

		} 
	} 
} 

def getOperatingSystem() {
	println "operating system works"
}

def getMachineName() {
	println "machine name...works"
}

In the output of my client I get:

Connected to localhost in port 2000
server> Connection was successful
client>os

In the output of my server I get:

Waiting for connection

I think that it attempts to send the message over to the server but the message is never received.

I appreciate any posts that can point me in the correct direction