Networking CSI 3125 Preliminaries page 1 Inetaddress Class

  • Slides: 41
Download presentation
Networking CSI 3125, Preliminaries, page 1

Networking CSI 3125, Preliminaries, page 1

Inetaddress Class • When establishing a connection across the Internet, addresses are fundamental. •

Inetaddress Class • When establishing a connection across the Internet, addresses are fundamental. • The java. net. Inet. Address class used to identify an IP address. • It is used by most of the other networking classes, including Socket, Server. Socket, URL, Datagram. Socket, Datagram. Packet. • This class represents an Internet address as two fields: host. Name (a String) and address (an int). • There are no public constructors in the Inet. Address class. CSI 3125, Preliminaries, page 2

Inetaddress Class methods • HInet. Address has three static methods that return suitably initialized

Inetaddress Class methods • HInet. Address has three static methods that return suitably initialized Inet. Address objects. • static Inet. Address get. Local. Host( ) throws Unknown. Host. Exception • returns the Inet. Address of the machine on which it's running. • Eg: • Inet. Address address = Inet. Address. get. Local. Host(); • System. out. println(address); CSI 3125, Preliminaries, page 3

Inetaddress Class methods • static Inet. Address get. By. Name(String host. Name) throws Unknown.

Inetaddress Class methods • static Inet. Address get. By. Name(String host. Name) throws Unknown. Host. Exception • returns an Inet. Address for a host name passed to it and it throws an Unknown. Host. Exception if the host can't be found • Eg: • address = Inet. Address. get. By. Name("amaljyothi. ac. in"); • System. out. println(address); CSI 3125, Preliminaries, page 4

Inetaddress Class methods • static Inet. Address[] get. All. By. Name(String host. Name) throw

Inetaddress Class methods • static Inet. Address[] get. All. By. Name(String host. Name) throw Unknown. Host. Exception • Some computers have more than one Internet address. • Given a hostname, this method returns an Inet. Address array that contains all the addresses corresponding to that name. • Eg • • Inet. Address add[] = Inet. Address. get. All. By. Name("google. com"); System. out. println(Inet. Address. get. By. Name("google. com")); for (int i=0; i<add. length; i++) System. out. println(add[i]); CSI 3125, Preliminaries, page 5

