SlideShare a Scribd company logo
1 of 8
Pemrograman Jaringan Komputer
Chatting dengan Beberapa PC/Laptop
Program ini digunakan untuk chating dengan beberapa PC/Laptop. Berikut adalah
listing codenya.
MultiThreadChatServer.java
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
/*
* chat server yang mengantar pesan publik dan private.
*/
public class MultiThreadChatServer {
// server socket.
private static ServerSocket serverSocket = null;
// client socket.
private static Socket clientSocket = null;
// chat server dapat menerima sampai maxClientsCount pada koneksi
client.
private static final int maxClientsCount = 10;
private static final clientThread[] threads = new
clientThread[maxClientsCount];
public static void main(String args[]) {
// default port number.
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();
}
/*
* buka server socket pada portNumber (default 2222). catatan:kita
tidak da
* pat memilih port dibawah 1023 jika kita bukan administrator
(root).
*/
try {
serverSocket = new ServerSocket(portNumber);
} catch (IOException e) {
System.out.println(e);
}
/*
* buat client socket untuk setiap koneksi dan sesuaikan dengan
thread
* client baru.
*/
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);
}
}
}
}
/*
* 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 da-
* lam chatroom, selama data diterima, data di kembalikan ke semua
* client. saat client meninggalkan chatroom thread ini juga
menginformasikan
* pada client lainnya.
*/
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 {
/*
* membuat input dan output streams untuk client.
*/
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 + " ***");
/*
* buat variabel thread terakhir menjadi null agar client baru
* dapat diterima oleh server.
*/
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] == this) {
threads[i] = null;
}
}
/*
* tutup output stream, tutup input stream, tutup socket.
*/
is.close();
os.close();
clientSocket.close();
} catch (IOException e) {
}
}
}
MultiThreadChatClient.java
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 {
// client socket
private static Socket clientSocket = null;
// output stream
private static PrintStream os = null;
// input stream
private static DataInputStream is = null;
private static BufferedReader inputLine = null;
private static boolean closed = false;
public static void main(String[] args) {
// default port.
int portNumber = 2222;
// default host.
String host = "10.17.193.86";
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();
}
/*
* Buka sebuah socket pada host dan port. Buka input dan output
streams.
*/
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);
}
/*
* Jika semua telah diinisialisasi lalu kita ingin menulis data
pada socket yang kita buka pada koneksi port number.
*/
if (clientSocket != null && os != null && is != null) {
try {
/* Buat sebuah thread untuk membaca dari server. */
new Thread(new MultiThreadChatClient()).start();
while (!closed) {
os.println(inputLine.readLine().trim());
}
/*
* tutup output stream, tutup input stream, tutup socket.
*/
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
/*
* buat sebuah thread untuk membaca dari server.
*/
public void run() {
/*
* tetap membaca dari socket sampai kita menerima "Bye" dari
* server. setelah diterima maka break.
*/
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);
}
}
}
Pertama, running program MultiThreadChatServer.java, kemudian akan muncul
tampilan seperti ini.
Kemudian running program MultiThreadChatClient.java, kemudian pada tampilan
MultiThreadChatClient akan muncul tampilan seperti ini.
Jika sudah terkoneksi, maka anda bisa langsung chat dengan PC/Laptop yang berbeda.

More Related Content

What's hot

swift-nio のアーキテクチャーと RxHttpClient
swift-nio のアーキテクチャーと RxHttpClientswift-nio のアーキテクチャーと RxHttpClient
swift-nio のアーキテクチャーと RxHttpClientShinya Mochida
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machinejulien pauli
 
MultiClient chatting berbasis gambar
MultiClient chatting berbasis gambarMultiClient chatting berbasis gambar
MultiClient chatting berbasis gambaryoyomay93
 
Multi client
Multi clientMulti client
Multi clientAisy Cuyy
 
Netty: asynchronous data transfer
Netty: asynchronous data transferNetty: asynchronous data transfer
Netty: asynchronous data transferVictor Cherkassky
 
MQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker ExamplesMQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker ExamplesMicha Kops
 
Building Java and Android apps on the blockchain
Building Java and Android apps on the blockchain Building Java and Android apps on the blockchain
Building Java and Android apps on the blockchain Conor Svensson
 
Creating an Arduino Web Server from scratch hardware and software
Creating an Arduino Web Server from scratch hardware and softwareCreating an Arduino Web Server from scratch hardware and software
Creating an Arduino Web Server from scratch hardware and softwareJustin Mclean
 
Laporan multiclient chatting berbasis grafis (gambar)
Laporan multiclient chatting berbasis grafis (gambar)Laporan multiclient chatting berbasis grafis (gambar)
Laporan multiclient chatting berbasis grafis (gambar)Rara Ariesta
 
Multi client
Multi clientMulti client
Multi clientganteng8
 

What's hot (20)

