180 likes | 307 Vues
This overview explores the concept of sockets as essential endpoints for two-way communication between programs on a network. Sockets serve as an interface between application software and TCP/IP protocol, facilitating data exchange via port numbers that link applications to incoming data. Key topics include the client-server model, socket programming in Java, the use of TCP and UDP protocols, and the process of opening and closing sockets. This guide is designed for both beginners and experienced developers seeking to enhance their networking skills with practical examples.
E N D
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 • 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
Networking Review • TCP/IP Protocol Suite • Application programming interface to TCP/IP (API) • BSD Unix • Java.net.Socket • Platform Independence
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.
Networking Classes in Java • TCP: URL, URLConnection, Socket, ServerSocket • UDP: DatagramPacket, DatagramSocket, MulticastSocket • Socket and ServerSocket form two ends of communicaiton link.
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
Client/Server • Request - Response • A server application waits passively for contact. • A client application initiates communication actively.
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
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
Datagrams • An independent self-contained message • Not Reliable • DatagramPacket and DatagramSocket • DatagramSocket: Specify port or not • DatagramPacket: Contains addressing information • MulticastSocket: To broadcast
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."); } }
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."; } }
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(); }
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);
// 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(); } }