Inetaddress Class prog • • • • import java. net. *; class Inet {

Inetaddress Class prog • • • • import java. net. *; class Inet { public static void main(String args[]) throws Unknown. Host. Exception { Inet. Address address = Inet. Address. get. Local. Host(); System. out. println(address); address = Inet. Address. get. By. Name("amaljyothi. ac. in"); System. out. println(address); Inet. Address add[] = Inet. Address. get. All. By. Name("google. com"); System. out. println(Inet. Address. get. By. Name("google. com")); for (int i=0; i<add. length; i++) System. out. println(add[i]); } } CSI 3125, Preliminaries, page 6

TCP & UDP protocols • The java. net package provides support two common network

TCP & UDP protocols • The java. net package provides support two common network protocols: • TCP: TCP, Transmission Control Protocol is a connection oriented protocol which allows for reliable communication between two applications. • It guarantees that data sent from one end of the connection actually gets to the other end and in the same order it was sent. • Otherwise, an error is reported • Example telephone call. CSI 3125, Preliminaries, page 7

TCP & UDP protocols • UDP: UDP, User Datagram Protocol, is a connection-less protocol

TCP & UDP protocols • UDP: UDP, User Datagram Protocol, is a connection-less protocol that allows for packets of data to be transmitted between applications. • The destination port and IP addresses are written down in a datagram and the datagram is sent to the destination. • UDP does not guarantee that packets will be received in the order they were sent CSI 3125, Preliminaries, page 8

TCP & UDP protocols • The two computers can communicate until the connection is

TCP & UDP protocols • The two computers can communicate until the connection is closed or lost. • The java. net. Server. Socket and java. net. Socket classes are need to create a TCP/IP connection between two computers • The java. net. Socket class represents a socket, and • the java. net. Server. Socket class provides a mechanism for the server program to listen for clients and establish connections with them. CSI 3125, Preliminaries, page 9

TCP & UDP protocols CSI 3125, Preliminaries, page 10

TCP & UDP protocols CSI 3125, Preliminaries, page 10

TCP & UDP protocols • Sockets provide the communication mechanism between two computers using

TCP & UDP protocols • Sockets provide the communication mechanism between two computers using TCP. • A client program creates a socket on its end of the communication and attempts to connect that socket to a server. • When the connection is made, the server creates a socket object on its end of the communication. • The client and server can now communicate by writing to and reading from the socket. CSI 3125, Preliminaries, page 11

TCP & UDP protocols • The reading and writing of data over the socket

TCP & UDP protocols • The reading and writing of data over the socket is accomplished via the familiar stream classes. • Each socket has both an Output. Stream and an Input. Stream. • The client’s Output. Stream is connected to the server’s Input. Stream, and the client’s Input. Stream is connected to the server’s Output. Stream. • TCP is a two way communication protocol, so data can be sent across both streams at the same time. CSI 3125, Preliminaries, page 12

Socket Class • The java. net. Socket class is Java's fundamental class for performing

Socket Class • The java. net. Socket class is Java's fundamental class for performing client-side TCP operations. • It contains constructor that is used to create stream socket that connects to specific port number associated with exactly one host. • Following are two most commonly used constructors of Socket class. • Socket(String host. Name, int port) • Creates a socket that connects to the given port number on the specified host. • If the domain name server cannot resolve the hostname or is not functioning, the constructor throws an Unknown. Host. Exception. • If the socket cannot be opened for some other reason, the constructor • throws an IOException CSI 3125, Preliminaries, page 13

Socket Class • Socket(Inet. Address ip. Address, int port) • Creates a socket that

Socket Class • Socket(Inet. Address ip. Address, int port) • Creates a socket that connects to the given port number at the specified IP address. • It uses an Inet. Address object to specify the host rather than a hostname. • It throws an IOException if it can't connect CSI 3125, Preliminaries, page 14

Socket Class Methods • Inet. Address get. Inet. Address() • tells which remote host

Socket Class Methods • Inet. Address get. Inet. Address() • tells which remote host the Socket is connected to • int get. Port() - tells you which port the Socket is connected to on the remote host. • Input. Stream get. Input. Stream()-returns an input stream that can read data from the socket into a program. • It can be chained with Buffered. Input. Stream or a Buffered. Reader CSI 3125, Preliminaries, page 15

Socket Class Methods • Output. Stream get. Output. Stream()-returns a raw Output. Stream for

Socket Class Methods • Output. Stream get. Output. Stream()-returns a raw Output. Stream for writing data from application to the other end of the socket. • It can be chained with Data. Output. Stream or Output. Stream. Writer • public void close()- Closes the socket, CSI 3125, Preliminaries, page 16

Implementing A Client • 1. Creates a new socket with a Socket() constructor •

Implementing A Client • 1. Creates a new socket with a Socket() constructor • 2. The socket attempts to connect to the remote host. • 3. Once the connection is established, the local and remote hosts get input and output streams from the socket. • 4. When the transmission of data is complete, one or both sides close the connection. CSI 3125, Preliminaries, page 17

SERVERSOCKET Class • To establish a server, create a server socket and attach it

SERVERSOCKET Class • To establish a server, create a server socket and attach it to a port, which is where the server listens for connections. • The java. net. Server. Socket class is used by server applications to obtain a port and listen for client requests. • constructors of this class • public Server. Socket (int port)- creates a server socket on the port specified by the argument. The constructor throws an IOException CSI 3125, Preliminaries, page 18

SERVERSOCKET Class method • public int get. Local. Port()-Returns the port that the server

SERVERSOCKET Class method • public int get. Local. Port()-Returns the port that the server socket is listening on • public Socket accept() - returns a Socket object representing the connection between the remote client and the local server CSI 3125, Preliminaries, page 19

Implementing A Server • 1. A new Server. Socket is created on a particular

Implementing A Server • 1. A new Server. Socket is created on a particular port using a Server. Socket() constructor. • 2. The server invokes the accept() method of the Server. Socket class. • accept() blocks until a client attempts to make a connection, at which point accept() returns a Socket object connecting the client and the server. • 3. Depending on the requirement, either get. Input. Stream() method, get. Output. Stream() method, or both are called to get input and output streams that communicate with the client. • 4. The server and the client interact each other. • 5. The server, the client, or both close the connection. • 6. The server returns to step 2 and waits for the next connection. CSI 3125, Preliminaries, page 20

Client Server interaction CSI 3125, Preliminaries, page 21

Client Server interaction CSI 3125, Preliminaries, page 21

Client Prog • • • • • import java. io. *; import java. net.

Client Prog • • • • • import java. io. *; import java. net. *; class Client { public static void main(String args[]) throws IOException, Unknown. Host. Exception { Socket s=new Socket("localhost", 5555); Buffered. Reader br=new Buffered. Reader(new Input. Stream. Reader(System. in)); String line; System. out. println("Enter characters"); do { line=br. read. Line(); Print. Writer pw=new Print. Writer(new Output. Stream. Writer(s. get. Output. Stream()), true); pw. println(line); } while(!line. equals("EXIT")); s. close(); } } CSI 3125, Preliminaries, page 22

 • • • • • • • • Server Prog import java. net.

• • • • • • • • Server Prog import java. net. *; import java. io. *; class Server { Server. Socket ss; Socket s; Buffered. Reader br; String str; public Server() { try { ss=new Server. Socket(5555); s=ss. accept(); br=new Buffered. Reader(new Input. Stream. Reader(s. get. Input. Stream())); System. out. println("CONNECTED"); do { str=br. read. Line(); System. out. println(str); } while(!str. equals("EXIT")); ss. close(); } catch(Exception e) { } } public static void main(String args[]) { Server s = new Server(); } } CSI 3125, Preliminaries, page 23

User Datagram Protocol • User Datagram Protocol (UDP) provides a protocol for sending packets

User Datagram Protocol • User Datagram Protocol (UDP) provides a protocol for sending packets of data called datagrams between applications. • If TCP is similar to placing a telephone call, UDP can be compared to mailing someone a letter. • The datagram packet is like a letter, where a client sends a datagram to a server without actually connecting to the server. • This makes UDP an unreliable communication protocol when compared to TCP, where the client and server are directly connected. CSI 3125, Preliminaries, page 24

User Datagram Protocol • If I mail you two letters on the same day,

User Datagram Protocol • If I mail you two letters on the same day, they might be delivered on the same day. • In fact, there is no guarantee that both letters will even get delivered at all. • The same is true for datagram packets. • UDP does not guarantee that packets will be received in the order they were sent. • Datagram packets are used to implement a connectionless packet delivery protocol. • Each packet needs to have destination address and each packet might be routed differently, and might arrive in any order. CSI 3125, Preliminaries, page 25

User Datagram Protocol • The java. net. Datagram. Packet and java. net. Datagram. Socket

User Datagram Protocol • The java. net. Datagram. Packet and java. net. Datagram. Socket classes are used to send and receive datagram packets. • The Datagram. Packet class stuffs bytes of data into UDP packets called datagrams and lets you unstuff datagrams that you receive. • A Datagram. Socket sends as well as receives UDP datagrams. • To send data, you put the data in a Datagram. Packet and send the packet using a Datagram. Socket. • To receive data, you receive a Datagram. Packet object from a Datagram. Socket and then read the contents of the packet. • In UDP, everything about a datagram, including the address to which it is directed, is included in the packet itself; the socket needs to know only the local port on which to listen or send. CSI 3125, Preliminaries, page 26

User Datagram Protocol • All the data you stuff into a single datagram is

User Datagram Protocol • All the data you stuff into a single datagram is sent as a single packet and is either received or lost as a group. • One packet is not necessarily related to the next. • Given two packets, there is no way to determine which packet was sent first and which was sent second CSI 3125, Preliminaries, page 27

Datagramsocket Class • The java. net. Datagram. Socket class is used by both the

Datagramsocket Class • The java. net. Datagram. Socket class is used by both the sender and the recipient of a datagram packet to send and receive a packet, respectively. • constructors for Datagram. Socket: • Datagram. Socket(int port) throws Socket. Exception -Creates a datagram socket on the localhost computer at the specified port. • Datagram. Socket(int port, Inet. Address address) throws Socket. Exception) • Creates a datagram socket using the specified port and local address. CSI 3125, Preliminaries, page 28

