Вы находитесь на странице: 1из 5

6.

Implementesi program socket


dos.close();
Soal1_Server s1.close();
}
import java.io.DataInputStream; }
import java.io.IOException;
import java.net.*;
Soal2_server
public class server {
public class server {
public static void main(String args[]) throws
IOException { public static void main(String args[]) throws
// Register service on port 1254 IOException {
ServerSocket s = new ServerSocket(1254); // Register service on port 1254
Socket s1 = s.accept(); // Wait and accept a ServerSocket s = new ServerSocket(1254);
connection Socket s1 = s.accept(); // Wait and accept a
// Get an input file handle from the socket and connection
read the input // Get an input file handle from the socket
DataInputStream dis = new and read the input
DataInputStream(s1.getInputStream()); DataInputStream dis = new
String st = new String(dis.readUTF()); DataInputStream(s1.getInputStream());
System.out.println(st); String st = new String(dis.readUTF());
// When done, just close the connection and System.out.println(st);
exit
dis.close(); // Get a communication stream associated
s1.close(); with the socket
DataOutputStream dos = new
} DataOutputStream(s1.getOutputStream());
} // Send a string!
dos.writeUTF("pagi juga");

Soal1_Client // When done, just close the connection and


exit
public class Client { dis.close();
public static void main(String args[]) throws s1.close();
IOException
{ }
// Register service on port 1254 }
Socket s1=new Socket("192.168.1.15", 1254);
// Wait and accept a connection
// Get a communication stream associated with Soal2_Client
the socket
DataOutputStream dos = new public class Soal2_client {
DataOutputStream (s1.getOutputStream());
// Send a string! public static void main(String args[]) throws
dos.writeUTF("Bersemangatlah Kamu anak IOException {
Muda"); Socket s1 = new Socket("192.168.1.15",
// Close the connection, but not the server 1254);
String a = DataOutputStream dos1 = new
JOptionPane.showInputDialog(null, "Masukkan DataOutputStream(s1.getOutputStream());
Katamu"); // Send a string!
DataOutputStream dos = new if (st.equals("1")) {
DataOutputStream(s1.getOutputStream()); dos1.writeUTF("Soto");
// Send a string! } else if (st.equals("2")) {
dos.writeUTF(a); dos1.writeUTF("Bakso");
DataInputStream dis = new
DataInputStream(s1.getInputStream()); } else {
String st = new String(dis.readUTF()); dos1.writeUTF("Sate");
System.out.println(st);
// When done, just close the connection and }
exit
dis.close(); // When done, just close the connection and
exit
dos.close(); dis.close();
s1.close(); s1.close();
}
} }
}

Soal3_server
Soal3_client
public class server {
public class soal3_client {
public static void main(String args[]) throws
IOException { public static void main(String args[]) throws
// Register service on port 1254 IOException {
ServerSocket s = new ServerSocket(1254); // Register service on port 1254
Socket s1 = s.accept(); // Wait and accept a Socket s1 = new Socket("192.168.1.15",
connection 1254);
// Get a communication stream associated
with the socket DataInputStream dis = new
DataOutputStream dos = new DataInputStream(s1.getInputStream());
DataOutputStream(s1.getOutputStream()); String st = new String(dis.readUTF());
// Send a string! JOptionPane.showMessageDialog(null, st);
dos.writeUTF("1. Soto \n 2. Bakso \n 3. System.out.println(st);
Sate"); DataOutputStream dos = new
DataOutputStream(s1.getOutputStream());
// Get an input file handle from the socket String a =
and read the input JOptionPane.showInputDialog(null, "Masukkan
DataInputStream dis = new Pilihanmu");
DataInputStream(s1.getInputStream()); dos.writeUTF(a);
String st = new String(dis.readUTF()); DataInputStream dis1 = new
System.out.println(st); DataInputStream(s1.getInputStream());
String st1 = new String(dis1.readUTF());
// Get a communication stream associated JOptionPane.showMessageDialog(null, st1);
with the socket System.out.println(st1);
// When done, just close the connection and DataOutputStream dos = new
exit DataOutputStream(s1.getOutputStream());
dis.close(); String a =
dis1.close(); JOptionPane.showInputDialog(null, "Pesan
mu");
dos.close(); dos.writeUTF(a);
s1.close(); if(konek=false){
} dos.close();
} }
}

Soal4_server
}
public class server { }

public static void main(String args[]) throws


IOException { 7. Kesimpulan
ServerSocket s = new ServerSocket(1254);
Socket s1 = s.accept(); // Wait and accept a
connection

boolean kondisi = true;


DataInputStream dis = new
DataInputStream(s1.getInputStream());

for (int a = 0; a < 5; a++) {


String st = new String(dis.readUTF());
System.out.println(st);
if (kondisi == false) {
dis.close();
s1.close();
}
}

}
}

Soal4_client

public class Soal4_Client {


public static void main(String args[]) throws
IOException {
boolean konek=true;
Socket s1 = new Socket("192.168.1.15",
1254);
for(int i=0;i<5;i++){
Refrensi

1. Krawetz Neal.2007. Introduction To Network Security.Boston: CHARLES RIVER MEDIA


2. David Reilly, Michael Reilly . 2002.Java™ Network Programming and Distributed Computing.
Boston: Pearson Education

Вам также может понравиться