UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
Referring to Java API Specifications
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Datagram Programming A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
JAVA - Network DUT Info - Option ISI (C) Philippe Roose , 2005.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
第十一讲 分布式编程 Socket and RMI 李庆旭. 2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Java.net CS-328 Dick Steflik.
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
NETWORK PROGRAMMING CNET 441
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Network Programming Introduction
Socket programming with TCP
Java Network Programming
„Networking”.
Networking.
NETWORK PROGRAMMING CNET 441
Java Socket Programming
Chapter 2: Application layer
Socket Programming with UDP
Review Communication via paired sockets, one local and one remote
Presentation transcript:

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 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

//Datagram Server public class DatagramServer { public static void main(String[] args) { DatagramPacket datapacket, returnpacket; int port = 2018; int len = 1024; try { DatagramSocket datasocket = new DatagramSocket(port); byte[] buf = new byte[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); }

//Datagram Client public class DatagramClient { public static void 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 = new byte[echoline.length()]; buffer = echoline.getBytes(); sPacket = new DatagramPacket(buffer, buffer.length, ia, port); datasocket.send(sPacket); byte[] rbuffer = new byte[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

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

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

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

//NameServer public class 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")) {... } else if (tag.equals("insert")) {... } } public static void 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(); }

//Client for name server public class Name { BufferedReader din; PrintStream pout; public void getSocket() throws IOException { Socket server = new Socket(Symbols.nameServer, Symbols.ServerPort); din = new BufferedReader(new InputStreamReader(server.getInputStream())); pout = new PrintStream(server.getOutputStream()); } public int 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(); return new PortAddr(hname, portnum); } public static void 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()); }

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 public interface NameService extends Remote { public int search(String s) throws RemoteException; public int insert(String s, String hostName, int portNumber) throws RemoteException; public int getPort(int index) throws RemoteException; public String getHostName(int index) throws RemoteException; }

// A name service implementation public class NameServiceImpl extends UnicastRemoteObject implements NameService {... public NameServiceImpl() throws RemoteException { } public int search(String s) throws RemoteException {... } public int insert(String s, String hostName, int portNumber) throws RemoteException {... } public int getPort(int index) throws RemoteException { return ports[index]; } public String getHostName(int index) throws RemoteException { return hosts[index]; } public static void 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"); }

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)

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

//A RMI client program import java.rmi.*; public class NameRmiClient { public static void 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); }