Datagramsocket Class • METHODS • public void send(Datagram. Packet packet) throws IOException • Sends

Datagramsocket Class • METHODS • public void send(Datagram. Packet packet) throws IOException • Sends the specified datagram packet. • The Datagram. Packet object contains the destination information of the packet. • public void receive(Datagram. Packet packet) throws IOException • This method receives a single UDP datagram from the network and stores it in the pre-existing Datagram. Packet object. CSI 3125, Preliminaries, page 29

Datagrampacket Class • In Java, a UDP datagram is represented by an instance of

Datagrampacket Class • In Java, a UDP datagram is represented by an instance of the Datagram. Packet. • This class provides methods to get and set the source or destination address from the IP header, to get and set the source or destination port, to get and set the data, and to get and set the length of the data. • There are two types of constructors for Datagram. Packet. • The first constructor is used to receive data from the net; • The second is for data that you will send to the net. CSI 3125, Preliminaries, page 30

Datagrampacket Class • public Datagram. Packet(byte[] buffer, int length) • Creates a datagram packet

Datagrampacket Class • public Datagram. Packet(byte[] buffer, int length) • Creates a datagram packet for receiving a packet of the specified size. • The buffer will contain the incoming packet. • The array of bytes passed in to these constructors is used to contain the data of the incoming packet, and typically are empty arrays CSI 3125, Preliminaries, page 31

