1 / 14

UDP vs TCP

UDP vs TCP. UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP. Datagram Sockets . The sending/receiving point- Class DatagramSocket void close() : close a datagram socket

Download Presentation

UDP vs TCP

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. UDP vs TCP • UDP • Low-level, connectionless • No reliability guarantee • TCP • Connection-oriented • Not as efficient as UDP

  2. Datagram Sockets • The sending/receiving point- Class DatagramSocket • void close() : close a datagram socket • int getLocalPort() : returns the port number on which socket is bound • InetAddress getLocalAddress() : returns the local address to which the socket is bound • void receive(DatagramPacket p) : blocks until a datagram is received and the packet’s buffer contains the data received • void send(DatagramPacket p) : sends a datagram packet from this socket

  3. //Datagram Server publicclass DatagramServer { publicstaticvoid main(String[] args) { DatagramPacket datapacket, returnpacket; int port = 2018; int len = 1024; try { DatagramSocket datasocket = new DatagramSocket(port); byte[] buf = newbyte[len]; datapacket = new DatagramPacket(buf, buf.length); while (true) { try { datasocket.receive(datapacket); returnpacket = new DatagramPacket( datapacket.getData(), datapacket.getLength(), datapacket.getAddress(), datapacket.getPort()); datasocket.send(returnpacket); } catch (IOException e) { System.err.println(e); } } } catch (SocketException se) { System.err.println(se); } } }

  4. //Datagram Client publicclass DatagramClient { publicstaticvoid main(String[] args) { String hostname; int port = 2018; int len = 1024; DatagramPacket sPacket, rPacket; InetAddress ia = InetAddress.getByName(hostname); DatagramSocket datasocket = new DatagramSocket(); BufferedReader stdinp = new BufferedReader( new InputStreamReader(System.in)); while (true) { String echoline = stdinp.readLine(); if (echoline.equals("done")) break; byte[] buffer = newbyte[echoline.length()]; buffer = echoline.getBytes(); sPacket = new DatagramPacket(buffer, buffer.length, ia, port); datasocket.send(sPacket); byte[] rbuffer = newbyte[len]; rPacket = new DatagramPacket(rbuffer, rbuffer.length); datasocket.receive(rPacket); String retstring = new String(rPacket.getData()); System.out.println(retstring); } catch (IOException e) { System.err.println(e); } } // while } } // end main

  5. TCP Sockets • A connection is set up between the sender and the receiver • Class Socket • Socket(String host, int port) : creates a stream socket and connects it to the specified port number on the host • InputStream getInputStream() : returns an input stream for reading bytes from this socket • OutputStream getOutputStream() : returns an output stream for writing bytes to this socket

  6. Server Sockets • Creates a server side socket on the specified port • class ServerSocket • InetAddress getInetAddress() : returns the address to which this socket is connected • Socket accept() : blocking method which waits for a connection to be made and accepts it

  7. Example: NameServer • Maps a name to the host and port number • Create a server socket • Listen for incoming connections (accept())

  8. //NameServer publicclass NameServer { NameTable table; public NameServer() { table = new NameTable(); } void handleclient(Socket theClient) { BufferedReader din = new BufferedReader (new InputStreamReader(theClient.getInputStream())); PrintWriter pout = new PrintWriter(theClient.getOutputStream()); String getline = din.readLine(); StringTokenizer st = new StringTokenizer(getline); String tag = st.nextToken(); if (tag.equals("search")) { ... } elseif (tag.equals("insert")) { ... } } publicstaticvoid main(String[] args) { NameServer ns = new NameServer(); System.out.println("NameServer started:"); ServerSocket listener = new ServerSocket(Symbols.ServerPort); while (true) { Socket aClient = listener.accept(); ns.handleclient(aClient); aClient.close(); } } }

  9. //Client for name server publicclass Name { BufferedReader din; PrintStream pout; publicvoid getSocket() throws IOException { Socket server = new Socket(Symbols.nameServer, Symbols.ServerPort); din = new BufferedReader(new InputStreamReader(server.getInputStream())); pout = new PrintStream(server.getOutputStream()); } publicint insertName(String name, String hname, int portnum){ getSocket(); pout.println("insert " + name + " " + hname + " " + portnum); pout.flush(); return Integer.parseInt(din.readLine()); } public PortAddr searchName(String name) throws IOException { getSocket(); pout.println("search " + name); pout.flush(); String result = din.readLine(); StringTokenizer st = new StringTokenizer(result); int portnum = Integer.parseInt(st.nextToken()); String hname = st.nextToken(); returnnew PortAddr(hname, portnum); } publicstaticvoid main(String[] args) { Name myClient = new Name(); myClient.insertName("hello1", "birch.ece.utexas.edu", 1000); PortAddr pa = myClient.searchName("hello1"); System.out.println(pa.gethostname() + ":" + pa.getportnum()); } }

  10. Remote Objects • Methods can be called by another JVM on a different host • Interface is remote if it extends Remote • Remote object implements a remote interface and extends UnicastRemoteObject publicinterface NameService extends Remote { publicint search(String s) throws RemoteException; publicint insert(String s, String hostName, int portNumber) throws RemoteException; publicint getPort(int index) throws RemoteException; public String getHostName(int index) throws RemoteException; }

  11. // A name service implementation publicclass NameServiceImpl extends UnicastRemoteObject implements NameService { . . . public NameServiceImpl() throws RemoteException { } publicint search(String s) throws RemoteException { . . . } publicint insert(String s, String hostName, int portNumber) throws RemoteException { . . . } publicint getPort(int index) throws RemoteException { return ports[index]; } public String getHostName(int index) throws RemoteException { return hosts[index]; } publicstaticvoid main(String args[]) { // create security manager System.setSecurityManager(new RMISecurityManager()); NameServiceImpl obj = new NameServiceImpl(); Naming.rebind("MyNameServer", obj); System.out.println("MyNameServer bound in registry"); } }

  12. Parameter Passing • Primitive types are passed by value • Objects (that are not remote) • They are serialized and then passed by value. • At the other end the objects are deserialized • Any references inside the object are also serialized • Remote Objects • Passed as remote references (stubs)

  13. RMI Client • Obtain a reference for the remote object • URL for the remote object is specified as • rmi://host:port/name

  14. //A RMI client program import java.rmi.*; publicclass NameRmiClient { publicstaticvoid main(String args[]) { try { NameService r = (NameService) Naming.lookup("rmi://linux02/MyNameServer"); int i = r.insert("p1", "tick.ece", 2058); int j = r.search("p1"); if (j != -1) System.out.println(r.getHostName(j) + ":" + r.getPort(j)); } catch (Exception e) { System.out.println(e); } } }

More Related