Selasa, 05 Maret 2013

Soket



Soket adalah mekanisme komunikasi yang memungkinkan terjadinya pertukaran data antar program atau proses baik dalam satu mesin atau pada mesin lain. Soket dapat menangani banyak client (multiple client). 

Soket dapat melakukan komunikasi antar program/proses melalui jaringan berbasis TCP/IP.
Dalam komunikasi, soket berfungsi sebagai interface pada jaringan yang menjadi titik komunikasi antarmesin pada Internet Protocol, dan tentunya tanpa komunikasi ini, tidak akan ada pertukaran data dan informasi jaringan. Karena itu soket memungkinkan adanya komunikasi client server.

Contoh soket dengan program client-server


Soket server
package jarkom1;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Jarkom1 {

    public static void main(String[] args) {
        // TODO code application logic here
        int port = 1517;
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        BufferedReader readFromClient = null;
        DataOutputStream sendToClient = null;
        String Line = "";
        boolean isOpen = true;

        try {
        serverSocket = new ServerSocket(port);
        System.out.println("srv_msg = server on. . .");

        clientSocket = serverSocket.accept();
        System.out.println("srv_msg = client connected. . .");

        readFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sendToClient = new DataOutputStream(clientSocket.getOutputStream());

        try {
            while(isOpen) {
                Line = readFromClient.readLine();
                System.out.println("srv_msg = from client = " + Line);

                if(Line.contains("hello")) {
                    sendToClient.writeBytes("hello world....\n");}

                else if(Line.contains("close")){
                    isOpen = false;}
                else {
                    sendToClient.writeBytes("srv_msg = unidentified command...\n");}


                }
        sendToClient.writeBytes("srv_msg = bye sir/madam...\n");
        sendToClient.close();
        readFromClient.close();
        clientSocket.close();
        }   catch (IOException ex) {
            System.out.println("srv_msg = error" + ex);}
        }catch (IOException ex){
         System.out.println("srv_msg = error" + ex);}     

        }

        }

Soket Client
package jarkomclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Jarkomclient {

    public static void main(String[] args) {
        // TODO code application logic here
        // TODO code application logic here
        boolean isOpen = true;
        try
        {
           Socket sock = new Socket("localhost",1517);

           PrintStream pr = new PrintStream(sock.getOutputStream());

           while (isOpen) {
           System.out.print("enter something: ");
           InputStreamReader rd = new InputStreamReader(System.in);

           BufferedReader br = new BufferedReader(rd);

           String tmp = br.readLine();
           pr.println(tmp);

           if(tmp.contains("close")) isOpen=false;
           BufferedReader bd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
           String tp = bd.readLine();
           System.out.print(tp);
           }
       }
        catch(Exception ex){}
}
}