Pada postingan kali ini saya akan membagikan program java Server client multi thread. Pada server dapat terkoneksi beberapa client sekaligus dalam 1 waktu.
Source Codenya di bawah ini :
Alur Data |
Server.java
package server;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server { private static ServerSocket serverSocket; private static final int PORT = 1234;
public static void main(String[] args) throws IOException { try { serverSocket = new ServerSocket(PORT);
System.out.println("Server berjalan, menunggu client...");
} catch (IOException ioEx) { System.out.println("\nTidak dapat mensetup port!");
System.exit(1);
} do {
//Menunggu koneksi dari client...
Socket client = serverSocket.accept();
System.out.println("\nClient baru diterima.\n");
//Buat thread untuk menangani komunikasi dengan client ini
//lewatkan socket yang relevan ke contructor dari thread ini
ClientHandler handler = new ClientHandler(client);
handler.start();
//menjalankan thread yang telah dibuat } while (true); }
} class ClientHandler extends Thread { private Socket client;
private Scanner input;
private PrintWriter output;
public ClientHandler(Socket socket) {
//Set up referensi ke socket yang beraosiasi...
client = socket;
try { input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
} catch (IOException ioEx) { //ioEx.printStackTrace(); }
} public void run() { String received;
do { //Terima pesan dari client pada input stream socket... received = input.nextLine(); System.out.println(received);
//Echo-kan pesan kembali ke client pada stream output socket...
output.println("[" + received + "]");
//Ulangi sampai client mengirimkan pesan 'QUIT'... } while (!received.equals("QUIT")); try { if (client != null) { System.out.println("Menutup koneksi...");
client.close();
}
} catch (IOException ioEx) { System.out.println("Penutupan koneksi gagal!");
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server { private static ServerSocket serverSocket; private static final int PORT = 1234;
public static void main(String[] args) throws IOException { try { serverSocket = new ServerSocket(PORT);
System.out.println("Server berjalan, menunggu client...");
} catch (IOException ioEx) { System.out.println("\nTidak dapat mensetup port!");
System.exit(1);
} do {
//Menunggu koneksi dari client...
Socket client = serverSocket.accept();
System.out.println("\nClient baru diterima.\n");
//Buat thread untuk menangani komunikasi dengan client ini
//lewatkan socket yang relevan ke contructor dari thread ini
ClientHandler handler = new ClientHandler(client);
handler.start();
//menjalankan thread yang telah dibuat } while (true); }
} class ClientHandler extends Thread { private Socket client;
private Scanner input;
private PrintWriter output;
public ClientHandler(Socket socket) {
//Set up referensi ke socket yang beraosiasi...
client = socket;
try { input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
} catch (IOException ioEx) { //ioEx.printStackTrace(); }
} public void run() { String received;
do { //Terima pesan dari client pada input stream socket... received = input.nextLine(); System.out.println(received);
//Echo-kan pesan kembali ke client pada stream output socket...
output.println("[" + received + "]");
//Ulangi sampai client mengirimkan pesan 'QUIT'... } while (!received.equals("QUIT")); try { if (client != null) { System.out.println("Menutup koneksi...");
client.close();
}
} catch (IOException ioEx) { System.out.println("Penutupan koneksi gagal!");
}
}
}
Sedangkan untuk client-nya di bawah ini :
Client.java
package client;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
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("\nHost ID tidak ditemukan!\n");
System.exit(1);
}
sendMessages();
}
private static void sendMessages() {
Socket socket = null;
try {
socket = new Socket(host, PORT);
Scanner networkInput = new Scanner(socket.getInputStream());
PrintWriter networkOutput = new PrintWriter(socket.getOutputStream(), true);
//Set up stream untuk masukan dari keyboard...
Scanner userEntry = new Scanner(System.in);
String message, response;
do {
System.out.print("Masukkan pesan ('QUIT' untuk keluar): ");
message = userEntry.nextLine();
//Kirim pesan (message)ke server pada output stream socket...
//Terima respon dari server pada stream input socket...
networkOutput.println(message);
response = networkInput.nextLine();
//Tampilkan respon dari server...
System.out.println("\nSERVER> " + response);
} while (!message.equals("QUIT"));
} catch (IOException ioEx) {
//ioEx.printStackTrace();
} finally {
try {
System.out.println("\nMenutup koneksi...");
socket.close();
} catch (IOException ioEx) {
System.out.println("Gagal menutup koneksi!");
System.exit(1);
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
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("\nHost ID tidak ditemukan!\n");
System.exit(1);
}
sendMessages();
}
private static void sendMessages() {
Socket socket = null;
try {
socket = new Socket(host, PORT);
Scanner networkInput = new Scanner(socket.getInputStream());
PrintWriter networkOutput = new PrintWriter(socket.getOutputStream(), true);
//Set up stream untuk masukan dari keyboard...
Scanner userEntry = new Scanner(System.in);
String message, response;
do {
System.out.print("Masukkan pesan ('QUIT' untuk keluar): ");
message = userEntry.nextLine();
//Kirim pesan (message)ke server pada output stream socket...
//Terima respon dari server pada stream input socket...
networkOutput.println(message);
response = networkInput.nextLine();
//Tampilkan respon dari server...
System.out.println("\nSERVER> " + response);
} while (!message.equals("QUIT"));
} catch (IOException ioEx) {
//ioEx.printStackTrace();
} finally {
try {
System.out.println("\nMenutup koneksi...");
socket.close();
} catch (IOException ioEx) {
System.out.println("Gagal menutup koneksi!");
System.exit(1);
}
}
}
}
Selamat Mencoba!!