Java: TCP Server-TCP Client

Program java TCP Echo Server ; TCP Echo Client

======================================

Script TCP Server disimpan dengan file TCPEchoServer.java

import java.io.*;
import java.net.*;
import java.util.*;
public class TCPEchoServer
{
private static ServerSocket servSock;
private static final int PORT = 1234;    //Port yang digunakan

/* main method */
public static void main(String[] args)
{
System.out.println(“Membuka port…”);
try
{
System.out.print(“akan membuat objek servSock”);
servSock = new ServerSocket(PORT);        //Step 1.
System.out.println(“……..[OK]”);
}
catch(IOException ioEx)
{
System.out.println(“……..[Fail]”);
System.out.println(“Gagal membuka port!”);
System.exit(1);
}
do
{
handleClient();
} while (true);
}

/* methode menangani clien */
private static void handleClient()
{
Socket link = null;                            //Step 2.
try
{
System.out.println(“Menunggu koneksi dari client…\n”);
link = servSock.accept();                    //Step 2.
System.out.println(“>Ada client terhubung…\n”);
Scanner input = new Scanner(link.getInputStream());//Step 3.
PrintWriter output = new PrintWriter(
link.getOutputStream(),true); //Step 3.
int numMessages = 0;
String message = input.nextLine();                //Step 4.
while (!message.equals(“*TUTUP*”))
{
System.out.println(“>>Menerima pesan.”);
numMessages++;
output.println(“Pesan ” + numMessages
+ “: ” + message);             //Step 4.
message = input.nextLine();
System.out.println(“>>>Pesan telah dikirim kembali ke client.”);
}
output.println(numMessages+ ” pesan telah diterima.”);//Step 4.
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println(“\n* Menutup koneksi… *”);
link.close();                                //Step 5.
}
catch(IOException ioEx)
{
System.out.println(“Gagal menutup koneksi”);
System.exit(1);
}
}
}
}

==========================================

Script TCP Client disimpan dengan nama TCPEchoClient.java

import java.io.*;
import java.net.*;
import java.util.*;
public class TCPEchoClient
{
private static InetAddress host;
private static final int PORT = 1234;
public static void main(String[] args)
{
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println(“Host ID not found!”);
System.exit(1);
}
accessServer();
}

/* method akses server */
private static void accessServer()
{
Socket link = null;                        //Step 1.
try
{
link = new Socket(host,PORT);        //Step 1.

Scanner input =    new Scanner(link.getInputStream()); //Step 2.
PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 2.

//Set up stream for keyboard entry…
Scanner userEntry = new Scanner(System.in);
String message, response;
do
{
System.out.print(“\nMasukkan pesan: “);
message = userEntry.nextLine();
output.println(message);                //Step 3.
response = input.nextLine();            //Step 3.
System.out.println(“SERVER> “+response);
}while (!message.equals(“*TUTUP*”));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println(“\n* Menutup koneksi… *”);
link.close();                        //Step 4.
}
catch(IOException ioEx)
{
System.out.println(“Gagal tutup koneksi!”);
System.exit(1);
}
}
}
}

===============================================

Lalu bagaimana menjalanknnya????

1. Buka command prompt jalankan perintah “javac TCPEchoServer.java (enter)” setelah tidak ada yang error ketikkan lagi “javac TCPEchoServer.java (enter)” pembuatan class server dan client berhasil

2. cara mencompilenya “java TCPEchoServer” buka commandprompt baru ketikkan “java TCPEchoClient

3. amati hasilnya

Selamat mecoba🙂🙂

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s