NETWORK PROGRAMMING CNET 441 CHAPTER 2 Starting Network

  • Slides: 21
Download presentation
NETWORK PROGRAMMING CNET 441 CHAPTER -2 Starting Network Programming TCP Sockets 1

NETWORK PROGRAMMING CNET 441 CHAPTER -2 Starting Network Programming TCP Sockets 1

Chapter 2 : Objectives After Studying Chapter-2, the Student will be able to understand

Chapter 2 : Objectives After Studying Chapter-2, the Student will be able to understand the following concepts. • The Inet. Address class and Methods • Address Types • TCP Sockets – Server Side • Server. Socket class • Server Socket Creation Steps • TCP Sockets – Client Side • Sockets for Clients • Client Socket Creation Steps • Getting Information About a Socket • Socket Options 2

Inet. Address Class • Inet. Address Class is in the package java. net. •

Inet. Address Class • Inet. Address Class is in the package java. net. • It handles Internet addresses both as host names and as IP addresses. Methods of Inet. Address Class: 1. get. By. Name() 2. get. Local. Host() get. By. Name(): This method uses DNS (Domain Name System) to return the Internet address of a specified host name as an Inet. Address object. get. Local. Host(): This method is used to retrieve the IP address of the current machine. Program 4. 1 , Program 4. 2 – Please refer Text Book. 3

Getter Methods The Inet. Address class contains four getter methods that return the hostname

Getter Methods The Inet. Address class contains four getter methods that return the hostname as a string and the IP address as both a string and a byte array. Ø public String get. Host. Name() Ø public String get. Canonical. Host. Name() Ø public byte[] get. Address() Ø public String get. Host. Address() 4

Getter Methods (cont. . ) 1. The get. Host. Name() method returns a String

Getter Methods (cont. . ) 1. The get. Host. Name() method returns a String that contains the name of the host with the IP address represented by this Inet. Address object. If the machine doesn’t have a hostname, a dotted quad format of the numeric IP address is returned. 2. The get. Canonical. Host. Name() method calls DNS if it can, and may replace the existing cached hostname. This is particularly useful when you’re starting with a dotted quad IP address rather than the hostname. Program 4. 3 – Please refer Text Book 5

Getter Methods (cont. . ) 3. The get. Host. Address() method returns a string

Getter Methods (cont. . ) 3. The get. Host. Address() method returns a string containing the dotted quad format of the IP address. Program 4. 4 – Please refer Text Book. 4. The get. Address() method returns the IP address of a machine as an array of bytes in network byte order. Program 4. 5 – Please refer Text Book. 6

Address Types public boolean is. Any. Local. Address() - Wildcard Address public boolean is.

Address Types public boolean is. Any. Local. Address() - Wildcard Address public boolean is. Loopback. Address() - Loopback Address public boolean is. Link. Local. Address() - Link Local Address public boolean is. Site. Local. Address() - Site Local Address public boolean is. Multicast. Address() - Multicast Address public boolean is. MCGlobal() - Global Multicast Address public boolean is. MCNode. Local() - Interface Local Multicast Address 8. public boolean is. MCLink. Local() - Subnet Wide Multicast Address 9. public boolean is. MCSite. Local()- Site Wide Multicast Address 10. public boolean is. MCOrg. Local() - Organization Wide Multicast Address 1. 2. 3. 4. 5. 6. 7. 7

Basic life cycle of a server program 1. A new Server. Socket is created

Basic life cycle of a server program 1. A new Server. Socket is created on a particular port using a Server. Socket() constructor. 2. The Server. Socket listens for incoming connection attempts on that port using its accept() method. 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 type of server, either the Socket’s 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 according to an agreed-upon protocol until it is time to close the connection. 5. The server, the client, or both close the connection. 6. The server returns to step 2 and waits for the next connection. 8

Process Communicating through TCP Sockets 9

Process Communicating through TCP Sockets 9

Server. Socket (cont. . ) 10

