1. MultiThreadChatServer.java
import java.io.*;
import java.net.*;
public class MultiThreadChatServer{
// Declaration section:
// declare a server socket and a client socket for the server
// declare an input and an output stream
static Socket clientSocket = null;
static ServerSocket serverSocket = null;
// This chat server can accept up to 10 clients' connections
static clientThread t[] = new clientThread[10];
public static void main(String args[]) {
// The default port
int port_number=2212;
if (args.length < 1)
{
System.out.println("Usage: java MultiThreadChatServer \n"+ "Port yang sedang digunakan="+port_number);
} else {
port_number=Integer.valueOf(args[0]).intValue();
}
try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e)
{System.out.println(e);}
while(true){
try {
clientSocket = serverSocket.accept();
for(int i=0; i<=9; i++){
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}
catch (IOException e) {
System.out.println(e);}
}
}
}
2. MultiThreadChatClient
import java.io.*;
import java.net.*;
public class MultiThreadChatClient implements Runnable{
// keterangan deklarasi
// clientClient: the client socket
// os: output stream
// is: input stream
static Socket clientSocket = null;
static PrintStream os = null;
static DataInputStream is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
public static void main(String[] args) {
// port default
int port_number=2212;
String host="localhost";
if (args.length < 2)
{
System.out.println("Usage: java MultiThreadChatClient \n"+ "Host yang digunakan="+host+", Nomer Port="+port_number);
} else {
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
// Initialization section:
// membuka socket dengan memberikan host dan port
// membuka input dan output stream
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader
(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("HOST TAK TERDETEKSI "+host);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host "+host);
}
if (clientSocket != null && os != null && is != null) {
try {
// membuat thread untuk membaca dari server
new Thread(new MultiThreadChatClient()).start();
while (!closed) {
os.println(inputLine.readLine());
}
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
public void run() {
String responseLine;
// server tetap akan hidup selama kita belum menulis "bye""
try{
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1) break;
}
closed=true;
} catch (IOException e) {
System.err.println("IOException: " + e); } }}
3. clientThread.java
import java.io.*;
import java.net.*;
class clientThread extends Thread{
DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
clientThread t[];
public clientThread(Socket clientSocket, clientThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}
public void run()
{
String line;
String name;
try{
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
os.println("Masukkan Nama Anda : .");
name = is.readLine();
os.println("Hello "+name+" anda telah bergabung di chat room.\nuntuk keluar dari room masukkan /quit pada baris baru");
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** User Baru "+name+" masuk di chat room !!! ***" );
while (true) {
line = is.readLine();
if(line.startsWith("/quit")) break;
for(int i=0; i<=9; i++)
if (t[i]!=null) t[i].os.println("<"+name+"> "+line);
}
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** User "+name+" telah meninggalkan room !!! ***" );
os.println("*** Bye "+name+" ***");
for(int i=0; i<=9; i++)
if (t[i]==this) t[i]=null;
is.close();
os.close();
clientSocket.close();
}
catch(IOException e){};
}
}
Cara Pengoperasian Program
1. Run MultiThreadChatServer.java
Setelah itu akan muncul jendela sebagai berikut :
2. Run MultiThreadChatClient.java
Setelah itu akan muncul jendela sebagai berikut :
Setelah itu masukan nama anda, misalnya Budi
3. Untuk menambahkan client baru kita tinggal melakukan run kembali MultiThreadChatClient.java dan masukan kembali nama client. Misalnya Frida
Ketika Frida masuk ke dalam chat room maka user Budi akan mendapat pemberitahuan
4. Setelah itu dapat saling mengirimkan pesan antara user yang telah masuk kedalam chat room
5. Untuk keluar dari Char room kita tinggal mengetikan /quit
Ketika Budi telah meninggalkan Chat room maka Frida akan mendapat pemberitahuan bahwa Budi telah meninggalkan Chat room