Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.

Slides:



Advertisements
Similar presentations
Network Programming Chapter 11 Lecture 6. Networks.
Advertisements

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.
Socket Programming.
1 Java Networking – Part I CS , Spring 2008/9.
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.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
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.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
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.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
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.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
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.
Data Communications and Networks Chapter 6 – IP, UDP and TCP ICT-BVF8.1- Data Communications and Network Trainer: Dr. Abbes Sebihi.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Network Communications A Brief Introduction. 2 Network Communications.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming. These days almost all devices.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Object-Orientated Analysis, Design and Programming
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Java Network Programming
CSCD 330 Network Programming
UNIT-6.
Networking.
Sockets and URLs 3-Dec-18.
CSCD 330 Network Programming
Java Socket Programming
Presentation transcript:

Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and output streams

Socket Programming What is socket programming? The term network programming (also called socket programming) refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network. Examples of network applications include web, torrents, mail, chat etc Java.net package provides APIs for writing applications that run on the net

Telephone Analogy A telephone call over a PSTN or Mobile Network works as follows:  Both parties who wants to communicate must have telephone installed  A phone number is assigned to each telephone  Turn on the ringer alert to listen for a call  Caller dials a number  Telephone rings and the receiver answers the call  Both parties talk and exchange data/information  They hang up This is called circuit switched system

Explaining the Analogy A telephone call is an example of a network application Network applications work as follows:  An endpoint (telephone) for communication is created for both sides  An address (telephone number) is assigned to both ends  One end point (caller) initiates a connection to another  The receiver actively waits(listens ) for any incoming connections and accepts  Once a connection is made, data is exchanged  A connections is closed

Packet Switched Networks Packet-switched networks move data in separate, small blocks -- packets -- based on the destination address in each packet. When received, packets are reassembled in the proper sequence to make up the message

TCP/IP Model

TCP and UDP TCP: stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. TCP is stateful using three way handshake UDP: stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications UDP has no acknowledgements and is stateless

Putting it all together Socket() - the end points of a communication Bind() - Assign a unique telephone number Listen() - Wait for a caller Connect() - Dial a number Accept() - Answer a call Send()/Recv() - Talk/exchange data Close () - Hang up

Socket A socket is an endpoint for communication between two machines. The actual work of the socket is performed by an instance of the Socket class. A socket consists of three things:  An IP address  A transport protocol  A port number

Steps in Making a TCP connection with Sockets The server instantiates a ServerSocket object, denoting which port number communication is to occur on. The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port. After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to. The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server. On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket.

TCP connection

Ports A port is a virtualisation identifier defining a service endpoint used to differentiate between different services The purpose of ports is to differentiate multiple endpoints on a given network address 16-bit number Categorized into:  Reserved 0, 1023,  Well Known – from  "dyn" in the ports field denotes dynamically allocated port(s), usually in the range >=1024 <=65535

Well Known Ports

Java Classes for Sockets InetAddress Socket ServerSocket DatagramSocket DatagramPacket

InetAddress class  static methods you can use to create new InetAddress objects.  getByName(String host)  getAllByName(String host)  getLocalHost() InetAddress x = InetAddress.getByName( “cse.unr.edu”);  Throws UnknownHostException Java Socket Programming 16

try { InetAddress a = InetAddress.getByName(hostname); System.out.println(hostname + ":" + a.getHostAddress()); } catch (UnknownHostException e) { System.out.println("No address found for " + hostname); } try { InetAddress a = InetAddress.getByName(hostname); System.out.println(hostname + ":" + a.getHostAddress()); } catch (UnknownHostException e) { System.out.println("No address found for " + hostname); } Java Socket Programming

Socket class  Corresponds to active TCP sockets only!  client sockets  socket returned by accept();  Passive sockets are supported by a different class:  ServerSocket  UDP sockets are supported by  DatagramSocket Java Socket Programming 18

JAVA TCP Sockets  java.net.Socket  Implements client sockets (also called just “sockets”).  An endpoint for communication between two machines.  Constructor and Methods Socket(String host, int port): Creates a stream socket and connects it to the specified port number on the named host. InputStream getInputStream() OutputStream getOutputStream() close()  java.net.ServerSocket  Implements server sockets.  Waits for requests to come in over the network.  Performs some operation based on the request.  Constructor and Methods ServerSocket(int port) Socket Accept(): Listens for a connection to be made to this socket and accepts it. This method blocks until a connection is made. Java Socket Programming 19

Socket Constructors  Constructor creates a TCP connection to a named TCP server.  There are a number of constructors: Socket(InetAddress server, int port); Socket(InetAddress server, int port, InetAddress local, int localport); Socket(String hostname, int port); Java Socket Programming 20

Socket Methods void close(); InetAddress getInetAddress(); InetAddress getLocalAddress(); InputStream getInputStream(); OutputStream getOutputStream();  Lots more (setting/getting socket options, partial close, etc.) Java Socket Programming 21

Socket I/O  Provides a mechanism through which data and read and written into an application  Socket I/O is based on the Java I/O support – in the package java.io; InputStream and OutputStream are abstract classes The inputStream class provide methods for reading data The output stream class provide methods for writing data to the application Java Socket Programming 22

Streams IO Streams are a core concept in Java IO. A stream is a conceptually endless flow of data. You can either read from a stream or write to a stream. A stream is connected to a data source or a data destination. Streams in Java IO can be either byte based (reading and writing bytes) or character based (reading and writing characters).

Reader and Writer A program that needs to read data from some source needs an InputStream or a Reader. A program that needs to write data to some destination needs an OutputStream or a Writer. SourceProgramDestination Reader/InputStream Writer/OutputStream

Java IO Purposes and Features Java IO contains many subclasses of the InputStream, OutputStream, Reader and Writer classes. The reason is, that all of these subclasses are addressing various different purposes. The purposes addressed are summarized below: File Access Network Access Internal Memory Buffer Access Inter-Thread Communication (Pipes) Buffering Filtering Parsing Reading and Writing Text (Readers / Writers) Reading and Writing Primitive Data (long, int etc.) Reading and Writing Objects

InputStream Basics // reads some number of bytes and // puts in buffer array b int read(byte[] b); // reads up to len bytes int read(byte[] b, int off, int len); Both methods can throw IOException. Both return –1 on EOF. Java Socket Programming 26

OutputStream Basics // writes b.length bytes void write(byte[] b); // writes len bytes starting // at offset off void write(byte[] b, int off, int len); Both methods can throw IOException. Java Socket Programming 27

ServerSocket Class (TCP Passive Socket)  Constructors: ServerSocket(int port); ServerSocket(int port, int backlog); ServerSocket(int port, int backlog, InetAddress bindAddr); Java Socket Programming 28

ServerSocket Methods Socket accept(); void close(); InetAddress getInetAddress(); int getLocalPort(); throw IOException, SecurityException Java Socket Programming 29