Book
BookBook
Book
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
 
swift-nio のアーキテクチャーと RxHttpClient
swift-nio のアーキテクチャーと RxHttpClientswift-nio のアーキテクチャーと RxHttpClient
swift-nio のアーキテクチャーと RxHttpClient
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
 
Whispered secrets
Whispered secretsWhispered secrets
Whispered secrets
 
MultiClient chatting berbasis gambar
MultiClient chatting berbasis gambarMultiClient chatting berbasis gambar
MultiClient chatting berbasis gambar
 
Multi client
Multi clientMulti client
Multi client
 
Reactive server with netty
Reactive server with nettyReactive server with netty
Reactive server with netty
 
Fidl analysis
Fidl analysisFidl analysis
Fidl analysis
 
Netty: asynchronous data transfer
Netty: asynchronous data transferNetty: asynchronous data transfer
Netty: asynchronous data transfer
 
MQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker ExamplesMQTT and Java - Client and Broker Examples
MQTT and Java - Client and Broker Examples
 
Building Java and Android apps on the blockchain
Building Java and Android apps on the blockchain Building Java and Android apps on the blockchain
Building Java and Android apps on the blockchain
 
Creating an Arduino Web Server from scratch hardware and software
Creating an Arduino Web Server from scratch hardware and softwareCreating an Arduino Web Server from scratch hardware and software
Creating an Arduino Web Server from scratch hardware and software
 
Winform
WinformWinform
Winform
 
Learning Dtrace
Learning DtraceLearning Dtrace
Learning Dtrace
 
Web3j 2.0 Update
Web3j 2.0 UpdateWeb3j 2.0 Update
Web3j 2.0 Update
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 
Laporan multiclient chatting berbasis grafis (gambar)
Laporan multiclient chatting berbasis grafis (gambar)Laporan multiclient chatting berbasis grafis (gambar)
Laporan multiclient chatting berbasis grafis (gambar)
 
Php engine
Php enginePhp engine
Php engine
 
Multi client
Multi clientMulti client
Multi client
 

Viewers also liked

This is What Matters
This is What MattersThis is What Matters
This is What Mattersamizen
 
The ABC of IoT
The ABC of IoTThe ABC of IoT
The ABC of IoTcprojector
 
Capps vacation
Capps vacationCapps vacation
Capps vacationmtcapps
 
Info server dan info client
Info server dan info clientInfo server dan info client
Info server dan info clientyayaria
 
Browsing (1)
Browsing (1)Browsing (1)
Browsing (1)yayaria
 
Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...
Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...
Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...helderoliveira85
 
Herbology review HiH
Herbology review HiHHerbology review HiH
Herbology review HiHlouise searle
 
Program sms menggunakan java ria
Program sms menggunakan java riaProgram sms menggunakan java ria
Program sms menggunakan java riayayaria
 

Viewers also liked (17)

Tugas 1
Tugas 1Tugas 1
Tugas 1
 
Waxraninaka pdf
Waxraninaka pdfWaxraninaka pdf
Waxraninaka pdf
 
instalar google drive en windows
instalar google drive en windowsinstalar google drive en windows
instalar google drive en windows
 
This is What Matters
This is What MattersThis is What Matters
This is What Matters
 
Mosa book es-de
Mosa book es-deMosa book es-de
Mosa book es-de
 
The ABC of IoT
The ABC of IoTThe ABC of IoT
The ABC of IoT
 
Capps vacation
Capps vacationCapps vacation
Capps vacation
 
Muaz
MuazMuaz
Muaz
 
Facebook Dilemma
Facebook DilemmaFacebook Dilemma
Facebook Dilemma
 
Info server dan info client
Info server dan info clientInfo server dan info client
Info server dan info client
 
Browsing (1)
Browsing (1)Browsing (1)
Browsing (1)
 
Asap methodology
Asap methodologyAsap methodology
Asap methodology
 
Sinha_WhitePaper
Sinha_WhitePaperSinha_WhitePaper
Sinha_WhitePaper
 
Sustainable transport
Sustainable transportSustainable transport
Sustainable transport
 
Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...
Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...
Apostilaadministracao aplicada-a-seguranca-no-trabalho-150317074308-conversio...
 
Herbology review HiH
Herbology review HiHHerbology review HiH
Herbology review HiH
 
Program sms menggunakan java ria
Program sms menggunakan java riaProgram sms menggunakan java ria
Program sms menggunakan java ria
 

Similar to Chatting dengan beberapa pc laptop

Laporan multi client
Laporan multi clientLaporan multi client
Laporan multi clientichsanbarokah
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programmingashok hirpara
 
I need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docxI need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docxhendriciraida
 
#3 (Multi Threads With TCP)
#3 (Multi Threads With TCP)#3 (Multi Threads With TCP)
#3 (Multi Threads With TCP)Ghadeer AlHasan
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programgovindjha339843
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13Sasi Kala
 
