School of Information Technologies Distributed Programming NETS3303/3603 Week 2.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Network Programming and Java Sockets
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.
Socket Programming.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
1 Java Networking – Part I CS , Spring 2008/9.
Client-Server Network Programming Session 2: Programming with Java and project A workshop by Dr. Junaid Ahmed Zubairi Department of Computer Science State.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
System Programming Practical session 10 Java sockets.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Network Layers Application Transport Network Data-Link Physical bits.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
CS 3830 Day 11 Introduction : Application Layer 2 Server-client vs. P2P: example Client upload rate = u, F/u = 1 hour, u s = 10u, d min ≥ u s.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
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
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Distributed Systems Concepts and Design Chapter 4.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
Part 4: Network Applications Client-server interaction, example applications.
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
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.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
1 Network Communications A Brief Introduction. 2 Network Communications.
Network Programming. These days almost all devices.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Object-Orientated Analysis, Design and Programming
MCA – 405 Elective –I (A) Java Programming & Technology
NETWORK PROGRAMMING CNET 441
Socket programming - Java
CSCD 330 Network Programming
Socket Programming.
CSCD 330 Network Programming
Chapter 2: Application layer
Socket Programming with UDP
Review Communication via paired sockets, one local and one remote
Presentation transcript:

School of Information Technologies Distributed Programming NETS3303/3603 Week 2

School of Information Technologies 2 Distributed Programming What is a distributed system? –Consists of a collection of computers linked together by a network Main characteristics: –Autonomous nodes with no global clock (asynchronous) –Communication is via message passing Advantages: enables computers to share resources (hardware, software, data) What is distributed programming? –To assign the work of a program to two or more processes—where processes may exists on different computers © Qusay H. Mahmoud

School of Information Technologies 3 Architecture of Distributed Programming Goals: –Dividing responsibilities (processes) between system components properly –Processes with well-defined responsibilities interact with each other to perform a useful activity © Qusay H. Mahmoud

School of Information Technologies 4 Distributed Programming Models The client-server model –Most common –Division of software in a client and a server process –Server has access to some hw/sw resource that client wants to use –A many-to-one relationship –E.g.: web server, search engine

School of Information Technologies 5 Client - Server Process 1 Process 2 Process 3

School of Information Technologies 6 Distributed Programming Models Peer-to-Peer (aka multi-agent) –All nodes equal, both client and server (on the rise) –All peers play similar roles –Can make requests as well as fulfil requests –Better interactive response –Examples: Skype, BitTorrent, Napster, Gnutella

School of Information Technologies 7 P-2-P

School of Information Technologies 8 Interprocess Communication (IPC) A set of programming APIs that coordinate activities among different processes to run concurrently in a network IPC methods include pipes, message queuing, semaphores, shared memory, and sockets

School of Information Technologies 9 Process Communication A pipe is a pseudo-file that can be used to connect two processes together Two related processes on a single machine may communicate through a pipe $ ls | grep “test” A write B read pipe Flow of data © Qusay H. Mahmoud

School of Information Technologies 10 Client/Server Communication Two unrelated processes may communicate through files (process A write to a file and process B reads from it) How can two processes located on two different machines communicate? Solution: Berkeley Sockets (or BSD Sockets or Socket APIs)! © Qusay H. Mahmoud

School of Information Technologies 11 Socket A socket is an end point of a connection or an interface between user and network Used in the same way as a file descriptor: –1- Creation (open socket) –2- Receive/send (read/write from/to socket) –3- Destruct (close socket) © Qusay H. Mahmoud

School of Information Technologies 12 Types of Sockets A number of connections to choose from: –TCP, UDP, Multicast Types of Sockets: –SOCK_STREAM (TCP sockets) –SOCK_DGRAM (UDP Sockets) –SOCK_RAW (ICMP) ICMP (Internet Control Message Protocol) sockets: –For security reasons not supported in Java (available in C) –Used in programs such as ping (echo request/reply) © Qusay H. Mahmoud

