Presentation is loading. Please wait.

Presentation is loading. Please wait.

NETWORK PROGRAMMING CNET 441

Similar presentations


Presentation on theme: "NETWORK PROGRAMMING CNET 441"— Presentation transcript:

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

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

3 InetAddress Class InetAddress Class is in the package java.net.
It handles Internet addresses both as host names and as IP addresses. Methods of InetAddress Class: getByName() getLocalHost() getByName(): This method uses DNS (Domain Name System) to return the Internet address of a specified host name as an InetAddress object. getLocalHost(): This method is used to retrieve the IP address of the current machine. Program 4.1 , Program 4.2 – Please refer Text Book.

4 Getter Methods The InetAddress 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 getHostName() public String getCanonicalHostName() public byte[] getAddress() public String getHostAddress()

5 Getter Methods (cont..) 1. The getHostName() method returns a String that contains the name of the host with the IP address represented by this InetAddress object. If the machine doesn’t have a hostname, a dotted quad format of the numeric IP address is returned. 2. The getCanonicalHostName() 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

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

7 Address Types public boolean isAnyLocalAddress() - Wildcard Address
public boolean isLoopbackAddress() - Loopback Address public boolean isLinkLocalAddress() - Link Local Address public boolean isSiteLocalAddress() - Site Local Address public boolean isMulticastAddress() - Multicast Address public boolean isMCGlobal() Global Multicast Address public boolean isMCNodeLocal() - Interface Local Multicast Address public boolean isMCLinkLocal() - Subnet Wide Multicast Address public boolean isMCSiteLocal()- Site Wide Multicast Address public boolean isMCOrgLocal() - Organization Wide Multicast Address

8 Basic life cycle of a server program
1. A new ServerSocket is created on a particular port using a ServerSocket() constructor. 2. The ServerSocket 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 getInputStream() method, getOutputStream() 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.

9 Process Communicating through TCP Sockets

10 ServerSocket (cont..)

11 ServerSocket class Create a Socket after accept()

12 Socket Based Client Server Communication

13 Constructing Server Sockets
There are four public ServerSocket constructors: 1. public ServerSocket(int port) throws BindException, IOException 2. public ServerSocket(int port, int queueLength) throws BindException, IOException 3. public ServerSocket(int port, int queueLength, InetAddress bindAddress) throws IOException 4. public ServerSocket() throws IOException

14 Steps to Create TCP Server Socket
1. Create a ServerSocket object. Example: ServerSocket servSock = new ServerSocket(1234); 2. Put the server into a waiting state. Example: Socket link = servSock.accept(); 3. Set up input and output streams. Example: BufferedReader in = new BufferedReader( new InputStreamReader(link.getInputStream())); Example: PrintWriter out = new PrintWriter(link.getOutputStream(),true); 4. Send and receive data. Example: output.println("Awaiting data…"); String input = in.readLine(); 5. Close the connection (after completion of the dialogue). Example: link.close(); Program 2.3 – Please refer Text Book

15 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 UnknownHostException, IOException public Socket(InetAddress host, int port) throws IOException

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

17 Client-Server Sockets in Java

18 Getting Information about Server Sockets
The ServerSocket class provides two getter methods that tell you the local address and port occupied by the server socket. public InetAddress getInetAddress() This method returns the address being used by the server (the local host). public int getLocalPort() This method lets you find out what port you’re listening on.

19 Getting Information about Client Sockets
The following methods are used to get socket information. public InetAddress getInetAddress() public int getPort() public InetAddress getLocalAddress() public int getLocalPort() Program 8.6 – Please refer Text Book

20 Socket Options - Server
For server sockets, Java supports three options: SO_TIMEOUT public void setSoTimeout(int timeout) throws SocketException public int getSoTimeout() throws IOException SO_REUSEADDR public boolean getReuseAddress() throws SocketException public void setReuseAddress(boolean on) throws SocketException SO_RCVBUF public int getReceiveBufferSize() throws SocketException public void setReceiveBufferSize(int size) throws SocketException

21 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 IP_TOS


Download ppt "NETWORK PROGRAMMING CNET 441"

Similar presentations


Ads by Google