Uncategorized

Tugas Rancang 1 – Sistem Terdistribusi (Subject: Socket – TCP)


Berikut ini adalah contoh source code untuk simple chat dengan menggunakan teknologi socket – TCP pada Java

Server.java

import java.io.DataInputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author yere
 */
public class Server {
    private static int PORT=8080;
    private static DataInputStream input;
    private static PrintStream output;
    private static Socket clientSocket;
    private static Scanner sc=null;

    public static void main(String[] args) {

        try {

            ServerSocket server=new ServerSocket (PORT);
            System.out.println("Start...");
            clientSocket = server.accept();
            System.out.println("Try to connect...");
            input = new DataInputStream(clientSocket.getInputStream());
            output=new PrintStream(clientSocket.getOutputStream());
            sc=new Scanner(System.in);

            String inputKar ="";
            while(!inputKar.equals("exit")){
                inputKar = input.readLine();
                System.out.println("Client :"+inputKar);

                System.out.println("Server : ");
                inputKar=sc.next();
                output.println(inputKar);
            }
            server.close();
            clientSocket.close();
            input.close();
        } catch (Exception e) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE,null,e);
        }
    }
}

Client.java

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author yere
 */
public class Client {
 public static void main(String[] args) {
 Socket clientSocket=null;
 DataInputStream input;
 PrintStream output;
 Scanner sc=null;

 try{
 clientSocket = new Socket("localhost",8080);
 output=new PrintStream(clientSocket.getOutputStream());
 input=new DataInputStream(clientSocket.getInputStream());
 sc = new Scanner(System.in);
 String kar="";
 while(!kar.equals("exit")){
 System.out.println("Client : ");
 kar=sc.next();
 output.println(kar);

 kar= input.readLine();
 System.out.println("Server : "+kar);
 }
 output.close();
 clientSocket.close();
 }catch(IOException e){

 }
 }
}

TUGAS RANCANG 1 – Socket TCP

Tugasnya adalah membuat program chatting sederhana, seperti Yahoo Messenger! Tampilannya adalah seperti berikut:

Syarat dan Ketentuan Berlaku:

  • Program di atas dikerjakan kelompok maksimal 2 mahasiswa
  • Program di atas dikerjakan tanpa adanya tindak kriminal antar mahasiswa (Ex. File Sharing)
  • Responsi akan diadakan saat praktikum minggu depan (24 – 29 Januari 2011)
  • Program di atas WAJIB diimplementasikan dalam LEBIH DARI 1 PC/LAPTOP yang saling terhubung
  • Tidak Mengerjakan atau Tidak responsi atau alasan lain… NILAI 0 (NOL) dan Tidak Ada Protes dikemudian hari
  • Jika ada pertanyaan silakan hubungi Asisten

Terima kasih atas kerjasamanya

Happy Coding

Salam Sayang,

-y2-

Advertisements

1 thought on “Tugas Rancang 1 – Sistem Terdistribusi (Subject: Socket – TCP)”

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 )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s