School of Information Technologies 13 Socket Addresses Each socket has an address that specifies message destinations Each socket address is a communication identifier consists of: –1- Internet address –2- Port number The port number is an integer that is needed to distinguish between services running on the same machine. –Port numbers between are reserved by the system –user port numbers ≥ 1024 © Qusay H. Mahmoud

School of Information Technologies 14 Addressing Protocol IP address 1 Ports Process 1 Process 2 Protocol IP address 2 Ports Process 1 Process 2

School of Information Technologies 15 Transport Protocols: Briefly TCP: Transmission Control Protocol UDP: User Datagram Protocol TCP vs. UDP: –In TCP two sockets must be connected before they can be used to transfer data whereas in UDP they must not –In TCP, packets are acknowledged but in UDP, they are not. Characteristics: –TCP is a reliable protocol, UDP is not –TCP is connection-oriented, UDP is connectionless –TCP incurs overheads, UDP incurs fewer overheads –UDP has a size limit of 64k, in TCP no limit –UDP is easier to use and faster. © Qusay H. Mahmoud

School of Information Technologies 16 Communication Fundamentals For processes to exchange data: –Need pipe from process to stack –Need address to other process –Need knowledge of how data is represented and agreement between processes Things happen both in: –User space –Kernel space

School of Information Technologies 17 Data representation So now we know –Send data to IP x, Proto y, Port z But?? –How do we interpret data? –How do we know what data to expect? –How do we know when things go right or wrong?

School of Information Technologies 18 Have Protocols! Protocols are agreements of –When we can send data –How data is represented; 8-bit, 16-bit etc. –How to interpret data / react For now –Transport protocols (UDP, TCP) –Application protocols (HTTP, SMTP, GNUTELLA)

School of Information Technologies 19 Java APIs J2SE 5.0: InetAddress In java.net package Handles Internet address (name and IP) Static method getByName –returns host name as InetAddress –throws UnknownHostException (so catch it!) There are other useful methods like, isReachable(int timeout) –Check API javadocs for other methods

School of Information Technologies 20 import java.net.*; import java.io.*; public class IPFinder { public static void main(String[] args) throws IOException { String host; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\n\nEnter host name: "); host = input.readLine(); try { InetAddress address = InetAddress.getByName(host); System.out.println(“Host: " + address.toString()); } catch (UnknownHostException e) { System.out.println("Could not find " + host); }

School of Information Technologies 21 Client/server Model A client initiates communication But, before this happens: Server has to listen for connection attempts

School of Information Technologies 22 TCP sockets Connection- oriented—connection remain open throughout duration, until ended Needs two programs for this application

School of Information Technologies 23 TCP Server Create a ServerSocket object –ServerSocket servSock = new ServerSocket (1234); –Server will listen for a connection from a client Put into waiting state (blocks) –Socket link = servSock.accept(); Set up input & output streams –Use getInputStream and getOutputStream –BufferedReader input = new BufferedReader( new InputStreamReader(link.getInputStream())); –PrintWriter out = new PrintWriter(link.getOutputStream(),true); –2 nd argument causes output buffer to be flushed with every println

School of Information Technologies 24 TCP Server II Send and receive data –out.println(“Awaiting data…”); –message = in.readLine(); Close connection –link.close();

