Chatting
Menggunakan Java
Untuk menjalankan program chatting
menggunakan java, ada 2 program yang harus dibuat,pertama program chat server
dan program chat client. Untuk program chat server, berikut listing programnya:
import
java.io.DataInputStream;
import
java.io.PrintStream;
import
java.io.IOException;
import
java.net.Socket;
import
java.net.ServerSocket;
public
class MultiThreadChatServer {
private static ServerSocket serverSocket =
null;
private static Socket clientSocket = null;
private static final int maxClientsCount =
10;
private static final clientThread[] threads =
new clientThread[maxClientsCount];
public static void main(String args[]) {
int portNumber = 2222;
if (args.length < 1) {
System.out
.println("Usage: java
MultiThreadChatServer <portNumber>\n"
+ "Now using port
number=" + portNumber);
} else {
portNumber =
Integer.valueOf(args[0]).intValue();
}
try {
serverSocket = new
ServerSocket(portNumber);
} catch (IOException e) {
System.out.println(e);
}
while (true) {
try {
clientSocket = serverSocket.accept();
int i = 0;
for (i = 0; i < maxClientsCount;
i++) {
if (threads[i] == null) {
(threads[i] = new clientThread(clientSocket,
threads)).start();
break;
}
}
if (i == maxClientsCount) {
PrintStream os = new
PrintStream(clientSocket.getOutputStream());
os.println("Server too busy. Try
later.");
os.close();
clientSocket.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
class
clientThread extends Thread {
private DataInputStream is = null;
private PrintStream os = null;
private Socket clientSocket = null;
private final clientThread[] threads;
private int maxClientsCount;
public clientThread(Socket clientSocket,
clientThread[] threads) {
this.clientSocket = clientSocket;
this.threads = threads;
maxClientsCount = threads.length;
}
public void run() {
int maxClientsCount = this.maxClientsCount;
clientThread[] threads = this.threads;
try {
is = new
DataInputStream(clientSocket.getInputStream());
os
= new PrintStream(clientSocket.getOutputStream());
os.println("Enter your name.");
String name = is.readLine().trim();
os.println("Hello " + name
+ " to our chat room.\nTo leave
enter /quit in a new line");
for (int i = 0; i < maxClientsCount;
i++) {
if (threads[i] != null &&
threads[i] != this) {
threads[i].os.println("*** A new
user " + name
+ " entered the chat room
!!! ***");
}
}
while (true) {
String line = is.readLine();
if (line.startsWith("/quit"))
{
break;
}
for (int i = 0; i < maxClientsCount;
i++) {
if (threads[i] != null) {
threads[i].os.println("<" + name + "&gr; " +
line);
}
}
}
for (int i = 0; i < maxClientsCount;
i++) {
if (threads[i] != null &&
threads[i] != this) {
threads[i].os.println("*** The
user " + name
+ " is leaving the chat room
!!! ***");
}
}
os.println("*** Bye " + name +
" ***");
for (int i = 0; i < maxClientsCount;
i++) {
if (threads[i] == this) {
threads[i] = null;
}
}
is.close();
os.close();
clientSocket.close();
} catch (IOException e) {
}
}
}
Untuk program chat client, berikut
listingnya:
import
java.io.DataInputStream;
import
java.io.PrintStream;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.io.IOException;
import
java.net.Socket;
import
java.net.UnknownHostException;
public
class MultiThreadChatClient implements Runnable {
private static Socket clientSocket = null;
private static PrintStream os = null;
private static DataInputStream is = null;
private static BufferedReader inputLine =
null;
private static boolean closed = false;
public static void main(String[] args) {
int portNumber = 2222;
String host = "192.168.1.1";
if (args.length < 2) {
System.out
.println("Usage: java
MultiThreadChatClient <host> <portNumber>\n"
+ "Now using host=" +
host + ", portNumber=" + portNumber);
} else {
host = args[0];
portNumber =
Integer.valueOf(args[1]).intValue();
}
try {
clientSocket = new Socket(host,
portNumber);
inputLine = new BufferedReader(new
InputStreamReader(System.in));
os = new
PrintStream(clientSocket.getOutputStream());
is = new
DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about
host " + 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 {
new Thread(new
MultiThreadChatClient()).start();
while (!closed) {
os.println(inputLine.readLine().trim());
}
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);
}
}
}
Jika program sudah dibuat, pastika
komputer terhubung dengan internet. Jalankan program chat server, biarkan tetap
terbuka, tunggu sampai komputer teman terhubung:
Untuk chat client, jalankan
programnya,
Masukkan username anda:
Tunggu sampai punya teman
terhubung, jika sudah, chatting bisa langsung dijalankan:
0 komentar:
Posting Komentar