0

I'm teaching myself Java and I'm using the Deitel book as it came highly recommended and I've hit a bit of a snafu.

So I tried copying over the figure 27.5-8 in the book Java: How to Program. I figured I would need the .5 figure as it's the server and the .7 figure because it's the client. So I made them both in the same project and then combined their main classes (figures .6 and .8) so they when I ran the program it would boot up both the server and the client. But when I tell netBeans to compile and run it opens the windows I have set up for the server and the client but the textFields won't enable (as they're supposed to when they receive a connection.) and as far as I can tell they aren't connecting to each other.

The server.java and client.java files should be exactly the same as they were in the book, so I figure I must have messed up when I blended the main files to boot them both up. Here is my combined main file. Maybe I did something wrong here?

package server_client;
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        Server application = new Server(); //create server
        Client applicationClient; //declare client application

        //if no command line args
        if (args.length==0)
            applicationClient = new Client ("127.0.0.1"); //connect to localhost
        else
            applicationClient = new Client (args[0]); //use args to connect

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        applicationClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.runServer(); //run server application
        applicationClient.runClient(); //run client application
    }//end main
}//end class Main
Rob
  • 9
  • 1

2 Answers2

0

You are mixing things up. Let's start from the beginning. Firstly, this is how you create a simple UI.

public static void main(String[] args){
    JFrame frame = new JFrame(); // This will be holding your future buttons

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Request Generator");
    frame.setSize(300, 300); // Size x = 300, size y = 300
    frame.setLocationRelativeTo(null); // Puts the frame in the middle of the screen

    frame.setVisible(true); // Without this line of code, the frame won't show
}

But I wouldn't recommend creating a UI for test purposes in this case, as it is very time consuming and unnecessary. Use the console instead. You can output anything in the console like this:

System.output.println("Hello, world!");

Or even a variable, like a number.

int number = 10;
System.output.println("Variable number has value: " + number);

Second, I recommend you split up your Client and Server into two separate projects, and starting them separately. Or even better, I can give you a small example of a Client/Server connection if you'd want to. Because, personally, I have never encountered this implementation before.

D. Visser
  • 875
  • 2
  • 12
  • 19
0

can be tricky to solve.

There is a utility with windows called netstat which will display all your network connections.

Also learning how to use the debugger will help.

One possibility is that the connection is being established, prior to the GUI checking for it so the GUI doesn't know that the connection is there.

Try starting the server in one application and the client in a different one.

BevynQ
  • 8,089
  • 4
  • 25
  • 37