simpleServer.java
import java.io.*;
import java.net.*;
public class simpleServer {
public final static int TESTPORT = 5000;
public static void main(String args[]) {
ServerSocket checkServer = null;
String line;
BufferedReader is = null;
DataOutputStream os = null;
Socket clientSocket = null;
try {
checkServer = new ServerSocket(TESTPORT);
System.out.println("Aplikasi Server
hidup ...");
} catch (IOException e) {
System.out.println(e);
}
try {
clientSocket = checkServer.accept();
is = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
os = new
DataOutputStream(clientSocket.getOutputStream());
} catch (Exception ei) {
ei.printStackTrace();
}
try {
line = is.readLine();
System.out.println("Terima : " +
line);
if (line.compareTo("salam") == 0) {
os.writeBytes("salam juga");
} else {
os.writeBytes("Maaf, saya tidak
mengerti");
}
} catch (IOException e) {
System.out.println(e);
}
try {
os.close();
is.close();
clientSocket.close();
} catch (IOException ic) {
ic.printStackTrace();
}
}
}
simpleClient.java
import java.io.*;
import java.net.*;
public class simpleClient {
public final static int REMOTE_PORT = 5000;
public static void main(String args[]) throws
Exception {
Socket cl = null;
BufferedReader is = null;
DataOutputStream os = null;
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
String userInput = null;
String output = null;
// Membuka koneksi ke server pada port
REMOTE_PORT
try {
cl = new Socket(args[0], REMOTE_PORT);
is = new BufferedReader(new
InputStreamReader(cl.getInputStream()));
os = new
DataOutputStream(cl.getOutputStream());
} catch(UnknownHostException e1) {
System.out.println("Unknown Host: "
+ e1);
} catch (IOException e2) {
System.out.println("Erorr io: " +
e2);
}
// Menulis ke server
try {
System.out.print("Masukkan kata kunci:
");
userInput = stdin.readLine();
os.writeBytes(userInput + "\n");
} catch (IOException ex) {
System.out.println("Error writing to
server..." + ex);
}
// Menerima tanggapan dari server
try {
output = is.readLine();
System.out.println("Dari server: "
+ output);
} catch (IOException e) {
e.printStackTrace();
}
// close input stream, output stream dan
koneksi
try {
is.close();
os.close();
cl.close();
} catch (IOException x) {
System.out.println("Error
writing...." + x);
}
}
}
Logika Program
Listing program SimpleClient
blok program
diatas ini merupakan pengimportan library java dari masing – masing program,
yaitu SimpleClient dan SimpleServer. Beserta port info dengan value 50 ribu.
Lalu di InfoClient ditargetkan untuk ping dari pc yg dituju. menggunakan handle
try,,,catch dengan kondisi perulangan didalamnya terdapat pengulangan apabila
terpenuhi. Program InfoClient ini akan menangkap fungsi DataOutputStream dari InfoServer yaitu
outToClient lalu akan meminta kita untuk menginput TIM/time/TIME, net/NET,
QUIT/quit selain itu program akan error. Program InfoClient ini juga menerima
hasilnya getInputStream dari InfoServer dan akan berhenti apabila user
menginput quit.
Listing Program SimpleServer
Blok program
diatas ini menggunakan handle try,,,catch dengan kondisi perulangan didalamnya
terdapat pengulangan apabila terpenuhi. Program akan memberikan respon apabila
program dari InfoClient telah di run dengan memberitahu user “ada yg
terkoneksi” setelah itu program InfoServer akan mengirim pesan ke InfoClient
melalui fungsi java DataOutputStream dengan nama variable outToClient. Lalu
menunggu respon dengan menerima inputan dari InfoClient apabila nilai inputan
yang dimasukan adalah TIM/time/TIME, net/NET, QUIT/quit selain itu program akan
error. Akan berhenti apabila dari infoClient mendapatkan input quit.
OUTPUT
Tidak ada komentar:
Posting Komentar