Tugas 4 Interoperabilitas Aplikasi Chat Sederhana
Tugas 4 Interoperabilitas Aplikasi Chat Sederhana
INTEROPERABILITAS
APLIKASI CHAT SEDERHANA
Disusun Oleh :
FIRMAN AZIZ
121051124
121051068\
SAFRIAL AMRI
YOSI IMAN SETIADI
DALDIRI
121051080
121051005
121051050
import java.io.*;
import java.net.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Windows7Ar
*/
public class Server {
//Socket server dan client
private static ServerSocket ss=null;
private static Socket cs=null;
//jumlah client
private static final int maxClient= 5;
private static final clientThread[] Threads = new clientThread[maxClient];
public static void main(String[] args) {
int port=2626;
if(args.length < 1){
System.out.println("Server Online with port "+port+" "
+ "\nWaiting for user");
}else{
port=Integer.valueOf(args[0]).intValue();
}
try{
ss = new ServerSocket(port);
}catch(IOException e){System.out.println(e);}
//client socket untuk setiap koneksi
while(true){
try{
cs = ss.accept();
int i=0;
for(i=0; i<maxClient; i++){
if(Threads[i]==null){
(Threads[i]= new clientThread(cs,Threads)).start();
break;
}
}
if(i==maxClient){
PrintStream os= new PrintStream(cs.getOutputStream());
os.println("Server Busy");
os.close();
cs.close();
}
}catch(IOException e){System.out.println(e);}
}
}
/* *Thread chat client .
thread client ini membuka input dan output * streams untuk hubungan client,
menanyakan nama client,menginformasikan semua * client yang terhubung ke
server bahwa ada client baru
yang terhubung ke dalam chatroom, selama data diterima, data di kembalikan
ke semua * client.
saat client meninggalkan chatroom thread ini juga menginformasikan * pada
client lainnya. */
private static class clientThread extends Thread {
private DataInputStream in;
private PrintStream os;
private Socket cs;
private final clientThread[] Threads;
private int maxClient;
public clientThread(Socket cs, clientThread[] Threads) {
this.cs= cs;
this.Threads=Threads;
maxClient=Threads.length;
}
public void run(){
int maxClient=this.maxClient;
clientThread[] Threads= this.Threads;
//membuat input output streams Client
try{
in= new DataInputStream(cs.getInputStream());
os= new PrintStream(cs.getOutputStream());
os.println("Masukkan Nama/Nick Name");
String name=in.readLine().trim();
os.println("Hello "+name+" Selamat Datang << Untuk keluar
ketikan /quit >>");
for(int i=0; i<maxClient; i++){
if(Threads[i] != null && Threads[i] != this){
Threads[i].os.println(name+" Sedang Online");
}
}
while(true){
String line=in.readLine();
if(line.startsWith("/quit")){
break;
}
for(int i=0; i<maxClient; i++){
if(Threads[i] != null){
Threads[i].os.println("<" +name.toUpperCase()+ "> "+line);
}
}
}
for(int i=0; i<maxClient; i++){
if(Threads[i]!=null && Threads[i] != this){
Threads[i].os.println("User "+name+" Meninggalkan ruang
chat !");
}
}
//membuat variabel thread terakhir menjadi null
//agar client baru diterima oleh server
for (int i=0; i<maxClient; i++){
if(Threads[i] == this ){
Threads[i] = null;
}
}
//menutup instream,outstream,dan socket
in.close();
os.close();
cs.close();
}catch(IOException e){System.out.println(e);}
}
}
}
Client.java
import
import
import
import
java.io.*;
java.net.*;
java.util.logging.Level;
java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Windows7Ar
*/
public class Client implements Runnable{
//client socket
private static Socket cs=null;
//output stream
private static PrintStream os=null;
//input stream
private static DataInputStream ins=null;
private static BufferedReader inputLine=null;
private static boolean close=false;
public static void main(String[] args) {
//port yg dipakai
int port=2626;
//host
String host="localhost";
if (args.length<2){
System.out.println("Connected to "+host+" on port "+port);
}else{
host = args[0];
port=Integer.valueOf(args[1]).intValue();
}
//Membuka host,port, inputstream dan outputstream
try{
cs=new Socket(host, port);
inputLine= new BufferedReader(new InputStreamReader(System.in));
os= new PrintStream(cs.getOutputStream());
ins= new DataInputStream(cs.getInputStream());
}catch(UnknownHostException e){System.err.println("Host tidak diketahui
"+e);}
catch (IOException ex) {
System.err.println("Couldn't get I/O for the connection to the host
"+host);
}
if (cs!=null && ins!= null && os!=null){
try{
//Thread untuk membaca dari server
new Thread(new Client()).start();
while(!close){
os.println(inputLine.readLine().trim());
}
//menutup
os.close();
ins.close();
cs.close();
}catch(IOException e){System.err.println(e);}
}
}
//thread untuk membaca dari server
public void run(){
//tetap terhubung dari socket sampai menerima "quit" dari server, lalu
break
String responseLine;
try{
while((responseLine=ins.readLine()) != null){
System.out.println(responseLine);
if(responseLine.indexOf("*** quit") != -1)
break;
}
close=true;
}catch(IOException e){System.err.println("IOException : "+e);}
}
}
Running
Server
Client 1
Client 2