Server. Socket (cont. . ) 10

Server. Socket class Create a Socket after accept() 11

Server. Socket class Create a Socket after accept() 11

Socket Based Client Server Communication 12

Socket Based Client Server Communication 12

Constructing Server Sockets There are four public Server. Socket constructors: 1. public Server. Socket(int

Constructing Server Sockets There are four public Server. Socket constructors: 1. public Server. Socket(int port) throws Bind. Exception, IOException 2. public Server. Socket(int port, int queue. Length) throws Bind. Exception, IOException 3. public Server. Socket(int port, int queue. Length, Inet. Address bind. Address) throws IOException 4. public Server. Socket() throws IOException 13

Steps to Create TCP Server Socket 1. Create a Server. Socket object. Example: Server.

Steps to Create TCP Server Socket 1. Create a Server. Socket object. Example: Server. Socket serv. Sock = new Server. Socket(1234); 2. Put the server into a waiting state. Example: Socket link = serv. Sock. accept(); 3. Set up input and output streams. Example: Buffered. Reader in = new Buffered. Reader( new Input. Stream. Reader(link. get. Input. Stream())); Example: Print. Writer out = new Print. Writer(link. get. Output. Stream(), true); 4. Send and receive data. Example: output. println("Awaiting data…"); String input = in. read. Line(); 5. Close the connection (after completion of the dialogue). Example: link. close(); Program 2. 3 – Please refer Text Book 14

TCP Client Socket The java. net. Socket class is Java’s fundamental class for performing

TCP Client Socket The java. net. Socket class is Java’s fundamental class for performing client-side TCP operations. Basic Constructors: Ø public Socket(String host, int port) throws Unknown. Host. Exception, IOException Ø public Socket(Inet. Address host, int port) throws IOException 15

Steps to Create TCP Client Socket 1. Establish a connection to the server. Example:

Steps to Create TCP Client Socket 1. Establish a connection to the server. Example: Socket link = new Socket(Inet. Address. get. Local. Host(), 1234); 2. Set up input and output streams. 3. Send and receive data. 4. Close the connection. Program for Client – Please refer Text Book 16

Client-Server Sockets in Java 17

Client-Server Sockets in Java 17

Getting Information about Server Sockets The Server. Socket class provides two getter methods that

Getting Information about Server Sockets The Server. Socket class provides two getter methods that tell you the local address and port occupied by the server socket. Ø public Inet. Address get. Inet. Address() This method returns the address being used by the server (the local host). Ø public int get. Local. Port() This method lets you find out what port you’re listening on. 18

Getting Information about Client Sockets The following methods are used to get socket information.

Getting Information about Client Sockets The following methods are used to get socket information. Ø public Inet. Address get. Inet. Address() Ø public int get. Port() Ø public Inet. Address get. Local. Address() Ø public int get. Local. Port() Program 8. 6 – Please refer Text Book 19

Socket Options - Server For server sockets, Java supports three options: 1. SO_TIMEOUT Ø

Socket Options - Server For server sockets, Java supports three options: 1. SO_TIMEOUT Ø public void set. So. Timeout(int timeout) throws Socket. Exception Ø public int get. So. Timeout() throws IOException 2. SO_REUSEADDR Ø public boolean get. Reuse. Address() throws Socket. Exception Ø public void set. Reuse. Address(boolean on) throws Socket. Exception 3. SO_RCVBUF Ø public int get. Receive. Buffer. Size() throws Socket. Exception Ø public void set. Receive. Buffer. Size(int size) throws Socket. Exception 20

Socket Options - Client Java supports nine options for client-side sockets: Ø TCP_NODELAY Ø

Socket Options - Client Java supports nine options for client-side sockets: Ø TCP_NODELAY Ø SO_BINDADDR Ø SO_TIMEOUT Ø SO_LINGER Ø SO_SNDBUF Ø SO_RCVBUF Ø SO_KEEPALIVE Ø OOBINLINE 21