Client merupakan proses
yang melakukan suatu permintaan data atau layanan ke server sedangkan server
ialah, sistem atau proses yang menyediakan data atau layanann yang di minta
oleh client.
Client-server adalah
pembagian kerja antara server dan client yang mengakses server dalam suatu
jaringan. Jadi arsitektur client-server adalah desain sebuah aplikasi yang
terdiri dari client dan server yang saling berkomunikasi ketika mengakses
server dalam suatu jaringan.
Di sini saya menggunakan NetBeans IDE 7.0.1 sebagai
compilernya. Pertama kita buat new project yang di dalamnya terdapat dua file,
file server dan client.
Berikut adalah source code untuk client:
package socket.client;
/**
*
* @author novhy
*/
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException
;
public class Client {
public static void
main(String[] args) {
Socket clientSocket = null;
DataInputStream is
= null;
PrintStream os =
null;
DataInputStream
inputLine = null;
/*
* socket pada port
2222, dan pembuka pada output stream dan inputstream.
*/
try {
clientSocket =
new Socket("localhost", 2222);
os = new
PrintStream(clientSocket.getOutputStream());
is = new
DataInputStream(clientSocket.getInputStream());
inputLine = new
DataInputStream(new BufferedInputStream(System.in));
} catch
(UnknownHostException e) {
System.err.println("Don't know about host");
} catch
(IOException e) {
System.err.println("Couldn't get I/O for the connection to
host");
}
/*
* socket membuka
conection pada port 2222
*/
if (clientSocket !=
null && os != null && is != null) {
try {
System.out.println("The client started. Type any text. To quit it
type 'Exit'.");
String
responseLine;
os.println(inputLine.readLine());
while
((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if
(responseLine.indexOf("Exit") != -1) {
break;
}
os.println(inputLine.readLine());
}
/*
* penutup pada
input stream, output stream dan socket.
*/
os.close();
is.close();
clientSocket.close();
} catch
(UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch
(IOException e) {
System.err.println("IOException:
" + e);
}
}
}
}
Dan berikut ini adalah source code untuk server
Sebelumnya
maaf nie y... kalo pencetakan world di cetak secara manual, jadi ketika clien
menginput kata hallo maka server hanya akan menjawab hallo juga, namu di sini
saya menambahkan pencetakan world sehingga jadilah hello world.
package socket.client;
/**
*
* @author novhy
*/
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
public class ServerClient {
public static void
main(String args[]) {
ServerSocket
echoServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket
= null;
try {
echoServer = new
ServerSocket(2222);
} catch
(IOException e) {
System.out.println(e);
}
System.out.println("The server started");
try {
clientSocket =
echoServer.accept();
is = new
DataInputStream(clientSocket.getInputStream());
os = new
PrintStream(clientSocket.getOutputStream());
while (true) {
line =
is.readLine();
os.println("Server : " + line+ " World");
}
} catch
(IOException e) {
System.out.println(e);
}
}
}
Tidak ada komentar:
Posting Komentar