Catatan tugas socket java

# buat pause sebentar pakai sleep
try {
 	Thread.sleep(5000); //dalam milisec
}
catch(InterruptedException ie) {
	System.out.println("error msg");
}


# pake thread
while(something){
// Main server adalah kelas yg implement runnable
// kelas yang implement runnable punya fungsi void run() yg akan dijalankan selalu sampai threadnya selesai dipakai, stop.
	MainServer v = new MainServer(c); 
//inisiasi thread t untuk kelas implement runnable v, lalu start
	Thread t = new Thread(v);
	t.start();
}


# socket (server)
//sama kaya client


# socket (client)
import java.io.*;
import java.net.*;

    private static final int port = 9697;
    private static final String host = "localhost";

        Socket c = new Socket(host,port);
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
        BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String m = null;
        m = r.readLine(); //<---------------baca dari server awal
        while (!m.equalsIgnoreCase("exit")) {
            m = in.readLine();
            w.write(m);
            w.newLine();
            w.flush();// <-----------send ke server
            m = r.readLine();//<---------------baca dari server
        }
        w.close();
        r.close();
        c.close();


#tools membuat setter getter di netbeans
navigation > pilih satu atribut > refractor > encapsulate field > senang-senang :))

#check if there's any obj in list
obj x = new obj();
list.indexOf(x) <--- return int posisi list

#kalo mau komunikasi kelas  (writeObject) lewat socket
kelasnya harus implements Serializable.. gak usah implement fungsi override apapun

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s