Advanced Java Session 4 - Extra New York University School of Continuing and Professional Studies.

Slides:



Advertisements
Similar presentations
Java Network Programming Vishnuvardhan.M. Dept. of Computer Science - SSBN Java Overview Object-oriented Developed with the network in mind Built-in exception.
Advertisements

CS3505 The Internet and Info Hiway transport layer protocols : TCP/UDP.
Network Programming Chapter 11 Lecture 6. Networks.
Socket Programming.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
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
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
1 © 2003, Cisco Systems, Inc. All rights reserved. CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
IST 228\Ch3\IP Addressing1 TCP/IP and DoD Model (TCP/IP Model)
CS 350 Chapter-6. A brief history of TCP/IP 1983 TCP/IP came to ARPAnet ARPAnet and MILNET dissolved in 1990 BSD UNIX.
Chapter Eleven An Introduction to TCP/IP. Objectives To compare TCP/IP’s layered structure to OSI To review the structure of an IP address To look at.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Inter-process Communication.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Network Protocols. Why Protocols?  Rules and procedures to govern communication Some for transferring data Some for transferring data Some for route.
70-291: MCSE Guide to Managing a Microsoft Windows Server 2003 Network Chapter 3: TCP/IP Architecture.
Introduction to Networking and TCP/IP J. H. Wang Jun. 8, 2005.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
TCP/IP Essentials A Lab-Based Approach Shivendra Panwar, Shiwen Mao Jeong-dong Ryoo, and Yihan Li Chapter 5 UDP and Its Applications.
1 7-Oct-15 OSI transport layer CCNA Exploration Semester 1 Chapter 4.
Chapter 4 TCP/IP Overview Connecting People To Information.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
 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.
1 Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Distributed Systems Concepts and Design Chapter 4.
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.
1 UDP : User Datagram Protocol Computer Network System Sirak Kaewjamnong.
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.
Basic IP Protocol Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
1 Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
1 Netprog 2002 TCP/IP UDP/IP in Java Based on Java Network Programming and Distributed Computing.
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.
Fall 2004FSU CIS 5930 Internet Protocols1 TCP/IP Protocol Suite Reading: Chapter 13.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
Network Basics CS490 - Security in Computing Copyright © 2005 by Scott Orr and the Trustees of Indiana University.
TCP/IP Protocol Suite ©Richard L. Goldman September 25, 2002.
TCP/IP PROTOCOL UNIT 6. Overview of TCP/IP Application FTP, Telnet, SMTP, HTTP.. Presentation Session TransportHost-to-HostTCP, UDP NetworkInternetIP,
1 14-Jun-16 S Ward Abingdon and Witney College CCNA Exploration Semester 1 OSI transport layer CCNA Exploration Semester 1 Chapter 4.
Networking Mehdi Einali Advanced Programming in Java 1.
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.
Socket Programming Ameera Almasoud
Application Layer Functionality and Protocols Abdul Hadi Alaidi
The Transport Layer Implementation Services Functions Protocols
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
Socket Programming Cal Poly Pomona Young CS380.
The Socket API 9/17/2018.
Introduction to TCP/IP
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Chapter 16 Socket Interface.
27.
Transport Protocols An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Outline Introduction Networking Basics Understanding Ports and Sockets
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Presentation transcript:

Advanced Java Session 4 - Extra New York University School of Continuing and Professional Studies

2 Objectives TCP/IP Protocol Stack Java and TCP/UDP/Multicast SSLSocket and SSLServerSocket SocketChannel class SocketImpl and SocketFactory

3 TCP/IP protocols ping Telnet, Rlogin, FTP, SMTP, X, POP3, HTTP, NNTP DNS, TFTP, BOOTP, SNMP, NFS, RPC Trace- route ICMP IPIGMP ARP Data Link RARP TCPUDP

4 Java Socket Classes TCPUDPMulticast EndpointSocket, SSLSocket ServerSocket, SSLServerSocket DatagramSocketMulticastSociet (extends DatagramSocket) DatainputStream and outputStream DatagramPacket ChannelSocketChannel ServerSocketChannel (extend SelectableChannel) DatagramChannel (extend SelectableChannel) DatagramChannel (extend SelectableChannel) Data for Channel ByteBuffer

5 Client TCP Sockets Connect to a remote machine Socket socket = new Socket(host, port); Send/Receive data InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); Close Connection socket.close();

6 Server TCP Sockets Create a ServerSocket and bind to a port ServerSocket serversocket = new ServerSocket(port); Accepting incoming connections Socket socket = ssocket.accept(); Send/Receive data InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); Close an individual socket Connection socket.close(); Close all conencted sockets and stop listening serversocket.close();

7 Secure Client TCP Sockets Add Security Provider Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Get Default Socket Factory SSLSocketFactory factory = SSLSocketFactory.getDefault(); Create a new socket Socket socket = factory.createSocket(“login.hotmail.com”, 80); Send/Receive data InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); Close socket Socket.close();

8 Secure Server TCP Sockets Add Security Provider Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Get Default Socket Factory SSLServerSocketFactory factory = SSLServerSocketFactory.getDefault(); Create a ServerSocket and bind to a port SSLServerSocket serversocket = factory.createServerSocket(port); Accepting incoming connections Socket socket = ssocket.accept(); Send/Receive data InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); Close an individual socket Connection socket.close(); Close all conencted sockets and stop listening serversocket.close();

