T 07 Connection Oriented

Connection Oriented
Programming
Kholid F.

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
• UNIX sockets.
– Socket dapat menangani dua macam stream:
– input stream
– output stream.

Socket
• Adalah class yang digunakan untuk membuat
suatu hubungan ke mesin atau proses lain.
• Socket dibuat untuk membuat aplikasi client
pada connection oriented.
• Setelah ada hubungan antar sistem, baru bisa
dilakukan pertukaran data melalui stream

dengan membuat input stream dan output
stream dari obyek socket.

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)
dan
• Socket(String host, int port, InetAddress localAaddr, int localPort)
Membuat sebuah socket yang mengkoneksikannya ke nomor port yang
dituju pada alamat IP yang disebutkan pada parameter address atau nama
host. Selain itu juga dilakukan bind socket ke alamat lokal dan port lokal.
(Hal ini dilakukan jika koneksi antara client dan server membutuhkan
nomor port yang sudah ditentukan).

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.
• getLocalPort()
untuk mendapatkan nomor port localhost.
• getLocalAddress()
untuk mendapatkan alamat local di tempat socket digunakan.
• dll

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.
• 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 menerima permintaan dari client
melalui method accept().

ServerSocket
• ServerSocket()
Creates an unbound server socket.
• ServerSocket(int port)
Creates a server socket on a specified port.
• ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified local port
number, with the specified backlog.
• ServerSocket(int port, int backlog, InetAddress bindAddr)
Create a server with the specified port, listen backlog, and
local IP address to bind to

Method ServerSocket
• accept()

menghasilkan sebuah obyek kelas socket yang
terkoneksi dengan client.
• close()
menutup sesi listen client.

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.
3. Membuat input stream untuk membaca respon dari
server.
4. Melakukan I/O dengan input dan output streams.
5. Menutup koneksi Socket .

Implementing a Client
• Most of the methods described throw an
IOException and need to be wrapped in a
try/catch block.


1. Membuat obyek Socket
• Socket adalah class Java yang digunakan untuk
membuat koneksi.
• Client terhubung dengan server yang
menunggu koneksi pada port yang disediakan.
• Cara membuat socket :
Socket client = new Socket (“hostname”,portNumber);

or
Socket client = new Socket (“IP address”,portNumber);

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.
• Berbagai macam OutputStream yang tersedia untuk
file juga bisa digunakan untuk sockets.

• Salah satu output stream yang bisa digunakan adalah
PrintWriter.

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.

• Example:
PrintWriter out = new PrintWriter(client.getOutputStream(),true);

• To send complex Java objects use
ObjectOutputStream.

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.
• Example:
InputStreamReader in = new InputStreamReader(client.getInputStream());

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 =
new BufferedReader(new InputStreamReader(client.getInputStream()));

• If the server is sending complex object, you will want
to open an ObjectInputStream and use readObject to
receive data


4. Melakukan I/O dengan input dan
output streams
• A PrintStream has print and println methods.
• PrintStream inherits some simple write
methods from OutputStream.
• write method let you send binary data be
sending an individual byte or an array of
bytes.

@

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
information back to the client.
5. Do I/O with input and output streams.
6. Close the Socket when done.
Note: most of the methods described throw an IOException,
so they need to be wrapped inside a try/catch block in an
actual implementation.