1 / 18

Introduction to Sockets

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 number so that the TCP layer can identify the application to which data is destined to be sent.”. Sockets.

Download Presentation

Introduction to Sockets

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. 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 number so that the TCP layer can identify the application to which data is destined to be sent.”

  2. Sockets • Interface between application and protocol software • de facto standard • Usually, part of OS itself • Integrated with system I/O. Like file I/O. • open-read-write-close paradigm • Referenced by integer socket descriptor

  3. Networking Review • TCP/IP Protocol Suite • Application programming interface to TCP/IP (API) • BSD Unix • Java.net.Socket • Platform Independence

  4. Ports • TCP and UDP use ports to map incoming data to a particular process running on the computer • Ports are 16 bit numbers • Need IP Address (32-bits) and port. • 0-1023 Restricted out of 65,535.

  5. Networking Classes in Java • TCP: URL, URLConnection, Socket, ServerSocket • UDP: DatagramPacket, DatagramSocket, MulticastSocket • Socket and ServerSocket form two ends of communicaiton link.

  6. Procedure • Open a socket • Open an input and output stream to the socket • Read from and write to the stream according to the server’s protocol • close streams • close socket

  7. Client/Server • Request - Response • A server application waits passively for contact. • A client application initiates communication actively.

  8. Clients • Client temporarily when remote access needed. Does other computation locally. • Runs locally • Invoked by user. • Initiates contact • Can access multiple services as needed • No special h/w or OS

  9. Servers • Special purpose to provide one service • Invoked automatically at boot • Runs on server (shared computer) • Waits on connections passively • Accept connections from arbitrary clients • Requires powerful h/w and special purpose OS

  10. Datagrams • An independent self-contained message • Not Reliable • DatagramPacket and DatagramSocket • DatagramSocket: Specify port or not • DatagramPacket: Contains addressing information • MulticastSocket: To broadcast

  11. import java.io.*; import java.net.*; import java.util.*; public class QuoteMain { public static void main(String[] args) throws IOException { BufferedReader in = null; boolean moreQuotes = true; DatagramSocket socket = new DatagramSocket(4445); try { in = new BufferedReader(new FileReader("one-liners.txt")); } catch (FileNotFoundException e) { System.err.println("Could not open quote file. Serving time instead."); } }

  12. while (moreQuotes) { try { byte[] buf = new byte[256]; // receive request DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); // figure out response String dString = null; if (in == null) dString = new Date().toString(); else { try { if ((dString= in.readLine()) == null) { in.close(); moreQuotes = false; dString = "No more quotes. Goodbye."; } } catch (IOException e) { dString = "IOException occurred in server."; } }

  13. buf = dString.getBytes(); // send the response to the client at "address" and "port" InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); } catch (IOException e) { e.printStackTrace(); moreQuotes = false; } } socket.close(); }

  14. import java.io.*; import java.net.*; import java.util.*; public class QuoteClient { public static void main(String[] args) throws IOException { if (args.length != 1) { System.out.println("Usage: java QuoteClient <hostname>"); return; } // get a datagram socket DatagramSocket socket = new DatagramSocket(); // send request byte[] buf = new byte[256]; InetAddress address = InetAddress.getByName(args[0]); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445); socket.send(packet);

  15. // get response packet = new DatagramPacket(buf, buf.length); socket.receive(packet); // display response String received = new String(packet.getData()); System.out.println("Quote of the Moment: " + received); socket.close(); } }

More Related