9 Sending UDP Datagram Create a new Socket and bind it to port 19 DatagramSocket socket = new DatagramSocket(19); Send data to on port 91 byte[] sendbuf = new byte[8192]; InetAddress ia = InetAddress.getByName(“ ”); DatagramPacket dp = new DatagramPacket(sendbuf, sendbuf.length); dp.setAddress(is); dp.setPort(91) socket.send(dp); Receive data (from anyone) Byte[] recvbuf = new byte[8192]; DatagramPacket dp = new DatagramPacket(recvbuf, recvbuf.length) Dp

10 Receiving UDP Datagram Create a new Socket and bind it to port 19 DatagramSocket socket = new DatagramSocket(19); Connect to on port 91 (java 1.2) InetAddress ia = InetAddress.getByName(“ ”); socket.connect(ia, 91); Receive data (from anyone if not connected) Byte[] recvbuf = new byte[8192]; DatagramPacket dp = new DatagramPacket(recvbuf, recvbuf.length) Socket.receive(dp);

11 Receiving on Multicast Sockets Create a Multicast Socket MulticastSocket ms = new MulticastSocket(4000); Join a Multicast Group InetAddress ia = InetAddress.getByName(“ ”); ms.joinGroup(ia); Receive Data byte[] buffer = new buffer[8192]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ms.receive(dp); System.out.println(new String(dp.getData());

12 Sending to Multicast Groups Create a Multicast Socket MulticastSocket ms = new MulticastSocket(); Join a Multicast Group InetAddress ia = InetAddress.getByName(“ ”); ms.joinGroup(ia); Send Data byte[] buffer = new buffer[8192]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 4000); ms.send(dp);

13 Clients using SocketChannel Create a SocketChannel SocketAddress kaza = new InetSocketAddress( 80); SocketChannel client = SocketChannel.open(kaza); Receiving data using a SocketChannel ByteBuffer rbuf = ByteBuffer.allocate(74); int bytesRead = client.read(rbuf); Sending Data using a SocketChannel ByteBuffer sbuf = ByteBuffer.allocate(74); sbuf.put(“hello”.getBytes(), 5); sbuf.flip() client.write(sbuf); Configuring for Non-Blocking IO Client.configureBlocking(false);

14 Servers using SocketChannel Create a ServerSocketChannel to listen on port 19 ServerSocketChannel sChannel = ServerSocketChannel.open(); ServerSocket ss = serverChannel.socket(); ss.bind(new InetSocketAddress(19); Accepting incoming connections SocketChannel client = serverChannel.accept(); Receiving data ByteBuffer rbuf = ByteBuffer.allocate(74); int bytesRead = client.read(rbuf); Sending Data ByteBuffer sbuf = ByteBuffer.allocate(74); sbuf.put(“hello”.getBytes()); sbuf.flip() client.write(sbuf);

15 Using Selector Create a ServerSocketChannel to listen on port 19 ServerSocketChannel sChannel = ServerSocketChannel.open(); ServerSocket ss = serverChannel.socket(); ss.bind(new InetSocketAddress(19); Set the channel to non-blocking mode sChannel.configureBlocking(false); Create a Selector Selector selector = Selector.open(); Register Server channel with the Selector sChannel.register(selector, SelectionKey.OP_ACCEPT);

16 Accepting Connections using Selector Get the set of ready SelectionKeys Set readyKeys = selector.selectedKeys(); Iterate over the set to find the channel that’s ready for “accept” operation Iterator iter = readyKeys.iterator(); While( iter.hasNext() ) { SelectionKey key = (SelectionKey)iter.next(); iter.remove(); // I don’t need this key again if( key.isAcceptable() ) { ServerSocketChannel server = (ServerSocketChannel)key.channel(); SocketChannel client = server.accept(); client.configureBlocking(false); SelectionKey clientKey = client.register(selector, SelectionKey.OP_WRITE); // if the server only needs to write to this ByteByffer buffer = ByteBuffer.allocate(16); buffer.put(“Hello World”.getBytes()); key.attach(buffer); }

17 Writing data using Selector Get the set of ready SelectionKeys Set readyKeys = selector.selectedKeys(); Iterate over the set to find the channel that’s ready for “write” operation Iterator iter = readyKeys.iterator(); While( iter.hasNext() ) { SelectionKey key = (SelectionKey)iter.next(); iter.remove(); // I don’t need this key again if( key.isWritable() ) { SocketChannel client = (SocketChannel)key.channel(); ByteByffer buffer = (ByteBuffer)key.attachment(); if( !buffer.hasRemaining() ) { // refill buffer and “flip” it } else client.write(buffer) }

18 SocketImpl Implements SocketOptions interface Can only be used by subclasses of Socket public class MySocket extends Socket { public MySocket(host, port) { SocketImpl si = new SocketImpl(); si.setOption(SO_LINGER, new Integer(10)); super(si); }

19 SocketImpl Another use is in defining custom SocketFactories and ServerSocketFactories e.g. to implement native sockets using JNI, one would implement JNISocket, JNISocektFactory, JNIServerSocketFactory

20 Unicast, Broadcast, Multicast Unicast is to a single IP address and is only possible with a connection oriented protocol - TCP Broadcast and Multicast both support sending data without a “connection” and are done with a connectionless protocol – UDP IGMP – Internet Group Management protocol provides support for Multicast Groups is the “All Hosts” group address – it refers to all the multicast-capable hosts and routers on a physical network – each host automatically joins this multicast group on all multicast-capable interfaces when the interface is initialized.

21 UDP Examples UDPServer.java and UDPEchoServer.java UDPClient.java

22 Multicast Examples MulticastSniffer.java MulticastSender.java

23 SocketChannel Examples ChargenClient.java ChargenServer.java

24 Network Security and Applets Can’t connect to servers other than the one it was downloaded from. Can’t access local hard disk Signed applets may be permitted to override the security – provided that the user permits it.

25 Thank you