Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Java Session 4 - Extra New York University School of Continuing and Professional Studies."— Presentation transcript:

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

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

3 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 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 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 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 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 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 9 Sending UDP Datagram Create a new Socket and bind it to port 19 DatagramSocket socket = new DatagramSocket(19); Send data to 192.168.1.128 on port 91 byte[] sendbuf = new byte[8192]; InetAddress ia = InetAddress.getByName(“192.168.1.128”); 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 10 Receiving UDP Datagram Create a new Socket and bind it to port 19 DatagramSocket socket = new DatagramSocket(19); Connect to 192.168.1.128 on port 91 (java 1.2) InetAddress ia = InetAddress.getByName(“192.168.1.128”); 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 11 Receiving on Multicast Sockets Create a Multicast Socket MulticastSocket ms = new MulticastSocket(4000); Join a Multicast Group InetAddress ia = InetAddress.getByName(“224.1.2.3”); 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 12 Sending to Multicast Groups Create a Multicast Socket MulticastSocket ms = new MulticastSocket(); Join a Multicast Group InetAddress ia = InetAddress.getByName(“224.1.2.3”); ms.joinGroup(ia); Send Data byte[] buffer = new buffer[8192]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 4000); ms.send(dp);

13 13 Clients using SocketChannel Create a SocketChannel SocketAddress kaza = new InetSocketAddress(www.kaza.com, 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 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 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 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 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 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 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 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 224.0.0.1 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 21 UDP Examples UDPServer.java and UDPEchoServer.java UDPClient.java

22 22 Multicast Examples MulticastSniffer.java MulticastSender.java

23 23 SocketChannel Examples ChargenClient.java ChargenServer.java

24 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 25 Thank you


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

Similar presentations


Ads by Google