Tugas pak achsan 4 – program untuk mengirim obyek lewat soket


program kali ini adalah program untuk mengirim obyek lewat socket menggunakan java. program ini saya ambil dari forum java internasional yang sourcenya akan saya cantumkan di bawah artikel ini.

pertama akan saya cantumkan code pada server.

public class Server extends Thread{

        ServerSocket ss=null;
	Socket socket=null;
	String line="";
	Measurements mes = new Measurements();

public void run(){
	try{

		ss = new ServerSocket(1905); 
		socket = ss.accept(); 

		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		ObjectOutputStream out = new ObjectOutputStream((socket.getOutputStream()));

		double x[] = new double[6];
		double u[] = new double[6];
		double v[] = new double[6];

		line = in.readLine();

		while ((!line.equals("STOP")) && (line.equals("Get Measurements"))){			

			for (int i=0;i<6;i++){

				synchronized (Simulator.lakes[i]){
				v[i]=Simulator.lakes[i].getInFlow();
				u[i]=Simulator.lakes[i].getOutFlow();
				x[i]=Simulator.lakes[i].getLevel();}
			}

			mes=new Measurements();
			mes.setX(x);
			mes.setU(u);
			mes.setV(v);

			Calendar calendar = Calendar.getInstance();
			java.util.Date now = calendar.getTime();
			java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
			mes.setDate(currentTimestamp);

			System.out.println("In server:  x[1]="+ mes.getX()[1]+"  u[1]="+mes.getU()[1]+"  v[1]="+mes.getV()[1]+"date= "+mes.getDate());

			out.writeObject(mes);

			sleep(1000);
			out.flush();
			line = in.readLine();
		}

	}catch(Exception e){e.printStackTrace();}
}
}

Selanjutnya adalah coding pada client

public Measurements getMeasurements(){

		out.println("Get Measurements");

		try {

			m=(Measurements) in.readObject();

			System.out.println("In client:  x[1]="+ m.getX()[1]+"  u[1]="+m.getU()[1]+"  v[1]="+m.getV()[1]+" date= "+m.getDate());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return m;
	}

Jika program ini berjalan, maka akan dihasilkan output sebagai berikut:
In server: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:57.681
In server: x[1]=32.5530 u[1]=0.6430 v[1]=0.9706 date= 2010-11-15 08:26:58.768
In server: x[1]=46.0680 u[1]=0.3521 v[1]=0.7266 date= 2010-11-15 08:26:59.774
In server: x[1]=49.1065 u[1]=0.1337 v[1]=0.3319 date= 2010-11-15 08:27:00.775
...

In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:57.681
In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:58.768
In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:59.774
In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:27:00.775
...


Thanks to alexandra who made this program :)
http://www.javaprogrammingforums.com/whats-wrong-my-code/6026-sending-object-through-socket.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s