Connection Oriented
Programming
Programming
Connection Oriented: Socket
•
Class Socket dan ServerSocket adalah abstraksi dari
standar TCP socket programming techniques.
•
Class socket menyediakan client-side socket interface
yang mirip dengan standard
yang mirip dengan standard
•
UNIX sockets.
–
Socket dapat menangani dua macam stream:
–
input stream
Socket
•
Adalah class yang digunakan untuk membuat
suatu hubungan ke mesin atau proses lain.
•
Socket dibuat untuk membuat aplikasi client
pada connection oriented.
pada connection oriented.
•
Setelah ada hubungan antar sistem, baru bisa
Constructor Socket
• Socket(InetAddress address, int port)
Membuat sebuah stream socket dan koneksi ke suatu nomor port pada sebuah komputer yang memiliki alamat IP.
• Socket(String host, int port)
Membuat sebuah stream socket dan juga koneksi ke suatu nomor port tertentu pada sebuah komputer berdasarkan namanya.
• Socket(InetAddress address, int port, InetAddress localAddr, int localPort) • Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
dan
• Socket(String host, int port, InetAddress localAaddr, int localPort)
Method Socket
• getInputStream dan getOutputStream
Keduanya mengembalikan suatu obyek stream yang dapat digunakan untuk berkomunikasi dengan socket.
• getInetAddress()
untuk mendapatkan nama host yang dituju dan alamat IP nya.
• getPort()
untuk mendapatkan nomor remote host. untuk mendapatkan nomor remote host.
• getLocalPort()
untuk mendapatkan nomor port localhost.
• getLocalAddress()
untuk mendapatkan alamat local di tempat socket digunakan.
ServerSocket
•
Menyatakan suatu koneksi TCP yang berfungsi untuk
listen yang siap menerima suatu permintaan dari
proses lain.
•
ServerSocket dipakai untuk membangun aplikasi
server yang bersifat connection oriented.
server yang bersifat connection oriented.
•
Setelah ada hubungan antar sistem, baru bisa
dilakukan pertukaran data melalui stream dengan
membuat input stream dan output stream dari
obyek socket yang dihasilkan ketika kelas
ServerSocket
•
ServerSocket()
Creates an unbound server socket.
•
ServerSocket(int port)
Creates a server socket on a specified port.
•
ServerSocket(int port, int backlog)
•
ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified local port
number, with the specified backlog.
Method ServerSocket
•
accept()
menghasilkan sebuah obyek kelas socket yang
terkoneksi dengan client.
•
close()
•
close()
Implementing a Client
•
Client adalah program yang meminta layanan.
•
Terdiri dari lima langkah:
1. Membuat obyek Socket.
2. Membuat output stream yang dapat digunakan untuk
mengirim informasi ke Socket.
mengirim informasi ke Socket.
3. Membuat input stream untuk membaca respon dari
server.
Implementing a Client
•
Most of the methods described throw an
1. Membuat obyek Socket
•
Socket adalah class Java yang digunakan untuk
membuat koneksi.
•
Client terhubung dengan server yang
menunggu koneksi pada port yang disediakan.
menunggu koneksi pada port yang disediakan.
•
Cara membuat socket :
Socket client = new Socket (“hostname”,portNumber);
or
2. Membuat output stream yang dapat digunakan
untuk mengirim informasi keSocket
•
Java programming language mampu menulis data ke
file, socket, dan standard output.
•
Java menyediakan berbagai macam class dan
interface untuk melakukan stream.
interface untuk melakukan stream.
•
Berbagai macam OutputStream yang tersedia untuk
file juga bisa digunakan untuk sockets.
•
Salah satu output stream yang bisa digunakan adalah
2. Membuat output stream yang dapat
digunakan untuk mengirim informasi ke
Socket
•
Konstruktor PrintWriter membutuhkan dua
argumen:
–
a generic OutputStream, which you can obtain from the
Socket by means of getOutputStream.
–
A Boolean, you should specify true to force autoflush.
–
A Boolean, you should specify true to force autoflush.
•
Example:
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
•
To send complex Java objects use
3. Membuat input stream untuk
membaca respon dari server.
•
Use standard input stream layered on top of
socket.
•
For handling character-based data, the
simplest is using InputStreamReader.
simplest is using InputStreamReader.
•
Example:
3. Membuat input stream untuk
membaca respon dari server.
•
In most cases, a better approach is to wrap the
socket’s generic InputStream inside a BufferedReader
•
Example:
BufferedReader in = BufferedReader in =
new BufferedReader(new InputStreamReader(client.getInputStream()));
•
If the server is sending complex object, you will want
4. Melakukan I/O dengan input dan
output streams
•
A
PrintStream
has
and
println
methods.
•
PrintStream
inherits some simple
write
methods from
OutputStream
.
•
write
method let you send binary data be
•
write
method let you send binary data be
5. Close the Socket when done
@
Close the socket with the close method:
client.close();
@
This method closes the associated input and
output streams
Implementing a Server
@ The server is the program that starts first and waits for incoming connections.
@ Implementing a server consists of six basic steps: 1. Create a ServerSocket object
2. Create a Socket object from the ServerSocket
3. Create an input stream to read input from the client. 4. Create an output stream that can be used to send 4. Create an output stream that can be used to send information back to the client.
5. Do I/O with input and output streams. 6. Close the Socket when done.