Networking
I wrote the framework to handle the networking action here. (SAVE IT TO YOUR FOLDER THAT YOU ARE WORKING WITH). In BLUEJ - click open project.
This is not a JApplet but a similar class to JFrame called WatcherFrame so extend that (ie)):
public class NetworkApp extends WatcherFrame
You need to have 2 methods, init which you always have and
public void receiveUpdate(Object b) {
Receive update will get called when you have received an update [note it receives an object, even if it might be a Dog, or whatever-> so you will need to cast it; ie (Dog d = (Dog)b;]
To send updates, anywhere in your code, you will say
connection.sendObject(whatever); //where whatever is the object you want to send
It should be noted, whatever the object is the you send, it must implement Serializable, ie:
public class Dog implements Serializable
Lastly, you will need to start a server and client version of the code which you will do with threads, see below:
public static void main(String[] args)
{
//for standalone app keep the code below and comment the 2 threads
// NetworkApp n= new NetworkApp();
// n.init();
// n.setSize(500,500);
// n.setVisible(true);
ServerBlueJ s=new ServerBlueJ(3); //3 is the number of connections you would like to allow new ClientBlueJ(new NetworkApp(),500,500,false); new ClientBlueJ(new NetworkApp(),500,500,false); new ClientBlueJ(new NetworkApp(),500,500,false);
}
Here is a complete project that uses network to send message back and forth. |