Thursday, January 20, 2011

Introduction: Simple, yet improved JAVA Server Client Application

Now hold on a damn minute, what's server-client? Is this about some business contract? Well kind of, only this time we take it to a virtual context. We do business in programming language... Cool huh!

What is server - client model?

Ok now let's get to "BUSINESS". Three words! SERVER, CLIENT, PROTOCOLS. So that's all? Well almost, Server is the entity which renders some service, client simply send request to the server. Client communicate with the server according to some protocol agreed by both server and the client. Basically that's all! Simple isn't it?



Ok now you're really making my head spin around.....
Let's do it in an example. Lets say we (client) want server (locally or on the internet) to say "I'm fine" when we say "How are you?". So we and server agree on "If client says "How are you?", server says "I'm fine". " This is the protocol.

What are we going to do?


Well, we're going to create a client program which sends requests and also the server program which responds accordingly.

I thought of dividing the procedure of a server and client to 3 stages.

Server
1. Initiate Server Sockets, connections, object streams
2. Listen on the socket initiated until the server receives a request from the client and respond accordingly.
3. Close object streams and socket.

Client
1. Initiate sockets, connections, object streams (socket has the same port number as the server socket)
2. Send request to the server
3. Close object streams and socket.

What...? More Basics...!

What is a socket?
Socket is simply the end points of the server and client which communicates with each other. A simple analogy 2 humans talking. Think about that!
In Java, there are server sockets (server) and sockets (client).

What is a object stream?
Well it's a stream which allows flowing of objects. You send an object from a particular socket through the stream it end up at the other end.
In Java, guess what? We have ObjectInputStream and ObjectOutputStream.

That's basics you are gonna require. So let's build it

1 comment:

  1. Gane...finish this tutorial...I'm waiting for the rest of this tutorial..=))

    ReplyDelete