School of Information Technologies 25 public class TCPEchoServer { private static ServerSocket servSock; private static final int PORT = 1234; public static void main(String[] args) { System.out.println("Opening port...\n"); try { servSock = new ServerSocket(PORT); //Step 1. } catch(IOException e) { System.out.println("Unable to attach to port!"); System.exit(1); } do { run(); }while (true); } private static void run() { Socket link = null; try { link = servSock.accept(); //Step 2. BufferedReader in = new BufferedReader( new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter( link.getOutputStream(),true); //Step 3. int numMessages = 0; String message = in.readLine(); //Step 4. while (!message.equals("***CLOSE***")) { System.out.println("Message received."); numMessages++; out.println("Message " + numMessages + ": " + message); //Step 4. message = in.readLine(); } out.println(numMessages + " messages received."); } catch(IOException e) { } finally { try { System.out.println("\n* Closing connection... *"); link.close(); //Step 5. } catch(IOException e) { System.out.println("Unable to disconnect!"); }

School of Information Technologies 26 TCP Client Establish a connection to server –Socket link = new Socket(InetAddress.getLocalHost(),1234); Setup input/output streams Send and receive data Close the connection

School of Information Technologies 27 public class TCPEchoClient { private static InetAddress host; private static final int PORT = 1234; public static void main(String[] args) { try { host = InetAddress.getLocalHost(); } catch(UnknownHostException e) { System.out.println("Host ID not found!"); System.exit(1); } run(); } private static void run() { Socket link = null;//Step 1. try { link = new Socket(host,PORT); //Step 1. BufferedReader in = new BufferedReader (new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter( link.getOutputStream(),true); //Step 2. //Set up stream for keyboard entry... BufferedReader userEntry = new BufferedReader (new InputStreamReader(System.in)); String message, response; do { System.out.print("Enter message: "); message = userEntry.readLine(); out.println(message); response = in.readLine();//Step 3. System.out.println("\nSERVER> " + response); }while (!message.equals("***CLOSE***")); } catch(IOException e) {} finally { try { System.out.println("\n* Closing connection... *"); link.close();//Step 4. } catch(IOException e) { System.out.println("Unable to disconnect!"); System.exit(1); }

School of Information Technologies 28 UDP sockets Connectionless => no session Each datagram is an isolated transmission So it is faster! And, server does not need to create a socket for each client! Both server and client use DatagramSocket, and DatagramPacket are created and sent

School of Information Technologies 29 UDP Server Create a DatagramSocket object –DatagramSocket dgramSock = new DatagramSocket(1234); Create a buffer for incoming datagram –Byte[] buff = new byte[256]; Create a DatagramPacket for incoming datagram –DatagramPacket inPkt = new DatagramPacket(buff,buff.length); Accept an incoming datagram –dgramSock.receive(inPkt);

School of Information Technologies 30 UDP Server II Get sender’s address and port –InetAddress clientAdd = inPkt.getAddress(); –int clientPort = inPkt.getPort(); Retrieve data from buffer –String msg = new String(inPkt.getData(),0,inPkt.getLength()); Create a response datagram –DatagramPacket outPkt = new DatagramPacket (reply.getBytes(),reply.length(),clientAdd,clientPort); –reply is a String object Send response datagram –dgramSock.send(outPkt); Close socket –dgramSock.close();

School of Information Technologies 31 DatagramSocket server = new DatagramSocket(5000); byte buffer[] = new byte[1000]; while (true) { DatagramPacket request = new DatagramPacket(buffer, buffer.length); server.receive(request); DatagramPacket reply = new DatagramPacket(request. getData(), request.getLength(), request.getAddress(), request.getPort()); server.send(reply); }

School of Information Technologies 32 UDP Client Create a DatagramSocket object –DatagramSocket dgramSock = new DatagramSocket(); Create the outgoing datagram –DatagramPacket outPkt = new DatagramPacket (msg.getBytes(),msg.length(),host,PORT); Send datagram –dgramSock.send(outPkt); Receiving and processing daragram is same as server before

School of Information Technologies 33 DatagramSocket client = new DatagramSocket(); String Msg=“Message”; byte msg[] = Msg.getBytes(); int port = 5000; InetAddress host = InetAddress.getByName(IPaddress); DatagramPacket request = new DatagramPacket (msg, Msg.length(), host, port); client.send(request); byte buffer[] = new byte[1000]; DatagramPacket reply= new DatagramPacket(buffer,buffer.length); client.receive(reply); System.out.println(“The reply is: “+reply.getData()); client.close();

School of Information Technologies 34 Hint on objects How send complex structures? Objects with states? Arrays, vectors, matrices, hashtables? Just serialize! public class UserData implements java.io.Serializable

School of Information Technologies 35 Loopback interface Use for local testing Run server and client on same host or symbolic constant localhost Hint: Select high port numbers for testing (above 10000)

School of Information Technologies 36 Summary Two programming models: C/S and p-2-p Each has its own pros and cons Sockets are used for any network communications Java provides both TCP and UDP sockets APIs Next: Multithreading and timers