Sebelumnya saya mau terima kasih dulu sama dosen pemograman jaringan saya yang sudah memberikan tugas ini..
Saya juga mau ngucapin makasih buat yg sudah bantuin buat program ini.. (Mr.TM u’re “java gods”.. hahahaa)
Program ini adalah program chatting dengan multiThreading client server (buatnya di netbeans yah, coz pake JAVA). Dimana client dapat melakukan chatting secara broadcast dan private message. Disisi server hanya menampilkan list nama-nama client..
Langsung aja deh kalo begitu kita coba yukss… Check it ouT, gan..!!
Langsung aja deh kalo begitu kita coba yukss… Check it ouT, gan..!!
Langkah awal:
1. buat project di Netbeans untuk sisi server nya:
package uaschattingserver;
import java.io.*;
import java.net.*;
public class ChattingServer {
static Socket clientSocket = null;
static ServerSocket serverSocket = null;
static clientThread t[] = new clientThread[10];
public static void main(String args[]) {
BufferedReader br = null;
PrintStream ps = null;
String line;
String nama;
int port_number=2224;
if (args.length < 1)
{
System.out.println("Port number="+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)
{
br = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));
ps = new PrintStream(clientSocket.getOutputStream());
ps.println("Masukan Nama Lengkap Kamu :");
nama = br.readLine();
(t[i] = new clientThread(clientSocket,t)).start();
// System.out.println(t[i].getName());
System.out.println(""+nama+"");
break;
}
}
}
catch (IOException e) {
System.out.println(e);}
}
}
}
class clientThread extends Thread{
BufferedReader is = null;
PrintStream os = null;
Socket clientSocket = null;
clientThread t[];
String name;
public clientThread(Socket clientSocket, clientThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}
public String getNama(){
return name;
}
public void run()
{
String line;
//String name;
try{
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os = new PrintStream(clientSocket.getOutputStream());
os.println("Masukan Nama panggilan Kamu:");
name = is.readLine();
os.println("hai" + name + "Selamat Datang.\nEnter /quit untuk keluar");
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** User "+name+" sudah masuk ke chat room ini !!! ***" );
while (true) {
line = is.readLine();
if(line.startsWith("/quit")) break;
//batas private
if (line.startsWith("+")){
java.util.StringTokenizer ts = new java.util.StringTokenizer(line, ":");
String users= ts.nextToken();
String user = users.substring(1, users.length());
String pesan=ts.nextToken();
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i].getNama().equalsIgnoreCase(user))
t[i].os.println(pesan);
// private
} else {
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+" meninggalkan chat 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){};
}
2. buat project di Netbeans untuk sisi client nya:
package uaschattingclient;
import java.io.*;
import java.net.*;
public class ChattingClient implements Runnable {
// clientClient: client socket
// os: the output stream
// is: the input stream
static Socket clientSocket = null;
static PrintStream os = null;
static BufferedReader is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
public static void main(String[] args) {
int port_number=2224;
String host="localhost";
if (args.length < 2)
{
System.out.println("Host="+host+", port_number="+port_number);
} else {
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
// Open a socket on a given host and port
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Host tidak ditemukan "+host);
} catch (IOException e) {
System.err.println("Tidak dapat menampilkan I/O "+host);
}
// Open a connection to on port port_number
if (clientSocket != null && os != null && is != null) {
try {
// Create a thread to read from the server
new Thread(new ChattingClient()).start();
while (!closed) {
os.println(inputLine.readLine()+'\n');
}
// Clean up:
// close the output stream
// close the input stream
// close the socket
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
public void run() {
String responseLine;
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);
}
}
}
Setelah buat kedua projectnya, project server di run terlebih dahulu server akan menunggu. Kemudian run project client.. maka program akan jalan.. Secara umum ini jika menjalankan client adalah chatting secara broadcast. untuk Chatting secara private message sesama client , maka disisi sebelum mengetikkan pesan, harus ditambah dengan menuliskan nama client yang dituju, contoh:Client akan mengirim message private kepada client yg bernama Budi, maka harus mengetik seperti di bawah ini:+Budi: pesaaaan
7 comments:
kayaknya asik ni bisa bikin aplikasi gini,,, makasih banget ilmunya
makasih... masih belajar juga saya :D
masih byk yg lebih bisa java
thanks yoooah brooo Good Posting
gan error pas ni listing pas di compile di bagian terakhir }
thanks bgd yah bwd program chattingnya soalny bantu bgd buat bljr bikin program chattingny :D
mbak boleh tau ngak output programnya gimana ya..?
kenapa pas di run kok ngak ada muncul apa-apa ya..??
hehhehee :)
izin comot gan
Post a Comment