Introduction to networking with JAVA

Hi so we are now beginning to focus on networking in JAVA for the Cardiff uni course that I’m doing. I’ve really been struggling at the moment to focus. I’ve been learning node.js and also trying to work on JAVA aswell. It’s really difficult to have direction when being self taught and it’s very difficult for me as I get so excited about the possibilities of programming that I tend to get really carried away.

So focus is really important. I feel like I know a lot about the theory but I am not actually creating enough things. So I have been working on a basic student record application which presents the user with options to add,update,display and delete student records, which is a lot of work. The brief is also quite unclear and is open ended, as in the real world I suppose. I will do another blog post on this at another time when the application is complete. I’ve sorted the basic GUI and now working on adding records to a RAF (random access file) and displaying the records when the user wishes to do so.

This blog post is focusing on networking. I am new to networking so I will just present a basic application that basically establishes a client-server connection and how to go about this.

The first thing we need to do is import the:

import java.net.*;
package. The diagram below from here demonstrates the classes that can be used when we import this package, which is quite useful:

JN3_1601

The server typically provides services such as database access and this can be fulfilled when requested by the client. The most common point to point client-server connection is done through TCP (transmission control protocol). TCP guarantees packet delivery and if packets are lost or damaged, TCP will resend the packages until it verifies packets have been successfully transmitted. TCP is extremley reliable in this sense. The command ping in linux enable us to see further information about packages, for example if any were lost during a request however this will be another post for another time!

In a client-server architecture, the client and server both bind to a socket at the end of the connection. Each connection is unique by its combination of ports and endpoints (IP addresses).

The client must know the correct port and ip address or host name of the server to initiate a connection request to the server. When the server accepts the connection, a connection is established.

In the server application, once a ServerSocket has been instantiated with designated port, a new socket is create to accept the connection request, by calling the accept() method on the ServerSocket object.

This image is a very simple and useful visual image of what the client-server architecture looks like:

NetworkingJava001

As we can see, one server can have many clients.

The basic server-client steps are outlined below:

Important things to note is that server and client port numbers MUST match and the server must be run before the client. Now onto the code examples:

server.java

 
import java.io.IOException;
import java.net.*;

public class server {
    public static void main(String[] args){

        /* creates server socket in order to listen to client requests */
        ServerSocket s = null;
        /* error handling must be in place */
        try {
            s = new ServerSocket(3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Server waiting for connection....");
        try{
            Socket c = s.accept();
            /* this statement prints when connection to client has been made */
            System.out.println("Connection established!");
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}

client.java

import java.io.IOException;
import java.net.*;

public class client {
    public static void main(String[] args){
    	/* like with the server file, error handling must be in place */
        try (Socket s = new Socket("localhost",3000)) {
        	/* connection established printed both on server and client terminal */
            System.out.println("Connection established!");
        }

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

To run this we first run the server application and presented with this:

server

Then we run the client in a new terminal window (with the server one still open) and as can be seen as soon as we run the client program, the connection is established!

servrandclient

I hope this post was useful and I welcome your feedback 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s