1 / 21

Pemrograman Jaringan Pemrograman Socket

Pemrograman Jaringan Pemrograman Socket. Kelas A & B Jonh Fredrik Ulysses jonh.fredrik.u@gmail.com. Paradigma client/server. Paradigma client/server adalah dasar untuk framework jaringan Java. Tentunya, penetapan ini terdiri dari dua elemen besar, yaitu client dan server.

Download Presentation

Pemrograman Jaringan Pemrograman Socket

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PemrogramanJaringanPemrogramanSocket Kelas A & B Jonh Fredrik Ulysses jonh.fredrik.u@gmail.com STMIK PALANGKARAYA - JFU - 2012

  2. Paradigma client/server • Paradigma client/server adalah dasar untuk framework jaringan Java. Tentunya, penetapan ini terdiri dari dua elemen besar, yaitu client dan server. • Client adalah mesin yang membutuhkan beberapa jenis informasi sedangkan server adalah mesin yang menyimpan informasi dan menunggu untuk menyampaikannya pada client. STMIK PALANGKARAYA - JFU - 2012

  3. Paradigma client/server • Paradigmainimenjelaskansebuahskenariosederhana. Tentunya, client terhubungdengan server danmemintainformasi. Kemudian server mengingatpermintaandanmengembalikaninformasi yang tersediakepada client. STMIK PALANGKARAYA - JFU - 2012

  4. Sockets • Konsep umum jaringan pada Java networking adalah dengan memperhatikan sockets. Kebanyakan pemrograman Java network menggunakan jenis khusus dari komunikasi jaringan yang diketahui sebagai sockets. STMIK PALANGKARAYA - JFU - 2012

  5. Socket • Socket adalah software abstrakuntuk media input atau output komunikasi. Socket digunakanoleh Java untukmengatasikomunikasipadajaringan level rendah. • Jalurkomunikasiinimemungkinkanuntukmentransfer data melalui port khusus. • Singkatnya, socket adalah point terakhiruntukkomunikasiantaraduamesin. STMIK PALANGKARAYA - JFU - 2012

  6. Operasi Socket • Socket dapat melakukan operasi: • Koneksi ke mesin remote • Mengirim data • Menerima data • Mentutup koneksi • Bind to a port • Listen pada data yang masuk • Menerima koneksi dari mesin remote pada port tertentu • Di tiap mesin yang saling berinterkoneksi, harus terpasang socket. STMIK PALANGKARAYA - JFU - 2012

  7. Socket API in Java • Pada J2SE telah disediakan paket java.net yang berisi kelas- kelas dan interface yang menyediakan API (Application Programming Interface): • level rendah (Socket, ServerSocket, DatagramSocket) • level tinggi (URL, URLConnection). • Disimpan pada package java.net.* STMIK PALANGKARAYA - JFU - 2012

  8. STMIK PALANGKARAYA - JFU - 2012

  9. InetAddress class • Kelas ini digunakan untuk mengambil informasi IP suatu komputer. Kelas ini bersifat static dan tidak memiliki konstruktor. Method-methodnya adalah: • getByName(namahost) yang akan menerima sebuah string nama host dan mengembalikan alamat IP berdasarkan DNS, berupa object InetAddress. • Untuk menampilkannya: gunakan method toString() • getLocalHost() yang akan mengembalikan alamat IP dan nama host pada komputer lokal. • getAllByName(namahost) mengembalikan array InetAddress • Kemungkinan error: UnknownHostException STMIK PALANGKARAYA - JFU - 2012

  10. Contoh getByName import java.net.*; import java.util.*; /** * * @author Jonh Fredrik U */ public class IPFinder { public static void main(String[] args) { String host; Scanner input = new Scanner(System.in); System.out.print("\n\nMasukannama host: "); host = input.next(); try { InetAddress address = InetAddress.getByName(host); System.out.println("IP address : " + address.toString()); } catch(UnknownHostExceptionuhEx) { System.out.println("Could not find " + host); } } } STMIK PALANGKARAYA - JFU - 2012

  11. Contoh getLocalHost import java.net.*; /** * * @author Jonh Fredrik U */ public class MyLocalIPAddress { public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); } catch(UnknownHostException uhEx) { System.out.println("Tidak menemukan local address"); } } } STMIK PALANGKARAYA - JFU - 2012

  12. Contoh getAllByName import java.net.*; /** * * @author Jonh Fredrik U */ public class Jaringan { public static void main(String[] args) { try { InetAddress address = InetAddress.getByName("www.google.com"); System.out.println(address); InetAddress[] addresses = InetAddress.getAllByName("localhost"); for(int i=0;i<addresses.length;i++) System.out.println(addresses[i]); InetAddress mesin = InetAddress.getLocalHost(); System.out.println(mesin); String lokal = mesin.getHostName(); String ip = mesin.getHostAddress(); System.out.println(lokal); System.out.println(ip); } catch(UnknownHostException uhEx) { } } } STMIK PALANGKARAYA - JFU - 2012

  13. Connection-oriented & Connectionless Socket • A socket programming construct can make use of either the UDP (User Datagram Protocol) or TCP (Transmission Control Protocol). • Sockets that use UDP for transport are known as datagram sockets, while sockets that use TCP are termed stream sockets/TCP sockets. STMIK PALANGKARAYA - JFU - 2012

  14. IP Protocol Suite STMIK PALANGKARAYA - JFU - 2012

  15. Socket to Socket Communication STMIK PALANGKARAYA - JFU - 2012

  16. Review: TCP vs. UDP • TCP • Connection-oriented • Reliable • Stateful • UDP • Connectionless • Unreliable • Stateless STMIK PALANGKARAYA - JFU - 2012

  17. Review: Ports • Used to differentiate applications running on same host (address) • Represented as 16-bit integer • Well-known ports: 0 – 1023 • Registered: 1024 – 49151 • Dynamic Ports: 49152 - 65535 STMIK PALANGKARAYA - JFU - 2012

  18. Java Socket & ServerSocket class • Untuk Socket Connection Oriented Programming • This means that the connection between server and client remains open throughout the duration of the dialogue between the two and is only broken (under normal circumstances) when one end of the dialogue formally terminates the exchanges (via an agreed protocol) • Kelas java.net.ServerSocket digunakan oleh Server untuk listen koneksi • Kelas java.net.Socket digunakan oleh Client untuk inisialisasi koneksi. • Setelah client terkoneksi ke server dengan menggunakan Socket, maka ServerSocket akan mengembalikan status server ke client melalui koneksiyang terbentuk sebelumnya. STMIK PALANGKARAYA - JFU - 2012

  19. Prinsip ServerSocket • Create a ServerSocket object. • ServerSocket servSock = new ServerSocket(1234); • Put the server into a waiting state. • Socket link = servSock.accept(); • Set up input and output streams. • Scanner input = new Scanner(link.getInputStream()); • PrintWriter output = new PrintWriter(link.getOutputStream(),true); • Send and receive data. output.println("Awaiting data..."); String input = input.nextLine(); • Close the connection (after completion of the dialogue). • link.close(); STMIK PALANGKARAYA - JFU - 2012

  20. Prinsip Socket (client) • Establish a connection to the server. • the server's IP address (of type InetAddress); • the appropriate port number for the service. Socket link = new Socket(InetAddress.getLocalHost(),1234); • Set up input and output streams. • Scanner input = new Scanner(link.getInputStream()); • PrintWriter output = new PrintWriter(link.getOutputStream(),true); • Send and receive data. • The Scanner object at the client will receive messages sent by the PrintWriter object at the server, • while the PrintWriter object at the client will send messages that are received by the Scanner object at the server (using methods nextLine and println respectively). • Close the connection. STMIK PALANGKARAYA - JFU - 2012

  21. Simple Port Scanner import java.io.IOException; import java.net.InetAddress; import java.net.Socket; /** * * @author Jonh Fredrik U */ public class PortScanner { public static void main(String[] args) throws Exception { String host = "localhost"; InetAddressinetAddress = InetAddress.getByName(host); String hostName = inetAddress.getHostName(); //for (int port = 0; port <= 65535; port++) for (int port = 0; port <= 80; port++) { try { Socket socket = new Socket(hostName, port); String text = hostName + " is listening on port " + port; System.out.println(text); socket.close(); } catch (IOException e) { String s = hostName + " is not listening on port " + port; System.out.println(s); } } } } STMIK PALANGKARAYA - JFU - 2012

More Related