Friday, 30 December 2011

Java Network Client update 31/12/11

So Christmas has been and gone. Got some nice jumpers and spent some time with the family, so all in all good times. But this blog is not about my personal life (not entirely anyway) so back to the Java Network Client.

I'm going to be frank (you can be who ever you want), development has ground to a halt.
This isn't because I cant be bothered, but actually due to the way I'm trying to implement it. As you know the whole program is based around being able to send files and messages around a home network with a Central computer redirecting everything.So computer A sends a message to computer B by first telling the Central computer that it wants to send to B and with the central computer then passing the information on, like a relay.

The concept seems pretty simple, but once you start threading it all gets complicated. Let me explain.

When A connects to the central computer, the central computer stores its IP address (when A initially connects it sends its IP across the network automatically). This now means a separate thread for A has now been created. When B connects the same thing happens. The major issue I found though was where can i store the IP addresses and make the accessible to all the threads? 



Now this is where the blog post gets a little bit confusing, but hang on in there. Initially i was writing the above post, then i suddenly came up with a way of fixing the problem, so i focused on that for an hour and forgot about this post. So here i am a day later to complete it and share what happened:

The solution didn't work.

I'll get back to what i was going to say...

So, the solution to the IP problem has been elusive to say the least. Actually that's not true; a working solution is a far better way to describe what's going on. When I first encounter the whole storing problem, I thought I could just store the IP in an array within an Object (coding term; look it up), but this wont work, as each thread would create a brand new Object to store the array, and thus NONE of the connected computers would have a complete array.

Ok, I understand that for people who don't do this stuff is all a bit "wha?", and i will write about Objects soon. But this has led to a seriously hard problem. How do I create a central store for all the IP data where each thread can access and update?
The idea I had was to create a separate file that stores the Object which contains the IP addresses. This file can then be accessed by any thread! Of course there are a few problems with using this methods which i will elaborate on in a later blog post, but the idea to me seems pretty sound. Well it would be if I could get it working!

For some bazaar reason when I read and write to files from the program it just fails! So with out a decent way to store the IPs I can't get more than interaction between the Central computer and a computer connected. Very annoying!

So at the moment I'm at a loss. Over the coming weeks i will keep working at it, and find away around it. When I'm back at University I'll ask around to see what I'm doing wrong or for other ideas.
In the mean time keep a lookout on my twitter to see updates, and if you want to help then leave a comment!

Hazz 22

Merry Christmas and a Happy New Year!      

  

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!)

Thursday, 15 December 2011

Christmas Project Part Two


So, assuming you read my most recent blog that i posted all of about 5 minutes ago, you are probably aware of the fact I'm going to be blogging over Christmas (If your not... go and read the blog below this!).  But blogging about what? Ah... now your asking the right questions.

I have probably 4 projects i wish to work on and complete over the next two to three weeks. For starters, a file transfer Java program.

As a Software engineering student, i get given course work that asks me to make simple programs. One such assignment is to simulate a Client-Server relationship on a local network. For example, the Client asks for a picture from the Server and the Server sends it. This got me thinking; could i extend this to something more useful?

So, here's my plan: create a Java program that will allow me to transfer files around all three of my computers that are connected to the local network.
Why you ask? Well, simply put its a pain to transfer files around at the moment. Each computer is connected to DropBox, so i can sync files that way, but my Internet in my house is pretty poor. It cuts out randomly, thus causing me to get out a USB pendrive and faff about with it. I just want someone i can load up from the desktop and send files around.
There is another step to this: User interface. I don't really want to be running it from the command line every time i want to use it, so I'm going to build a small Swing (Javas interfacing stuff) front to it. Some people don't like Swing, which is fair. I've never actually used it, so i thought i might as well give it a go!

This project is all about learning. 

You learn nothing from a map, but loads from getting lost

So yeah, thats the plan. I will post updates along with pictures and designs of progress etc.

Leave a comment if your interested!

Hazz 22

(I am aware there are FAR more efficient ways of doing this, but its all about learning)



Christmas Projects 2011

Hello all!

Right, well...er... this is awkward! Basically, yesterday i created a Tumblr account and today i posted too it. It's a great site, its just a massive shame it doesn't have ANY native stat tracking! As in, you can't tell how many people have visited etc.

So basically, I've ended up back here. I did have a brief stint using Wordpress, but got sick of how needlessly complicated it all was. I just wanted a blogging site that was easy to update, good looking and easy to track what views i was getting. So here i am once again, back on Blogger.

So what am i doing back here you ask? Well, as i wrote this morning on Tumblr, I'm here as its now the Christmas holidays. This means I FINALLY have some time off! No more feeling guilty about doing something fun for half an hour, when I should have been working.
So with all this extra time I have, I've decided to set my self some projects. I'm not going to explain them all now, as that will be another post, but I will say that I plan to "document" (blog) the work I do.

So anyway, its nice to be back!

Hazz 22