Skip to content
25 April 2009 / Jeffrey Hermanto Halimsetiawan

Socket Programming using Java


Dua protokol komunikasi utama untuk jaringan berbasis IP dan dapat digunakan dalam kaitannya dengan pemrograman socket :
1. UDP (User Datagram Protocol)
Setiap kali datagram akan dikirim, alama socket penerima harus ditetapkan setiap saat.
2. TCP (Transmission Control Protocol)
Sebelum memulai pertukaran data, sepasang socket dibuat sebagai sambungan. Satu socket sebagai server socket yang bertugas mendengarkan sambungan sedangkan yang lain meminta server socket untuk sambungan. Setelah terbentuk sambungan tersebut, socket baru dapat digunakan untuk mengirimkan data.

Berikut ini adalah contoh sederhana Socket Programming dengan Bahasa Java dengan protocol TCP :
Server.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Jeffrey
 */
public class Server {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        Socket client;
        try {
            server = new ServerSocket(1234);
        } catch (IOException ie) {
            System.out.println("Cannot open socket.");
            System.exit(1);
        }
        String temp;
        while(true) {
            try {
                client = server.accept();
                OutputStream clientOut = client.getOutputStream();
                PrintWriter pw = new PrintWriter(clientOut, true);
                InputStream clientIn = client.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
                temp = br.readLine();
                System.out.println(temp);
                pw.println(temp);
                br.close();
                pw.close();
                clientIn.close();
                clientOut.close();
            } catch (IOException ie) {
                ie.printStackTrace();
            }
        }
    }
}

Client.java



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**

 *

 * @author Jeffrey

 */

public class Client {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) throws UnknownHostException, IOException {

        String temp = "";

        do {

            try {

                Socket client = new Socket(InetAddress.getLocalHost(),1234);

                InputStream clientIn = client.getInputStream();

                OutputStream clientOut = client.getOutputStream();

                PrintWriter pw = new PrintWriter(clientOut, true);

                BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));

                BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

                System.out.println("Type a message for the server: ");

                temp = stdIn.readLine();

                pw.println(temp);

                System.out.println("Server message: ");

                System.out.println(br.readLine());

                pw.close();

                br.close();

                client.close();

            } catch (ConnectException ce) {

                System.out.println("Cannot connect to the server.");

            } catch (IOException ie) {

                System.out.println("I/O Error.");

            }

        }

        while (!temp.matches("EXIT"));

    }

}
  1. huda / Mei 4 2009 21:32

    jef gak ono sing iso kanggo nggawe post karo get

    • Jeffrey Hermanto / Mei 5 2009 18:26

      gk enek..
      aq nduwe seng contoh-e webserver..
      shiftku mailserver nek post karo get yo gk ruh..
      hahaha..

  2. oculy / Sep 7 2009 11:34

    mantap ..thanks ya

    • Jeffrey Hermanto / Sep 7 2009 11:42

      o y sama2..
      semoga bermanfaatšŸ˜€

  3. bonek / Jan 12 2012 14:51

    suwun masbro

    • Jeffrey Hermanto Halimsetiawan / Apr 23 2012 14:37

      sama2, semoga bermanfaatšŸ™‚

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

%d blogger menyukai ini: