Tuesday, 20 December 2011

Java Network Client: Part 1

Ok so here we go; I've now started work on creating the foundation to the Java program!

To be fair, I've probably spent around 2 hours on it, and within those hours not all that much has been achieved. Bits and pieces needed to be re-learned from earlier this year, as well as getting around a few slight issues.

I've decided to break the project down a little bit; first it will be a chat messenger, then I'll build on its features from there. I decided that if i went right into the program sending pictures etc, i would get bogged down in getting that part right than working on actually getting the networking part right! I will implement the actual file transferring stuff as soon as the chat part is done.

What i should probably do is tell you whats happened:

- Created two programs: the Client and the Server.
These do exactly what they say on the tin; the Client is what every user will be using, and the Server sorts where everything is going as well as keeping track on whats connected to it. I will go into both of these in more detail in a bit.

- Threaded both the Server and the Client (Will explain this further down)

- Working on getting the Server to understand who's who
Basically, when a client logs in it passes its IP address to the server to store. This can then be used by the Server to pass back text. This leads onto making the project a chat messenger.

The Server:
As i said above, the Server is the central part of the network. It will contain all the details of everyone connected, thus allowing for people to interact with each other. Let me explain a little further:
Person A wants to send messages to person B (lets assume person B is already logged in). Person A will connect to the Server (which will store person As IP address) and will then be told who else is connected to the network. Person A will then pick person B to talk to, and thus will send a message to them. The message will travel to the Server, which will then check to see if person B is still there and then pass the message on. This will then appear on person Bs screen.
Phew, sorry for such a "proper" explanation there, but it also helps me to work out whats going on.

Some of the coders among you will have been wondering how I'm going to deal with all this simultaneously, as in the Server dealing with the Clients sending and getting messages at the same time.
Simply put, im going to "thread" it all.
Threading is a rather clever idea. I'll explain it in context with the program.(If you already know about threading, skip this!)

So, the Sever will need to be able to deal with multiple connections at once, as clients are connecting and disconnecting with the Server all the time. The best way to do this is when the Client connects, it creates a "thread" which is self contained.
You could say that a program without threading is a single thread. Trying to make the program with connections coming in at any time and along with other things going on makes it almost impossible without threading (although it is achievable).
A loose analogy is that you're make a cup of tea. Asked to make more than one cup of tea at the same time will require more thinking, and more time. It will also depend on whether you have already boiled the water and what cups you have available. Now with threading when someone asks you for a cup of tea, you create a new instance of you making said tea; a new mug, kettle and tea bag are all created ready for you and thus the second tea making can be done at the same time as the first cup of tea.
Of course this is a bit of a loose analogy, so don't take it as 100%.

So back to what i was saying. The Server is threaded to allow for multiple connections, and the client is partially threaded.

What i mean by the client is partially threaded is because at the moment only the reciever part is threaded, not the sender. So you can receive messages at any time, but you only send one message at a time, thus at the moment the sender part doesnt need to be.

Anyway, sorry for diving into threading. This was supposed to be a more general update on whats happening, but got carried away!

Hope to get the Chat part finished soon, and in the next few days i will explain how I store the addresses of everyone connected.
Until then, have fun!

Hazz 22

(Small side note: The name is just for fun. I just thought it would need a name!)

No comments:

Post a Comment