• No results found

4 IMPLEMENTATION

4.2 Basic Concepts

4.2.1 What is Client Server Programming?

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent. Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request [5].

The client running on a computer or mobile device must know the hostname of the computer on which the server is running and the port number on which the server is listening.

The mobile device or computer client attempts to converge with the server computer on the server's computer and port to establish a connection request. The mobile device client also needs to introduce itself to the server computer, so it binds to a local client port number that it will use while this connection which is normally determined by the system.

The server computer acknowledges the connection, if everything goes alright. After acknowledgment, the server computer occupy a new socket bound to the same local port of server computer and also has its remote endpoint set to the address and port of the mobile device client. Now server computer needs another new socket so that server computer can carry on listening to the primary socket for connection requests while serving to the requests of the connected mobile device client.

If the connection is acknowledged on mobile device client side, a socket is successfully created and the mobile device client can use the socket to talk to the server.

The mobile device client and server computer can now talk to each other by writing to or reading from their sockets.

An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host and the server [5].

Here are the basic steps involved in every Client Server communication.

Let’s understand working of Client Server communication by taking Cloud Server example:

1. Open a socket.

2. Open an input stream and output stream to the socket.

3. Read from and write to the stream according to the server's protocol.

4. Close the streams.

5. Close the socket.

4.2.1.1 O

PEN A

S

OCKET

The Cloud Server Application starts by creating a new ServerSocket() object to listen on a particular port. While writing Cloud Server Application, it is required to choose a port that is not already dedicated to some other service. CloudServer listens on port 1234:

try {

cloudserverSocket = new ServerSocket(1234);

}

catch (IOException e) {

System.out.println("Could not listen on port: 1234");

System.exit(-1);

}

The system-independent implementation of the Cloud Server side of the “Cloud based FileSystem Mobile App” is provided by ServerSocket class which is a java.net class.

The ServerSocket constructor throws an exception if it is not able to listen on the specified port which might happen because the port is already being used. In this case, the Cloud Server has no option other than to exit.

Once the Cloud Server successfully binds to its port, then the ServerSocket object is successfully created and the Cloud Server move forward to the next step that is receiving a connection from a mobile device client:

Socket mobiledeviceclientSocket = null;

try {

mobiledeviceclientSocket = serverSocket.accept();

}

catch (IOException e) {

System.out.println("Cannot accept on port: 1234");

System.exit(-1);

}

The accept method waits until a mobile device client begins and send request for a connection on the hostname and port of the cloud server. When a connection is requested and successfully established, the accept method returns a new Socket object which is bound to the same local port and has its remote address and remote port set to that of the mobile device client. The cloud server can communicate with the mobile device client over this new Socket and continue to listen for mobile device client connection requests on the original

ServerSocket [5].

4.2.1.2 O

PEN AN

I

NPUT

S

TREAM AND

O

UTPUT

S

TREAM TO THE

C

LOUD

S

ERVER

S

OCKET

After the Cloud Server successfully establishes a connection with a Mobile Device Client, the Cloud Server communicates with the Mobile Device Client by getting the mobile device client’s socket's input and output stream and by opening readers and writers on them using following code:

PrintWriter serverout = new

PrintWriter(mobiledeviceclientSocket.getOutputStream(), true);

BufferedReader serverin = new BufferedReader(new

InputStreamReader(mobiledeviceclientSocket.getInputStream()));

4.2.1.3 R

EAD FROM AND

W

RITE TO THE

S

TREAM

A

CCORDING TO THE

C

LOUD

S

ERVER

'

S

P

ROTOCOL

String inputServerLine, outputServerLine;

FileServerProtocol fsp = new FileServerProtocol();

outputServerLine = fsp.processServerInput(null);

serverout.println(outputServerLine);

while ((inputServerLine = serverin.readLine()) != null) {

outputServerLine = fsp.processServerInput(inputServerLine);

serverout.println(outputServerLine);

if (outputServerLine.equals("Close Connection.")) break;

}

This code provides following functionalities:

1. Initiates conversation with the mobile device client by reading from the socket.

The above code starts the communication with the mobile device client. It creates a FileServerProtocol object-the object that keeps track of the files, the current state within the files exchange, and so on.

After the FileServerProtocol is created, the code calls FileServerProtocol's

processServerInput method to get the first message that the mobile device client sends to the cloud server. The first thing that the cloud server receives is "File Directory"

Next, the cloud server reading the information from the BufferedReader connected to the client socket, thereby receiving the message from the mobile device client.

2. Cloud Server talks to the mobile device client by reading from and writing to the socket (the while loop).

The code which provides this functionality is encoded in the while loop. As long as the mobile device client and cloud server still have something to say to each other, the cloud server reads from and writes to the socket, sending request/response messages back and forth between the mobile device client and the cloud server.

4.2.1.4 C

LOSE THE

S

TREAMS AND

S

OCKET

These statements close the readers and writers connected to the socket and to the standard input stream, and close the socket connection to the cloud server. The order here is important. You should close any streams connected to a socket before you close the socket itself.

An android application normally consists of number of activities. Every activity provides a user interface that lets the user to do different actions. To transfer the control from one activity to another activity Intents are used. So, every app defines its own “intent” to do

Related documents