Datagrampacket Class • public Datagram. Packet(byte[] buffer, int length, Inet. Address address, int port)

Datagrampacket Class • public Datagram. Packet(byte[] buffer, int length, Inet. Address address, int port) • Creates a datagram packet for sending a packet of the specified size. • The buffer contains the data of the packet, and the address and port denote the recipient. CSI 3125, Preliminaries, page 32

Datagrampacket Class • METHODS • Datagram. Packet has five methods • Inet. Address get.

Datagrampacket Class • METHODS • Datagram. Packet has five methods • Inet. Address get. Address()-The get. Address() method returns an Inet. Address object containing the address of the remote host. • int get. Port( ) -The get. Port() method returns an integer specifying the remote port. CSI 3125, Preliminaries, page 33

Receiving A Datagram Packet • 1. Create an array of bytes large enough to

Receiving A Datagram Packet • 1. Create an array of bytes large enough to hold the data of the incoming packet. • 2. Create object of Datagram. Packet using the array of bytes. • 3. A Datagram. Socket is instantiated, and it is specified which port. • 4. The receive() method of the Datagram. Socket class is invoked, passing in the Datagram. Packet object. • This blocks the execution of further code until a datagram packet is received or a time out occurs CSI 3125, Preliminaries, page 34

Sending A Datagram Packet • 1. Create an array of bytes large enough to

Sending A Datagram Packet • 1. Create an array of bytes large enough to hold the data of the packet to be sent, and fill the array with the data. • 2. Create a new Datagram. Packet object that contains the array of bytes, as well as the server name and port number of the recipient. • 3. A Datagram. Socket is instantiated, and it is specified which port on the localhost the socket will bind to. • 4. The send() method of the Datagram. Socket class is invoked, passing in the Datagram. Packet object. CSI 3125, Preliminaries, page 35

