Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and."— Presentation transcript:

1 CHAPTER13 @geoffjuma

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

3 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

4 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

5 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

6 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

7 TCP/IP Model

8 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

9 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

10 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

11 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.

12 TCP connection

13 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 0-1023  "dyn" in the ports field denotes dynamically allocated port(s), usually in the range >=1024 <=65535

14 Well Known Ports

15 Java Classes for Sockets InetAddress Socket ServerSocket DatagramSocket DatagramPacket

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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).

24 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

25 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

26 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

27 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

28 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

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


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

Similar presentations


Ads by Google