network socket programming with java
TRANSCRIPT
Q7M1 – SC Dudy Fathan Ali S.Kom
Network Socket ProgrammingQ7M1
Dudy Fathan Ali, S.Kom (DFA)2017
CEP - CCITFakultas Teknik Universitas Indonesia
Consider this scenario..
Q7M1 – SC Dudy Fathan Ali S.Kom
Hello, I want a pizza.
Guys, bob wants a pizza!
Bob
Pizza Store
Consider this scenario..
Q7M1 – SC Dudy Fathan Ali S.Kom
Pizza Store
Standard Operational Procedure
Hmm..
Q7M1 – SC Dudy Fathan Ali S.Kom
Client/Server Architecture
Q7M1 – SC Dudy Fathan Ali S.Kom
Bob
[Client]Pizza Store
[Server]
The client places a request or orderto the server.
The server processes the request of the client
The communicationbetween the client and the server is usually through a network.
The Client/Server model is an application development architecture designed to separate the presentation of data from its internal processing and storage.
Client/Server Architecture
Q7M1 – SC Dudy Fathan Ali S.Kom
Pizza Store
The processing that is done by the server is hidden from the client.
Client/Server Architecture
Q7M1 – SC Dudy Fathan Ali S.Kom
Steve
Bob
John
One server can service multiple clients.
Client/Server Architecture
Q7M1 – SC Dudy Fathan Ali S.Kom
The server and the client are not necessarily hardware components. They can be programs working on the same machine or on different machines.
Internet Protocol
Q7M1 – SC Dudy Fathan Ali S.Kom
The Internet Protocol (IP) is the principal communications protocol in the Internet protocol suite for relaying datagrams across network boundaries. Its routing function enables internetworking, and essentially establishes the Internet.
-- Source: wikipedia.org
-- Source: google.com
Internet Protocol
Q7M1 – SC Dudy Fathan Ali S.Kom
implicit rules in conversation?
Internet Protocol
Q7M1 – SC Dudy Fathan Ali S.Kom
o Data is sent from one machine to another in form of packets. o Rules govern packaging of data into packets, speed of transmission, and
reassembling of data into its original form.o These rules are called network protocols.
Communication rules between computers
IP Address & Port
Q7M1 – SC Dudy Fathan Ali S.Kom
IP Address & Port
Q7M1 – SC Dudy Fathan Ali S.Kom
The TCP protocol requires two data items: the IP address and the port number.
172.17.8.192 : 80IP Address Port
o Well-known portso Registered portso Dynamic/Private ports
IP Address & Port
Q7M1 – SC Dudy Fathan Ali S.Kom
IP Addresses of Popular Websites
Sockets
Q7M1 – SC Dudy Fathan Ali S.Kom
Sockets are used to handle the communication links between applications over the network. Further communication between the client and the server is through the socket.
-- Source: NIIT Courseware Q7M1
Java Classes for Network Programming
Q7M1 – SC Dudy Fathan Ali S.Kom
o The java.net package of the Java programming language contains classes and interfaces that provide support for networking.
o Networking classes contain methods to perform tasks, such as opening and closing a connection to remote machine, sending and receiving data packets and accessing resources on the Web.
Java Classes for Network Programming
Q7M1 – SC Dudy Fathan Ali S.Kom
Classes of the java.net Package:o DatagramPacket
o Represents a datagram packet. o DatagramSocket
o Represents a datagram socket object that can send and receive datagram packets.
o MulticastSocketo Creates a multicast datagram socket object that is used to send and receive
datagram packets to groups.o InetAddress
o Represents an IP address.o ServerSocket
o Represents a TCP/IP server socket object that receives connection requests from the clients.
Java Classes for Network Programming
Q7M1 – SC Dudy Fathan Ali S.Kom
The InetAddress Class:o Represents an IP address.o Contains the following static methods to initialize InetAddress
objects:o public static InetAddress getLocalHost()o public static InetAddress getByName(String host)o public static InetAddress[] getAllByName(String host)
Java Classes for Network Programming
Q7M1 – SC Dudy Fathan Ali S.Kom
The non-static methods of the InetAddress class are:o public boolean equals(Object obj)o public byte[] getAddress()o public String getHostAddress()o public String toString()
Java Classes for Network Programming
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example:
Result:
Returns an InetAddress object that contains the IP address of the local computer.
Returns the IP address of the InetAddressobject as a String.
TCP vs UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
TCP vs UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
Creating Application Using UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
o UDP is a fast, connectionless, and unreliable protocol.o UDP sockets use UDP protocol for communication over a network.o The java.net package provides the following two classes that
enable you to implement UDP sockets in a Java application:o DatagramPacket classo DatagramSocket class
o The DatagramPacket object is a data container that consists of datagram packets that are sent or received over the network.
o The constructor used to initialize DatagramPacket objects are:o public DatagramPacket( byte[] buffer, int
buffer_length)o public DatagramPacket( byte[] buffer, int
buffer_length, InetAddress address, int port)
Creating Application Using UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
o DatagramSocket class encapsulates the functionality to handle DatagramPacket objects.
o The constructors used to initialize DatagramSocket object are:o public DatagramSocket()o public DatagramSocket(int port)o public DatagramSocket(int port, InetAddress address)
Creating Application Using UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example [DataSender]:
Creating Application Using UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example [DataReceiver]:
Demonstration-Implementing UDP
Q7M1 – SC Dudy Fathan Ali S.Kom
Create a Client/Server application where the server can broadcast messages to all the clients of 233.0.0.1 group.
Q7M1 – SC Dudy Fathan Ali S.Kom
Thank You!Dudy Fathan Ali, S.Kom