1 / 21

Java Sockets Programming

Java Sockets Programming. The package java.net provides support for sockets programming (and more). Typically you import everything defined in this package with: import java.net.*;. Classes. InetAddress Socket ServerSocket DatagramSocket DatagramPacket. InetAddress class.

miguele
Download Presentation

Java Sockets Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Sockets Programming • The package java.net provides support for sockets programming (and more). • Typically you import everything defined in this package with: import java.net.*; Netprog: Java Sockets

  2. Classes InetAddress Socket ServerSocket DatagramSocket DatagramPacket Netprog: Java Sockets

  3. InetAddress class • static methods you can use to create new InetAddress objects. • getByName(String host) • getLocalHost() InetAddress x = InetAddress.getByName( “monte.cs.rpi.edu”); Netprog: Java Sockets

  4. Sample Code: Lookup.java • Uses InetAddress class to lookup hostnames found on command line. > java Lookup monte www.yahoo.com monte:128.213.8.110 www.yahoo.com:64.58.76.176 Netprog: Java Sockets

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

  6. 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 Netprog: Java Sockets

  7. 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); Netprog: Java Sockets

  8. Socket Methods void close(); InetAddress getInetAddress();getpeername InetAddress getLocalAddress(); getsockname InputStream getInputStream(); OutputStream getOutputStream(); • Lots more (setting/getting socket options, partial close, etc.) Netprog: Java Sockets

  9. Socket I/O • Socket I/O is based on the Java I/O support (in the package java.io). • InputStream and OutputStream are abstract classes • common operations defined for all kinds of InputStreams, OutputStreams… Netprog: Java Sockets

  10. 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. Netprog: Java Sockets

  11. 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. Netprog: Java Sockets

  12. ServerSocket Class(TCP Passive Socket) • Constructors: ServerSocket(int port); ServerSocket(int port, int backlog); ServerSocket(int port, int backlog, InetAddress bindAddr); Netprog: Java Sockets

  13. ServerSocket Methods Socket accept(); void close(); InetAddress getInetAddress(); int getLocalPort(); throw IOException, SecurityException Netprog: Java Sockets

  14. Sample Echo Server TCPEchoServer.java Simple TCP Echo server. Based on code from: TCP/IP Sockets in Java Netprog: Java Sockets

  15. UDP Sockets • DatagramSocket class • DatagramPacket class needed to specify the payload (incoming or outgoing). Netprog: Java Sockets

  16. DatagramSocket Constructors DatagramSocket(); DatagramSocket(int port); DatagramSocket(int port, InetAddress a); All can throw SocketException or SecurityException. Netprog: Java Sockets

  17. Datagram Methods void connect(InetAddress, int port); void close(); void receive(DatagramPacket p); void send(DatagramPacket p); Lots more! Netprog: Java Sockets

  18. DatagramPacket • Contain the payload (a byte array). • Can also be used to specify the destination address (when not using connected mode UDP). Netprog: Java Sockets

  19. DatagramPacket Constructors For receiving: DatagramPacket( byte[] buf, int len); For sending: DatagramPacket( byte[] buf, int len InetAddress a, int port); Netprog: Java Sockets

  20. DatagramPacket methods byte[] getData(); void setData(byte[] buf); void setAddress(InetAddress a); void setPort(int port); InetAddress getAddress(); int getPort(); destination address could be either address (depends on context) Netprog: Java Sockets

  21. Sample UDP code UDPEchoServer.java Simple UDP Echo server. Test using nc as the client (netcat): > nc –u hostname port Netprog: Java Sockets

More Related