Socket dalam Java : Basic

Huaaahhh.. sudah lama tidak nulis lagi di blog ini..
Kebetulan baru sadar juga ternyata belom ada cara coding socket pake Java.. hehehe..

Kalau mengenai pengertian socketnya sendiri mungkin bisa diliat di web lain atau di postingan saya yang Socket dalam C# : Basic. Codingannya pun mirip dengan yang ada di C#. Langsung ke codingannya aja ya.

Untuk kodingan di Servernya

public static void main(String[] args) {

   ServerSocket tServerSocket = null;
   try
   {
      tServerSocket = new ServerSocket(30190);
      while(true)
      {
         Socket tSocket = tServerSocket.accept();
         new Server(tSocket).start();
      }
   }
   catch(Exception ex)
   {
      ex.printStackTrace();
   }
}

Fungsi main tersebut memanggil kelas “Server”. Untuk kodingan di kelas Servernya adalah

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Server extends Thread{

   Socket mClientSocket = null;

   public Server(Socket pSocket)
   {
      mClientSocket = pSocket;
   }

   @Override
   public void run()
   {
      try
      {
         PrintWriter tWriter = new PrintWriter(mClientSocket.getOutputStream(), true);
         BufferedReader tReader = new BufferedReader(new InputStreamReader(mClientSocket.getInputStream()));
         String tMessage = "";

         tWriter.println("OK");
         while(true)
         {
            tMessage = tReader.readLine();
            if(tMessage.equalsIgnoreCase("OK"))
            {
               tWriter.println("OK");
            }
         }
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
   }
}

Sedangkan untuk kodingan di clientnya adalah

public static void main(String[] args) {
   Socket tSocket = null;
   String tIPAddress = "127.0.0.1";

   try
   {
      tSocket = new Socket(InetAddress.getByName(tIPAddress), 30190);
      PrintWriter tWriter = new PrintWriter(tSocket.getOutputStream(), true);
      BufferedReader tReader = new BufferedReader(new InputStreamReader(tSocket.getInputStream()));
      String tMessage = "";

      while(true)
      {
         tMessage = tReader.readLine();
         if(tMessage.equalsIgnoreCase("OK"))
         {
            tWriter.println("OK");
         }
      }
   }
   catch(Exception ex)
   {
      ex.printStackTrace();
   }
}

Kode itu akan membuat client dan server berbalas “OK” tanpa henti untuk memainkan protokolnya tinggal masukkan saja message yang dibutuhkan untuk dikirim dan diterima seperti apa di dalam while(true) baik pada pada server maupun pada client.

Sekian dulu deh..

Selamat mencoba..^_^

Advertisement

2 responses to this post.

  1. Hihi.. jd inget pas kuliah Distributed System, awal disuruh buat kaya gini untuk latihan mempelajari MPI. Terus berkembang pake multi threading untuk melayani banyak client.. :D
    Lumayan bikin bergadang nih… hehe

    Reply

  2. Posted by Aqsath on May 2, 2010 at 9:38 pm

    yups mas irfan.. yang di kode ini juga support multi client kok.. hehehe..

    Reply

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.