Sunday, June 9, 2013

contoh socket connection

Salah satu cara untuk komunikasi client server menggunakan socket connection.
berikut adalah salah satu contoh codingnya:

Coding pada PC Server
*********************
package tugas_server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Tugas_server {

    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(5111);
            Socket s;
            String balasan;
            String balasan2;
            ObjectInputStream in = null;
            ObjectOutputStream out = null;
            s = ss.accept();
            out = new ObjectOutputStream(s.getOutputStream());
            out.flush();
            in = new ObjectInputStream(s.getInputStream());
            balasan = "";
            balasan2 = "";
            while (true) {
                balasan = "";
                balasan2 = "";
                balasan = ReadMessage(in);
                if (balasan.equals("exit")) break;
                else {
                    if(balasan.equals("ini client"))
                    {
                    SendMessage("ini server, masukan username", out);
                    balasan2 = ReadMessage(in);
                    if (balasan2.equals("admin")){ SendMessage("Selamat Datang Admin", out);}
                    else {
                        if (balasan2.equals("user")){SendMessage("Selamat Datang User", out);}
                        else SendMessage("username tidak diketahui", out);
                        }
                    }
                    else
                    {
                        SendMessage("perintah tidak diketahui", out);
                }
            }
            }
    in.close();
    out.close();
    s.close();
    ss.close();
}catch (IOException ex){
    Logger.getLogger(Tugas_server.class.getName()).log(Level.SEVERE,null,ex);
    }
    }
 public static String ReadMessage(ObjectInputStream in)
 {
 String Reader="";
 try {
     Reader=(String)in.readObject();
 }catch (IOException ex){
     Logger.getLogger(Tugas_server.class.getName()).log(Level.SEVERE,null,ex);
 }catch (ClassNotFoundException ex){
     Logger.getLogger(Tugas_server.class.getName()).log(Level.SEVERE,null,ex);
 }
 System.out.println(Reader);
 return Reader;
 }
 public static void SendMessage(String word, ObjectOutputStream out)
 {
     try{
         out.writeObject(word);
         out.flush();
     }catch (IOException ex){
         Logger.getLogger(Tugas_server.class.getName()).log(Level.SEVERE,null,ex);
     }
    }

}


***********************************************************************

Coding pada PC Client.
********************

package tgs_client;
import java.util.Scanner;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author UNLA
 */
public class Tgs_Client {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException{
        Socket s=new Socket("10.0.0.4", 5111);
        ObjectInputStream in=null;
        ObjectOutputStream out=null;
        Scanner scanf=new Scanner(System.in);
        String input="";
        String balasan;
        String balasan2;
        out=new ObjectOutputStream(s.getOutputStream());
        out.flush();
        in=new ObjectInputStream(s.getInputStream());
        balasan = "";
        balasan2="";
        while(true)
        {
            balasan="";
            balasan2="";
            input=scanf.nextLine();
            SendMessage(input, out);
            if(input.equals("exit"))break;
            else
            {
                balasan=ReadMessage(in);
                if(balasan.equals("ini server, masukkan username"))
                {
                    input=scanf.nextLine();
                    SendMessage(input, out);
                    balasan2=ReadMessage(in);

                }
            }
        }
        in.close();
        out.close();
        s.close();

        // TODO code application logic here
    }
public static void SendMessage(String word, ObjectOutputStream out)
{
    try{
        out.writeObject(word);
        out.flush();
    } catch (IOException ex){
        Logger.getLogger(Tgs_Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public static String ReadMessage(ObjectInputStream in)
{
    String reader="";
    try{
        reader=(String) in.readObject();
    } catch (IOException ex){
        Logger.getLogger(Tgs_Client.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex){
        Logger.getLogger(Tgs_Client.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(reader);
    return reader;
}
}


Dalam coding client ganti coding localhost dengan ip komputer Server.
contoh coding client "  Socket s=new Socket("10.0.0.4", 5111);"  dengan mengubah jadi "  Socket s=new Socket("192.168.0.1", 5111);" tergantung ip yang kita setting di komputer sever.

berikut adalah screenshoot ketika program di run:
pada client:


pada server:




No comments:

Post a Comment