NETWORK PROGRAMMING CNET 441

Slides:



Advertisements
Similar presentations
Sockets For Servers Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Advertisements

Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
Prepared By E.Musa Alyaman1 Chapter 3 Internet Addressing.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
COMP1681 / SE15 Introduction to Programming
Client/Server In Java An Introduction to TCP/IP and Sockets.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
Multicast Sockets What is a multicast socket?
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Network Programming Introduction
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Java Network Programming
CSCD 330 Network Programming
Sockets and URLs 13-Nov-18.
Networking.
Sockets and URLs 3-Dec-18.
CSCD 330 Network Programming
NETWORK PROGRAMMING CNET 441
Java Socket Programming
Programming TCP Sockets
Programming TCP Clients
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

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

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

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

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

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

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.

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

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.

Process Communicating through TCP Sockets

ServerSocket (cont..)

ServerSocket class Create a Socket after accept()

Socket Based Client Server Communication

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

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

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

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

Client-Server Sockets in Java

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.

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

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

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