Client dan Server (D1-5)
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);
}
}
}
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);
}
}
}
Coding diatas juga merupakan bagian dari telnet. berikut ini adalah
coding yang digunakan untuk mengkoneksikan antara client ke server.Saat program server dijalankan maka akan muncul pesan "Aplikasi Server
Hidup..." saat server telah hidup, maka client harus menjalankan program
client dibawah ini.
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);
}
}
}
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);
}
}
}
Listing diatas merupakan listing untuk client, dimana aplikasi client
akan mengirimkan pesan ke server & server akan membalas pesan dari
client. Seletah itu program dijalankan client diminta untuk memasukkan kata kunci yaitu "salam" kemudian server akan membalas dengan kata "salam juga". Output dari program tersebut seperti dibawah ini:
Dan output yang diterima oleh server adalah :
Tidak ada komentar:
Posting Komentar