Chat Tutorial in NetBeans/Completion

From Arianne
Jump to navigation Jump to search

Making the Chat Client Network-ready

The Chat tutorial has been shown to work on a single machine. There are a few things that need to be done to make it work on a network; either a local network or the internet.

Specifying the server

The client currently connects to "localhost". This has been used so far because it represents the machine that the client is running on. To use this on more than one machine the connection must be made to the machine that is running the Chat server.

The server can be refered to by a four part number (an IP address such as 192.168.1.100) or by a name. The details will depend on how your network has been set up; the usual statement "if in doubt, consult the network administrator" applies here. One way to find the IP address is to open a terminal / command window and type the command "ipconfig" on windows, or "ifconfig" on linux or on a Mac.

The network may also be configured to change IP addresses in certain circumstances. Usually the network name will stay the same, so that might be the one to be used. If you have found the IP address, open a terminal / command window and type the command "ping (IP address)", i.e. "ping 192.168.1.100" if that is the IP address.

The name may also be a location on the internet.

A final solution might be to add the server as a field on the GUI client form so that it can be entered by the user. This might make it more difficult for the user, but would also allow the user to connect to several Chat servers.

Specifying the server port

A port on the server is a channel (like your TV) on which communications can be sent or received. Port 5555 is assumed to be free for the Chat application, but it may be used by another application or even blocked altogether. Again, "if in doubt, consult the network administrator". A different port number can be used if that is not in use, but choose wisely.

Giving the client a fixed server address

Edit the View class to add the server name or IP address. Find the line

    client.connect("localhost", 5555);

Change the word localhost to the server IP address, network name or internet address. Change the "5555" (port number) to the one used by the Chat server.

Once these changes are made, run the Build option for this project. Deploy the client files as before.

Putting the client on a local network

Having built the client project these files can be deployed to another machine on an internal network. See the deployment instructions from before for which files to copy, paste them in a directory on the client machine. Use those instructions to run the client; Java will need to be installed to run the client on that machine.

Setting up a web server

This is not covered in detail here, but you will need a web server to deploy the Chat application to a network if you are not going to install the client on each machine. The files will need to be copied to the directory that is mapped to the webserver address.

Using Java Web Start

This is the Java method for deploying applications. Open the properties page for the client project in NetBeans. Select the Web Start option from the list. Tick the "Enable Web Start" box to allow editing of the fields on that page (it may be hiding below the Application option, so expand that if required). In the Codebase field, select User defined. In the codebase Preview field, enter the web address of the directory into which you will deploy the client files (not the file directory on the server). Save the properties and Build the project.

NetBeans creates the jar files, and two additional files. File launch.html is the file that will be opened by the users browser. If the contents are viewed it is clearly a very simple file. It holds two methods of starting the client; one is through downloading some code from Java.com and using that code to create a launch button. The second is a simple html link that will show clickable text that launches the client. The second file, the one that is the target of both launch methods above, is launch.jnlp that contains the required settings to allow the browser to find the client code.

Copy these two files and client.jar to the web server directory.

Testing Java Web Start

Make sure that your web server is running on the server machine. Start the browser on the client machine and enter the web address of the launch.html file. It should open and show the launch button, or the launch link if you changed that file. Click on the button or link to run the client; it may show a few confirmation windows before running the client, depending on how the browser security is configured.

A small window with the Java logo will open, then launch the GUI client screen as expected. Enter the user information that was used previously (make sure the Chat server application is running!) and the server should respond as it did before. Get someone else to repeat this on another machine, and have a chat with them.

Have a little celebration if this works. This tutorial is done; congratulations!