network socket programming with java

28
Q7M1 – SC Dudy Fathan Ali S.Kom Network Socket Programming Q7M1 Dudy Fathan Ali, S.Kom (DFA) 2017 CEP - CCIT Fakultas Teknik Universitas Indonesia

Upload: dudy-ali

Post on 12-Apr-2017

54 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Network Socket Programming with JAVA

Q7M1 – SC Dudy Fathan Ali S.Kom

Network Socket ProgrammingQ7M1

Dudy Fathan Ali, S.Kom (DFA)2017

CEP - CCITFakultas Teknik Universitas Indonesia

Page 2: Network Socket Programming with JAVA

Consider this scenario..

Q7M1 – SC Dudy Fathan Ali S.Kom

Hello, I want a pizza.

Guys, bob wants a pizza!

Bob

Pizza Store

Page 3: Network Socket Programming with JAVA

Consider this scenario..

Q7M1 – SC Dudy Fathan Ali S.Kom

Pizza Store

Standard Operational Procedure

Page 4: Network Socket Programming with JAVA

Hmm..

Q7M1 – SC Dudy Fathan Ali S.Kom

Page 5: Network Socket Programming with JAVA

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.

Page 6: Network Socket Programming with JAVA

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.

Page 7: Network Socket Programming with JAVA

Client/Server Architecture

Q7M1 – SC Dudy Fathan Ali S.Kom

Steve

Bob

John

One server can service multiple clients.

Page 8: Network Socket Programming with JAVA

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.

Page 9: Network Socket Programming with JAVA

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

Page 10: Network Socket Programming with JAVA

Internet Protocol

Q7M1 – SC Dudy Fathan Ali S.Kom

implicit rules in conversation?

Page 11: Network Socket Programming with JAVA

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

Page 12: Network Socket Programming with JAVA

IP Address & Port

Q7M1 – SC Dudy Fathan Ali S.Kom

Page 13: Network Socket Programming with JAVA

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

Page 14: Network Socket Programming with JAVA

IP Address & Port

Q7M1 – SC Dudy Fathan Ali S.Kom

IP Addresses of Popular Websites

Page 15: Network Socket Programming with JAVA

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

Page 16: Network Socket Programming with JAVA

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.

Page 17: Network Socket Programming with JAVA

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.

Page 18: Network Socket Programming with JAVA

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)

Page 19: Network Socket Programming with JAVA

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()

Page 20: Network Socket Programming with JAVA

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.

Page 21: Network Socket Programming with JAVA

TCP vs UDP

Q7M1 – SC Dudy Fathan Ali S.Kom

Page 22: Network Socket Programming with JAVA

TCP vs UDP

Q7M1 – SC Dudy Fathan Ali S.Kom

Page 23: Network Socket Programming with JAVA

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)

Page 24: Network Socket Programming with JAVA

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)

Page 25: Network Socket Programming with JAVA

Creating Application Using UDP

Q7M1 – SC Dudy Fathan Ali S.Kom

Code Example [DataSender]:

Page 26: Network Socket Programming with JAVA

Creating Application Using UDP

Q7M1 – SC Dudy Fathan Ali S.Kom

Code Example [DataReceiver]:

Page 27: Network Socket Programming with JAVA

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.

Page 28: Network Socket Programming with JAVA

Q7M1 – SC Dudy Fathan Ali S.Kom

Thank You!Dudy Fathan Ali, S.Kom

[email protected]