UDP Client • • • • • • import java. io. *; import java.

UDP Client • • • • • • import java. io. *; import java. net. *; class UDPClient { public static void main(String args[])throws Exception { Buffered. Reader in. From. User =new Buffered. Reader(new Input. Stream. Reader(System. in)); Datagram. Socket client. Socket = new Datagram. Socket(); Inet. Address IPAddress=Inet. Address. get. By. Name("localhost"); byte[] send. Data = new byte[1024]; byte[] receive. Data = new byte[1024]; String sentence = in. From. User. read. Line(); send. Data = sentence. get. Bytes(); Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, 9876); client. Socket. send(send. Packet); Datagram. Packet receive. Packet =new Datagram. Packet(receive. Data, receive. Data. length); client. Socket. receive(receive. Packet); String modified. Sentence =new String(receive. Packet. get. Data()); System. out. println("Ffrom server: " + modified. Sentence); client. Socket. close(); } } CSI 3125, Preliminaries, page 36

UDP Server • • • • • • import java. io. *; import java.

UDP Server • • • • • • import java. io. *; import java. net. *; class UDPServer { public static void main(String args[]) throws Exception { Datagram. Socket server. Socket = new Datagram. Socket(9876); byte[] receive. Data= new byte[1024]; byte[] send. Data= new byte[1024]; while(true) { Datagram. Packet receive. Packet =new Datagram. Packet(receive. Data, receive. Data. length); server. Socket. receive(receive. Packet); String sentence = new String(receive. Packet. get. Data()); Inet. Address IPAddress = receive. Packet. get. Address(); int port = receive. Packet. get. Port(); String capitalized. Sentence = sentence. to. Upper. Case(); send. Data = capitalized. Sentence. get. Bytes(); Datagram. Packet send. Packet =new Datagram. Packet(send. Data, send. Data. length, IPAddress, port); server. Socket. send(send. Packet); } } } CSI 3125, Preliminaries, page 37

URL CLASS • URL stands for Uniform Resource Locator, represents a resource on the

URL CLASS • URL stands for Uniform Resource Locator, represents a resource on the World Wide Web, such as a Web page or FTP directory. • The java. net. URL class represents a URL. • Constructor • URL(String url)- Creates a URL from the given String. • Eg • URL url = new URL("https: //poposir. orgfree. com"); CSI 3125, Preliminaries, page 38

URL CLASS methods • public String get. Path()-Returns the path of the URL. •

URL CLASS methods • public String get. Path()-Returns the path of the URL. • public String get. Query()-Returns the query part of the URL. • public String get. Authority()-Returns the authority of the URL. • public int get. Port()-Returns the port of the URL. • public int get. Default. Port()-Returns the default port for the protocol ofthe URL. • public String get. Protocol()-Returns the protocol of the URL. • public String get. Host()-Returns the host of the URL. • public String get. File()-Returns the filename of the URL. • public String get. Ref()-Returns the reference part of the URL. CSI 3125, Preliminaries, page 39

URL CLASS prog • • • • • import java. net. *; import java.

URL CLASS prog • • • • • import java. net. *; import java. io. *; public class URLDemo { public static void main(String [] args) throws Exception { URL url = new URL("https: //poposir. orgfree. com/index. php? user=popo#inbox"); System. out. println("URL is " + url. to. String()); System. out. println("protocol is " + url. get. Protocol()); System. out. println("authority is " + url. get. Authority()); System. out. println("file name is " + url. get. File()); System. out. println("host is " + url. get. Host()); System. out. println("path is " + url. get. Path()); System. out. println("port is " + url. get. Port()); System. out. println("default port is " + url. get. Default. Port()); System. out. println("query is " + url. get. Query()); System. out. println("ref is " + url. get. Ref()); } } CSI 3125, Preliminaries, page 40

URL CLASS CSI 3125, Preliminaries, page 41

URL CLASS CSI 3125, Preliminaries, page 41