Anda di halaman 1dari 7

MENGGUNAKAN TCP SOCKET

Sekilas Mengenai TCP Socket 1. Hubungan komunikasi yang dibuat dibuat melalui TCP socket merupakan hubungan connection-oriented. 2. Hubungan antara server dan tetap terbuka selama dialog. 3. Dalamkeadaannormal.hanya dapat diputus aketika salah satu end-point (client atau server) menghentikan exchange melalui protokol yang disetujui

Contoh Program Server


import java.io.*; import java.net.*; import java.util.*; } catch(IOException ioEx){ public class TCPEchoServer{ System.out.println("Unable to attach private static ServerSocket servSocket; private static final int PORT = 2480; } public static void main(String args[]){ System.out.println("Opening port..."); System.out.println("Awaiting for data..."); }while(true); do{ handleClient(); to port!"); System.exit(1); // STEP1: Create ServerSocket object servSocket = new ServerSocket(PORT);

try{ }

private static void handleClient(){ Socket link = null; // STEP 2 }

output.println(numMessages + " messages received."); // STEP 4

catch(IOException ioEx){ try{ link = servSocket.accept(); // STEP 2 } finally{ try{ Scanner input = new Scanner(link.getInputStream()); PrintWriter output = new PrintWriter(link.getOutputStream(), true); // STEP 3 // STEP 3 System.out.println("\n* Closing connection... *"); link.close(); // STEP 5 ioEx.printStackTrace();

System.out.println("Waiting new connection..."); } catch(IOException ioEx){ System.out.println("Unable to disconnect"); System.exit(1); } } } }

int numMessages = 0; String message = ""; do{ message = input.nextLine(); // STEP 4

if(!message.equals("QUIT")){ System.out.println("Message received -> " + message); numMessages++; output.println("Message " + numMessages + ": " + message);

Contoh Program Client import java.io.*;

} import java.net.*; }while(!message.equals("QUIT")); import java.util.*;

try{ public class TCPEchoClient{ private static InetAddress host; private static final int PORT = 2480; Scanner input = new Scanner(link.getInputStream()); public static void main(String args[]){ try{ host = InetAddress.getLocalHost(); // ganti argumen pada method (getByName()) tersebut dengan nama host dmn program server diletakkan //host = InetAddress.getByName("ghanoz2480"); } catch(UnknownHostException uhEx){ System.out.println("Host ID not found!"); System.out.print("Client> Enter System.exit(1); } message: "); message = userEntry.nextLine(); output.println(message); accessServer(); } if(!message.equals("QUIT")){ private static void accessServer(){ // STEP 3 Socket link = null; // STEP 1 response); } System.out.println("\nServer> " + response = input.nextLine(); // STEP 3 do{ String message, response; // Set up stream for keyboard entry... Scanner userEntry = new Scanner(System.in); PrintWriter output = new PrintWriter(link.getOutputStream(), true); STEP 2 // // STEP 2 link = new Socket(host, PORT); // STEP 1

}while(!message.equals("QUIT")); // STEP 4 } response = input.nextLine(); // STEP 3 System.out.println(response); }

link.close();

catch(IOException ioEx){ System.out.println("Unable to disconnect!"); System.exit(1);

catch(IOException ioEx){ } ioEx.printStackTrace(); } } } finally{ } try{ System.out.println("\n* Closing connection... *");

Demo Sebelum mencoba program antar komputer, kita coba dulu secara localhost. Buka dua command satu untuk menjalankan program server satu lagi untuk menjalankan program client. Jalankan program server (TCPEchoServer) terlebih (dahulu karena jika tidak saat program client (TCPEchoClient) dijalankan akan mengalami crash Setelah dijalankan server akan menunggu koneksi dari akan client.

Jalankan program client. Client siap untuk mengirim pesan.

Mengirim pesan (Hallo) ke server

Server menampilkan pesan ( Hallo )yang dikirim oleh client.

Client memutuskan koneksi dari server.

Server menunggu koneksi baru dari client.

Sumber : http://ghanoz2480.wordpress.com