SlideShare a Scribd company logo
LAPORAN TUGAS NETWORK PROGRAMMING
NAMA : RAHMAT HAMDANI
NIM: 201913002
Keterangan : Permasalahannyaterletakdisaatrun,chatyang dikirimolehclienttidakbisa
diketahui olehclientlain.Programtidakmendukunguntukmelakukanbroadcastantar client.
Server hanya bisa menerima satu client dan tidak menerima lebih dari satu client. Indikasi
permasalahannya terletak pada kekuatan jaringan yang tidak mendukung.
Source code
package syarifchatting;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.Normalizer.Form;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
/**
* @author MhdSyarif
* Monday, 13 January 2014, 20 : 52 : 20 WIB
* Tugas III Matakulia Java2SE
* Mhd. Syarif | 49013075
* TKJMD - STEI - ITB
*/
public class Server extends javax.swing.JFrame implements Runnable{
int port=8080;
Socket client;
ServerSocket server;
BufferedReader Server_Reader, Client_Reader;
BufferedWriter Server_Writer, Client_Writer;
/**
* Creates new form Server
*/
public Server() {
super("www.mhdsyarif.com"); //SetTitle
initComponents();
//Menampilkan hasil ditengah window
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-
dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
getBackgroundColor();
}
//Pilihan ComboBox
private void getComboBox(){
if(ComboBox.getSelectedItem().equals("Server")){
ButtonOn.setText("On");
Username.setText("Server");
}else{
ButtonOn.setText("Connect");
Username.setText("Client");
}
}
//Koneksi client ke server
private void getClientConnec(){
try {
String ip = JOptionPane.showInputDialog(" Input IP Address");
client = new Socket(ip, port);
ComboBox.setEnabled(false);
ButtonOn.setText("Disconnect");
Server_Reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
Server_Writer = new BufferedWriter (new
OutputStreamWriter(client.getOutputStream()));
} catch (UnknownHostException ex) {
System.out.println("Accept Failed");
System.exit(-1);
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
}
private void getReadConnec(){
// try {
// try {
// try {
// server = new ServerSocket(port);
// this.setTitle("Please Wait");
// } catch (IOException ex) {
// System.out.println("Could not listen");
// System.exit(-1);
// }
// client = server.accept();
// this.setTitle("Connected" + client.getInetAddress());
// } catch (IOException ex) {
// System.out.println("Accept Failed");
// System.exit(-1);
// }
// Server_Reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
// Server_Writer = new BufferedWriter(new
OutputStreamWriter(client.getOutputStream()));
// } catch (IOException ex) {
// System.out.println("Read Failed");
// System.exit(-1);
// }
final int PORT = 8080;
try {
server = new ServerSocket(PORT);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE,
null, ex);
}
System.out.println("Server started...");
System.out.println("Wating for clients...");
while (true) {
try {
client = server.accept();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE,
null, ex);
}
Thread t = new Thread() {
public void run() {
try {
Server_Reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Server_Writer = new BufferedWriter (new
OutputStreamWriter(client.getOutputStream()));
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
try (
PrintWriter out = new
PrintWriter(client.getOutputStream(), true);
Scanner in = new Scanner(client.getInputStream());
) {
while (in.hasNextLine()) {
String input = in.nextLine();
if (input.equalsIgnoreCase("exit")) {
break;
}
System.out.println("Received radius from
client: " + input);
// double radius = Double.valueOf(input);
// double area = Math.PI* radius *radius ;
// out.println(area);
}
} catch (IOException e) { }
}
};
t.start();
}
}
private void getDisconnectedClient(){
try {
client.close();
Server_Reader.close();
Server_Writer.close();
ComboBox.setEnabled(true);
ButtonOn.setText("Connect");
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
}
private void getDisconnectedServer(){
try {
Server_Reader.close();
Server_Writer.close();
ButtonOn.setText("On");
this.setTitle("Disconected");
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
}
private void getButtonOn(){
if(ButtonOn.getText().equals("Connect"))
{
ButtonOn.setText("DIsconnect");
getClientConnec();
Thread thread1= new Thread(this);
Thread thread2= new Thread(this);
Thread thread3= new Thread(this);
Thread thread4= new Thread(this);
Thread thread5= new Thread(this);
Thread thread6= new Thread(this);
Thread thread7= new Thread(this);
Thread thread8= new Thread(this);
Thread thread9= new Thread(this);
Thread thread10= new Thread(this);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
} else if(ComboBox.getSelectedItem().equals("Server")){
ButtonOn.setText("Off");
getReadConnec();
Thread thread1= new Thread(this);
Thread thread2= new Thread(this);
Thread thread3= new Thread(this);
Thread thread4= new Thread(this);
Thread thread5= new Thread(this);
Thread thread6= new Thread(this);
Thread thread7= new Thread(this);
Thread thread8= new Thread(this);
Thread thread9= new Thread(this);
Thread thread10= new Thread(this);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}else if(ButtonOn.getText().equals("Disconnect")){
getDisconnectedClient();
}else if(ButtonOn.getText().equals("Off")){
getDisconnectedServer();
}
}
private void getSend(){
try {
Server_Writer.write(Username.getText()+ ":
"+TextChat.getText());
Server_Writer.newLine();
Server_Writer.flush();
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
ListChat.add(Username.getText()+ ": " +TextChat.getText());
//jClient.setText(Username.getText());
TextChat.setText("");
}
//Background color
public void getBackgroundColor(){
Color c = JColorChooser.showDialog(null,"Background
Color",jPanel.getBackground());
jPanel.setBackground(c);
}
//Konfirmasi keluar
public void getExit(){
int confirm =JOptionPane.showConfirmDialog(this,"Are you sure will
exit this application ?","Exit
Application",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (confirm == JOptionPane.YES_OPTION){
System.exit(0);
}
}

More Related Content

PDF
Tdd iPhone For Dummies
DOCX
Chatting dengan beberapa pc laptop
PDF
Exploit ie using scriptable active x controls version English
PDF
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
PDF
Connect2017 DEV-1550 Why Java 8? Or, What's a Lambda?
PDF
Networks lab
DOCX
network programing lab file ,
ODP
Sockets in nach0s
Tdd iPhone For Dummies
Chatting dengan beberapa pc laptop
Exploit ie using scriptable active x controls version English
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
Connect2017 DEV-1550 Why Java 8? Or, What's a Lambda?
Networks lab
network programing lab file ,
Sockets in nach0s

What's hot (19)

PPT
Web
PDF
PPTX
Full Stack Unit Testing
PDF
Unit Testing Express and Koa Middleware in ES2015
PDF
Enhanced Web Service Testing: A Better Mock Structure
PPTX
Node.js System: The Approach
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
PPTX
Monitoring patterns for mitigating technical risk
PDF
Unit Testing Express Middleware
PPTX
node.js errors
PDF
The Ring programming language version 1.5.3 book - Part 89 of 184
PDF
Ajax World Comet Talk
PDF
Clean code via dependency injection + guice
PPTX
Unit testing CourseSites Apache Filter
PDF
Architecture for Massively Parallel HDL Simulations
PPT
Csphtp1 22
PPTX
Qunit Java script Un
PDF
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PDF
Testing in android
Web
Full Stack Unit Testing
Unit Testing Express and Koa Middleware in ES2015
Enhanced Web Service Testing: A Better Mock Structure
Node.js System: The Approach
Advanced Jasmine - Front-End JavaScript Unit Testing
Monitoring patterns for mitigating technical risk
Unit Testing Express Middleware
node.js errors
The Ring programming language version 1.5.3 book - Part 89 of 184
Ajax World Comet Talk
Clean code via dependency injection + guice
Unit testing CourseSites Apache Filter
Architecture for Massively Parallel HDL Simulations
Csphtp1 22
Qunit Java script Un
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
Testing in android
Ad

Similar to Laporan tugas network programming (20)

DOCX
201913046 wahyu septiansyah network programing
PDF
Server1
PDF
Laporan multiclient chatting berbasis grafis (gambar)
PDF
Multi client
PDF
Laporan multiclient chatting client server
PDF
MultiClient chatting berbasis gambar
DOCX
Multi client
DOCX
Laporan multi client
PDF
Tugas 2
DOCX
Fia fabila
PDF
201913001 khairunnisa progres_harian
PPT
Pemrograman Jaringan
PDF
Hi, I need some one to help me with Design a client-server Chat so.pdf
PPT
Socket Programming
PPT
Multi user chat system using java
PPT
Chapter 4 slides
PDF
Advanced Java Practical File
PPTX
#3 (Multi Threads With TCP)
DOCX
Please look at the attach See.doc. I am getting this error all th.docx
PDF
TCPIP Client Server program exampleHere is a simple example of .pdf
201913046 wahyu septiansyah network programing
Server1
Laporan multiclient chatting berbasis grafis (gambar)
Multi client
Laporan multiclient chatting client server
MultiClient chatting berbasis gambar
Multi client
Laporan multi client
Tugas 2
Fia fabila
201913001 khairunnisa progres_harian
Pemrograman Jaringan
Hi, I need some one to help me with Design a client-server Chat so.pdf
Socket Programming
Multi user chat system using java
Chapter 4 slides
Advanced Java Practical File
#3 (Multi Threads With TCP)
Please look at the attach See.doc. I am getting this error all th.docx
TCPIP Client Server program exampleHere is a simple example of .pdf
Ad

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Computing-Curriculum for Schools in Ghana
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
RMMM.pdf make it easy to upload and study
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Lesson notes of climatology university.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Types and Its function , kingdom of life
Pharmacology of Heart Failure /Pharmacotherapy of CHF
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
GDM (1) (1).pptx small presentation for students
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Microbial disease of the cardiovascular and lymphatic systems
102 student loan defaulters named and shamed – Is someone you know on the list?
Computing-Curriculum for Schools in Ghana
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Supply Chain Operations Speaking Notes -ICLT Program
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
RMMM.pdf make it easy to upload and study
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Laporan tugas network programming

  • 1. LAPORAN TUGAS NETWORK PROGRAMMING NAMA : RAHMAT HAMDANI NIM: 201913002 Keterangan : Permasalahannyaterletakdisaatrun,chatyang dikirimolehclienttidakbisa diketahui olehclientlain.Programtidakmendukunguntukmelakukanbroadcastantar client.
  • 2. Server hanya bisa menerima satu client dan tidak menerima lebih dari satu client. Indikasi permasalahannya terletak pada kekuatan jaringan yang tidak mendukung. Source code package syarifchatting; import java.awt.Color; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.text.Normalizer.Form; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JColorChooser; import javax.swing.JOptionPane; /** * @author MhdSyarif * Monday, 13 January 2014, 20 : 52 : 20 WIB * Tugas III Matakulia Java2SE * Mhd. Syarif | 49013075 * TKJMD - STEI - ITB */
  • 3. public class Server extends javax.swing.JFrame implements Runnable{ int port=8080; Socket client; ServerSocket server; BufferedReader Server_Reader, Client_Reader; BufferedWriter Server_Writer, Client_Writer; /** * Creates new form Server */ public Server() { super("www.mhdsyarif.com"); //SetTitle initComponents(); //Menampilkan hasil ditengah window java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); java.awt.Dimension dialogSize = getSize(); setLocation((screenSize.width- dialogSize.width)/2,(screenSize.height-dialogSize.height)/2); getBackgroundColor(); } //Pilihan ComboBox private void getComboBox(){ if(ComboBox.getSelectedItem().equals("Server")){ ButtonOn.setText("On"); Username.setText("Server"); }else{ ButtonOn.setText("Connect"); Username.setText("Client"); } } //Koneksi client ke server private void getClientConnec(){ try { String ip = JOptionPane.showInputDialog(" Input IP Address"); client = new Socket(ip, port); ComboBox.setEnabled(false); ButtonOn.setText("Disconnect"); Server_Reader = new BufferedReader(new InputStreamReader(client.getInputStream())); Server_Writer = new BufferedWriter (new OutputStreamWriter(client.getOutputStream())); } catch (UnknownHostException ex) { System.out.println("Accept Failed"); System.exit(-1); } catch (IOException ex) { Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex); } } private void getReadConnec(){ // try { // try { // try { // server = new ServerSocket(port); // this.setTitle("Please Wait"); // } catch (IOException ex) {
  • 4. // System.out.println("Could not listen"); // System.exit(-1); // } // client = server.accept(); // this.setTitle("Connected" + client.getInetAddress()); // } catch (IOException ex) { // System.out.println("Accept Failed"); // System.exit(-1); // } // Server_Reader = new BufferedReader(new InputStreamReader(client.getInputStream())); // Server_Writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); // } catch (IOException ex) { // System.out.println("Read Failed"); // System.exit(-1); // } final int PORT = 8080; try { server = new ServerSocket(PORT); } catch (IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Server started..."); System.out.println("Wating for clients..."); while (true) { try { client = server.accept(); } catch (IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } Thread t = new Thread() { public void run() { try { Server_Reader = new BufferedReader(new InputStreamReader(client.getInputStream())); } catch (IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } try { Server_Writer = new BufferedWriter (new OutputStreamWriter(client.getOutputStream())); } catch (IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } try ( PrintWriter out = new PrintWriter(client.getOutputStream(), true); Scanner in = new Scanner(client.getInputStream()); ) {
  • 5. while (in.hasNextLine()) { String input = in.nextLine(); if (input.equalsIgnoreCase("exit")) { break; } System.out.println("Received radius from client: " + input); // double radius = Double.valueOf(input); // double area = Math.PI* radius *radius ; // out.println(area); } } catch (IOException e) { } } }; t.start(); } } private void getDisconnectedClient(){ try { client.close(); Server_Reader.close(); Server_Writer.close(); ComboBox.setEnabled(true); ButtonOn.setText("Connect"); } catch (IOException ex) { Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex); } } private void getDisconnectedServer(){ try { Server_Reader.close(); Server_Writer.close(); ButtonOn.setText("On"); this.setTitle("Disconected"); } catch (IOException ex) { Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex); } } private void getButtonOn(){ if(ButtonOn.getText().equals("Connect")) { ButtonOn.setText("DIsconnect"); getClientConnec(); Thread thread1= new Thread(this); Thread thread2= new Thread(this); Thread thread3= new Thread(this); Thread thread4= new Thread(this); Thread thread5= new Thread(this); Thread thread6= new Thread(this); Thread thread7= new Thread(this); Thread thread8= new Thread(this); Thread thread9= new Thread(this); Thread thread10= new Thread(this); thread1.start(); thread2.start();
  • 6. thread3.start(); thread4.start(); thread5.start(); thread6.start(); thread7.start(); thread8.start(); thread9.start(); thread10.start(); } else if(ComboBox.getSelectedItem().equals("Server")){ ButtonOn.setText("Off"); getReadConnec(); Thread thread1= new Thread(this); Thread thread2= new Thread(this); Thread thread3= new Thread(this); Thread thread4= new Thread(this); Thread thread5= new Thread(this); Thread thread6= new Thread(this); Thread thread7= new Thread(this); Thread thread8= new Thread(this); Thread thread9= new Thread(this); Thread thread10= new Thread(this); thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread5.start(); thread6.start(); thread7.start(); thread8.start(); thread9.start(); thread10.start(); }else if(ButtonOn.getText().equals("Disconnect")){ getDisconnectedClient(); }else if(ButtonOn.getText().equals("Off")){ getDisconnectedServer(); } } private void getSend(){ try { Server_Writer.write(Username.getText()+ ": "+TextChat.getText()); Server_Writer.newLine(); Server_Writer.flush(); } catch (IOException ex) { Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex); } ListChat.add(Username.getText()+ ": " +TextChat.getText()); //jClient.setText(Username.getText()); TextChat.setText(""); } //Background color public void getBackgroundColor(){ Color c = JColorChooser.showDialog(null,"Background Color",jPanel.getBackground()); jPanel.setBackground(c); } //Konfirmasi keluar
  • 7. public void getExit(){ int confirm =JOptionPane.showConfirmDialog(this,"Are you sure will exit this application ?","Exit Application",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if (confirm == JOptionPane.YES_OPTION){ System.exit(0); } }