16network Programming Servers
16network Programming Servers16network Programming Servers
16network Programming ServersAdil Jafri
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In JavaAnkur Agrawal
 
I need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxI need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxhendriciraida
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.comphanleson
 

Similar to Chatting dengan beberapa pc laptop (20)

Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Laporan multi client
Laporan multi clientLaporan multi client
Laporan multi client
 
java sockets
 java sockets java sockets
java sockets
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Network
NetworkNetwork
Network
 
Os 2
Os 2Os 2
Os 2
 
I need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docxI need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docx
 
#3 (Multi Threads With TCP)
#3 (Multi Threads With TCP)#3 (Multi Threads With TCP)
#3 (Multi Threads With TCP)
 
A.java
A.javaA.java
A.java
 
#2 (UDP)
#2 (UDP)#2 (UDP)
#2 (UDP)
 
Network programming1
Network programming1Network programming1
Network programming1
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in program
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
16network Programming Servers
16network Programming Servers16network Programming Servers
16network Programming Servers
 
Networking Core Concept
Networking Core ConceptNetworking Core Concept
Networking Core Concept
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
I need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxI need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docx
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 

Recently uploaded

%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

Chatting dengan beberapa pc laptop

  • 1. Pemrograman Jaringan Komputer Chatting dengan Beberapa PC/Laptop Program ini digunakan untuk chating dengan beberapa PC/Laptop. Berikut adalah listing codenya. MultiThreadChatServer.java import java.io.DataInputStream; import java.io.PrintStream; import java.io.IOException; import java.net.Socket; import java.net.ServerSocket; /* * chat server yang mengantar pesan publik dan private. */ public class MultiThreadChatServer { // server socket. private static ServerSocket serverSocket = null; // client socket. private static Socket clientSocket = null; // chat server dapat menerima sampai maxClientsCount pada koneksi client. private static final int maxClientsCount = 10; private static final clientThread[] threads = new clientThread[maxClientsCount]; public static void main(String args[]) { // default port number. 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(); }
  • 2. /* * buka server socket pada portNumber (default 2222). catatan:kita tidak da * pat memilih port dibawah 1023 jika kita bukan administrator (root). */ try { serverSocket = new ServerSocket(portNumber); } catch (IOException e) { System.out.println(e); } /* * buat client socket untuk setiap koneksi dan sesuaikan dengan thread * client baru. */ 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); } } } }
  • 3. /* * 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 da- * lam chatroom, selama data diterima, data di kembalikan ke semua * client. saat client meninggalkan chatroom thread ini juga menginformasikan * pada client lainnya. */ 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 { /* * membuat input dan output streams untuk client. */ 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++) {
  • 4. 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 + " ***"); /* * buat variabel thread terakhir menjadi null agar client baru * dapat diterima oleh server. */ for (int i = 0; i < maxClientsCount; i++) { if (threads[i] == this) { threads[i] = null; } } /* * tutup output stream, tutup input stream, tutup socket. */ is.close(); os.close(); clientSocket.close(); } catch (IOException e) {
  • 5. } } } MultiThreadChatClient.java 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 { // client socket private static Socket clientSocket = null; // output stream private static PrintStream os = null; // input stream private static DataInputStream is = null; private static BufferedReader inputLine = null; private static boolean closed = false; public static void main(String[] args) { // default port. int portNumber = 2222; // default host. String host = "10.17.193.86"; if (args.length < 2) { System.out .println("Usage: java MultiThreadChatClient <host> <portNumber>n" + "Now using host=" + host + ", portNumber=" + portNumber); } else { host = args[0];
  • 6. portNumber = Integer.valueOf(args[1]).intValue(); } /* * Buka sebuah socket pada host dan port. Buka input dan output streams. */ 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); } /* * Jika semua telah diinisialisasi lalu kita ingin menulis data pada socket yang kita buka pada koneksi port number. */ if (clientSocket != null && os != null && is != null) { try { /* Buat sebuah thread untuk membaca dari server. */ new Thread(new MultiThreadChatClient()).start(); while (!closed) { os.println(inputLine.readLine().trim()); } /* * tutup output stream, tutup input stream, tutup socket. */ os.close(); is.close(); clientSocket.close(); } catch (IOException e) { System.err.println("IOException: " + e);
  • 7. } } } /* * buat sebuah thread untuk membaca dari server. */ public void run() { /* * tetap membaca dari socket sampai kita menerima "Bye" dari * server. setelah diterima maka break. */ 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); } } } Pertama, running program MultiThreadChatServer.java, kemudian akan muncul tampilan seperti ini. Kemudian running program MultiThreadChatClient.java, kemudian pada tampilan MultiThreadChatClient akan muncul tampilan seperti ini.
  • 8. Jika sudah terkoneksi, maka anda bisa langsung chat dengan PC/Laptop yang berbeda.