Baiklah pertama-tama akan saya jelaskan bagaimana menginstall jdk.
- Pastikan komputer Anda terhubung dengan koneksi internet
- Buka terminal, ketikkan perintah sudo su lalu masukkan password Anda
- Ketikkan perintah apt-get install default-jdk, lalu enter maka akan ad proses yang berlangsung
- Ketik Y untuk melanjutkan lalu enter, maka kita akan memulai download jdk
- Tunggu hingga proses download selesai, maka kita sudah dapat mengcompile java
- Buka teks editor, copy paste source code berikut ini, lalu simpan dengan nama socket_server.java
- Buka terminal lalu ganti direktori ke tempat Anda menyimpan source code di atas, lalu ketikkan perintah javac socket_server.java untuk mengcompile
- Lalu ketikkan perintah java socket_server untuk meng-run hasil compile tadi
- Untuk mengetes program ini, bukalah terminal baru lalu ketikkan perintah telnet localhost 5000
- Jika Anda medapatkan tampilan seperti diatas berarti Anda sudah berhasil, langkah berikutnya adalah Anda tinggal mencoba mengetikkan pesan lalu akan segera dibalas oleh server dengan pesan yang sama
- Yap, Anda telah berhasil membuat client-server menggunakan Java. Dan ini adalah tampilan apabila Anda menutup terminal client
//java server example
import java.io.*;
import java.net.*;
public class socket_server
{
public static void main(String args[])
{
ServerSocket s = null;
Socket conn = null;
PrintStream out = null;
BufferedReader in = null;
String message = null;
try
{
//1. creating a server socket - 1st parameter is port number and 2nd is the backlog
s = new ServerSocket(5000 , 10);
//2. Wait for an incoming connection
echo("Server socket created.Waiting for connection...");
//get the connection socket
conn = s.accept();
//print the hostname and port number of the connection
echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());
//3. get Input and Output streams
out = new PrintStream(conn.getOutputStream());
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
out.println("Welcome. Server version 1.0");
out.flush();
//4. The two parts communicate via the input and output streams
do
{
//read input from client
message = (String)in.readLine();
echo("client>" + message);
if(message != null)
{
out.println(message);
}
else
{
echo("Client has disconnected");
break;
}
}
while(!message.equals("bye"));
}
catch(IOException e)
{
System.err.println("IOException");
}
//5. close the connections and stream
try
{
in.close();
out.close();
s.close();
}
catch(IOException ioException)
{
System.err.println("Unable to close. IOexception");
}
}
public static void echo(String msg)
{
System.out.println(msg);
}
}
//sumber: http://www.binarytides.com/java-socket-programming-tutorial/
- Buka teks editor, copy paste source code berikut ini, lalu simpan dengan nama socket_server2.java
- Buka terminal lalu ganti direktori ke tempat Anda menyimpan source code di atas, lalu ketikkan perintah javac socket_server2.java untuk mengcompile
- Lalu ketikkan perintah java socket_server2 untuk meng-run hasil compile tadi
- Untuk mengetesnya, bukalah 3 terminal baru lalu ketikkan perintah telnet localhost 5000 pada masing-masing terminal, maka Anda seharusnya mendapatkan tampilan seperti ini
- Jika sudah mendapatkan tampilan seperti itu, sekarang Anda hanya perlu memasukkan pesan yang ingin dikirim maka server akan membalas dengan pesan yang sama.
//java server example
import java.io.*;
import java.net.*;
public class socket_server2
{
public static void main(String args[])
{
ServerSocket s = null;
Socket conn = null;
try
{
//1. creating a server socket - 1st parameter is port number and 2nd is the backlog
s = new ServerSocket(5000 , 10);
//2. Wait for an incoming connection
echo("Server socket created.Waiting for connection...");
while(true)
{
//get the connection socket
conn = s.accept();
//print the hostname and port number of the connection
echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());
//create new thread to handle client
new client_handler(conn).start();
}
}
catch(IOException e)
{
System.err.println("IOException");
}
//5. close the connections and stream
try
{
s.close();
}
catch(IOException ioException)
{
System.err.println("Unable to close. IOexception");
}
}
public static void echo(String msg)
{
System.out.println(msg);
}
}
class client_handler extends Thread
{
private Socket conn;
client_handler(Socket conn)
{
this.conn = conn;
}
public void run()
{
String line , input = "";
try
{
//get socket writing and reading streams
DataInputStream in = new DataInputStream(conn.getInputStream());
PrintStream out = new PrintStream(conn.getOutputStream());
//Send welcome message to client
out.println("Welcome to the Server");
//Now start reading input from client
while((line = in.readLine()) != null && !line.equals("."))
{
//reply with the same message, adding some text
out.println("I got : " + line);
}
//client disconnected, so close socket
conn.close();
}
catch (IOException e)
{
System.out.println("IOException on socket : " + e);
e.printStackTrace();
}
}
}
//sumber: http://www.binarytides.com/java-socket-programming-tutorial/
check this one too...http://net-informations.com/java/net/socket.htm
ReplyDeleteLing