Java Network Programming

21
IADCS Diploma Course IADCS Diploma Course Java Network Programming Java Network Programming U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd

Transcript of Java Network Programming

Page 1: Java Network Programming

IADCS Diploma CourseIADCS Diploma CourseJava Network ProgrammingJava Network Programming

U Nyein OoCOO/Director(IT)Myanma Computer Co., Ltd

Page 2: Java Network Programming

Copyright: MCC Java Network Programming 2

Objectives

Introduction to Internet and its working Network Protocol TCP and UDP Explain Client/Server computing Classes of java.net package:

– InetAddress– Socket– ServerSocket– DatagramSocket– DatagramPacket

Page 3: Java Network Programming

Copyright: MCC Java Network Programming 3

Objectives(cont)

Explain Web related classes:– URL Class – URLConnection Class – URLEncoder Class – ContentHandler Class

Page 4: Java Network Programming

Copyright: MCC Java Network Programming 4

Networking and Internet

Networking is the concept of connecting two machines Internet is nothing but many computers

communicating with each other using protocols

Page 5: Java Network Programming

Copyright: MCC Java Network Programming 5

Protocol

Protocols help in passing information from one computer to another

Computers communicate with each other by exchanging packets of data

Internet Protocol (IP) is the network protocol used to send information from one computer to another over the Internet

Page 6: Java Network Programming

Copyright: MCC Java Network Programming 6

Protocol (Contd…)

The communication of computers is possible using Ports

Commonly used port numbers Port No Protocol Description

21 FTP File Transfer Protocol

23 TELNET Remote Login

25 SMTP Simple Mail Transfer Protocol

Page 7: Java Network Programming

Copyright: MCC Java Network Programming 7

TCP and UDP

Communication takes place with the help of transport protocols

TCP establishes link between the source port’s IP address and destination port’s IP address

Page 8: Java Network Programming

Copyright: MCC Java Network Programming 8

TCP and UDP (Contd…)

UDP is a connectionless protocol as it does not establish a link for the connection

When UDP is used, the destination port and IP addresses are written down in a datagram and the datagram is sent to the destination

UDP is less reliable as compared to TCP since there is no delivery assurance

Page 9: Java Network Programming

Copyright: MCC Java Network Programming 9

Client Server Computing

The Net provides a variety of services; these services are organized according to the client/server architecture

The client programs like browsers create connections to server programs such as Web and FTP servers

Page 10: Java Network Programming

Copyright: MCC Java Network Programming 10

Client Server Computing (Contd…)

The client makes the request to the server and the server responds by providing the required service to the client

Clients communicate through client sockets and servers communicate through server sockets

Page 11: Java Network Programming

Copyright: MCC Java Network Programming 11

Inside java.net

The java.net package provides the classes that support socket-based client/server communication

Classes of java.net package: – InetAddress– Socket– ServerSocket– DatagramSocket– DatagramPacket

Page 12: Java Network Programming

Copyright: MCC Java Network Programming 12

InetAddress Class

This class eases finding of addresses on the Internet

Only the name needs to be supplied to get the respective address

It supports both numeric IP address and hostnames

Page 13: Java Network Programming

Copyright: MCC Java Network Programming 13

Socket Class

Java programs connect to the network using a socket Socket helps in establishing connections and

developing applications between the client and server

Page 14: Java Network Programming

Copyright: MCC Java Network Programming 14

ServerSocket Class

The TCP server socket is implemented using the ServerSocket class

The server socket is made to listen using the accept( ) method and thereby wait till an incoming connection is established

The address of the host to which the socket is connected is returned using getInetAddress( ) method

Page 15: Java Network Programming

Copyright: MCC Java Network Programming 15

DatagramSocket Class

This class is used to implement the client and server sockets using the User Datagram Protocol (UDP)

UDP is a connectionless protocol that enables client and server to exchange information using datagrams

Datagrams are nothing but chunks of data The datagrams are sent and received using the

send( ) and receive( ) methods respectively

Page 16: Java Network Programming

Copyright: MCC Java Network Programming 16

DatagramPacket Class

This class encapsulates the datagrams that are sent and received using objects of DatagramSocket class

There are two different constructors, one for datagrams that are received from the datagram socket and the other for creating datagrams that are sent over the datagram socket

Page 17: Java Network Programming

Copyright: MCC Java Network Programming 17

Web related classes

While working with the Internet and World Wide Web, we need to create and manipulate URLs

The java.net package also provides a set of classes that support Web-specific applications 

java.net web-related classes:– URL– URLConnection– URLEncoder – ContentHandler

Page 18: Java Network Programming

Copyright: MCC Java Network Programming 18

URL Class

URL stands for Uniform Resource Locator It provides a uniform way to locate resources on the web The class java.net.URL encapsulates the object on the

web

Page 19: Java Network Programming

Copyright: MCC Java Network Programming 19

URLConnection Class

It is an abstract class If we have an active HTTP connection to the web, the

URLConnection class encapsulates it This class supports several methods so as to modify

the defaults, query and modify the current settings for a URLConnection object

URLConnection class is used for getting information about the web object and the connection to the web object so as to interact with the web object

HttpURLConnection is a subclass of the URLConnection class

Page 20: Java Network Programming

Copyright: MCC Java Network Programming 20

URLEncoder Class

This class is basically used for converting text strings to a suitable form useable as part of an URL

This class provides method called encode( ) that converts the spaces in the text to plus sign (+)

It uses a percent character (%) as an escape code for encoding special characters

Page 21: Java Network Programming

Copyright: MCC Java Network Programming 21

ContentHandler Class

ContentHandler class is used to process and extract data with MIME (Multipurpose Internet Mail Extension) types

MIME includes different types of objects such as sounds, images, videos, etc.

It provides viewers to process MIME types