L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.

Slides:



Advertisements
Similar presentations
Network Programming and Java Sockets
Advertisements

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.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
1 Network Layers Application Transport Network Data-Link Physical bits.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
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.
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- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
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.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
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.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Chapter 2 Applications and Layered Architectures Sockets.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
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.
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.
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.
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 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.
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
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.
Network Programming. These days almost all devices.
Distributed Systems1 Socket API  The socket API is an Interprocess Communication (IPC) programming interface originally provided as part of the Berkeley.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Object-Orientated Analysis, Design and Programming
Network Programming Introduction
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
CSCD 330 Network Programming
Socket Programming.
CSCD 330 Network Programming
Java Socket Programming
Socket Programming 2: Application Layer.
Chapter 2: Application layer
Socket Programming with UDP
Presentation transcript:

L 2 - 1 3( 1/ 20) : Java Network Programming

The Socket API The previous contents describe client-server interaction that application programs use when they communicate across an internet. Here, we show how clients and servers use transport protocols to communicate, and gives two complete programs for both client and server. API: Application Program Interface. -is the interface that an application uses when it interacts with transport protocol software. Although protocol standards allow OS designers to choose an API, many have adopted the socket API. Originally designed as part of the BSD UNIX OS that contained TCP/IP internet working protocol. Instead of modifying their basic OS, vendors created a socket library that provides the socket API.

The JAVA Socket API Java’s Socket class, which is used by both clients and servers, has methods that correspond to the first four of these operations. The last three operations are only needed by servers that need to wait for clients to connect to them. They are implemented by the ServerSocket class. The Java 2 platform API specification can be found from the link below: Java 2 Platform API Specification Sockets shield the programmer from low-level details, like media types, network address, … A socket can perform seven basic operations: connect to a remote machine send data receive data close a connection bind to a port listen for incoming data accept connection from remote machine on the bound port

The Socket/ServerSocket/DatagramSocket Class (in Java) Constructors : many constructors are available but some are introduced below. public Socket(String host, int port) Creates a stream socket and connects it to the specified port number on the named host. public Socket(InetAddress host, int port) Creates a stream socket and connects it to the specified port number at the specified IP address. public ServerSocket(int port) Creates a server socket on a specified port. public ServerSocket(int port, int backlog) Creates a server socket and binds it to the specified local port number. public DatagramSocket() Constructs a datagram socket and binds it to any available port on the local host machine. public DatagramSocket(int port) Constructs a datagram socket and binds it to the specified port on the local host machine.

The Socket Class methods Methods : many methods are available for use but some are introduced below. Socket theSocket = new Socket(“ 80); // for example public int getInetAddress() Returns the address to which the socket is connected. e.g. InetAddress host = theSocket.getInetAddress(); public int getPort() Returns the remote port to which this socket is connected. e.g. int port = theSocket.getPort(); public InputStream getInputStream() Returns an input stream for this socket. e.g. InputStream input = theSocket.getInputStream(); public OutputStream getOutputStream() Returns an output stream for this socket. e.g. OutputStream output = theSocket.getOutputStream(); public void close() close this socket. e.g. theSocket.close();

The ServerSocket/DatagramSocket Class methods (in Java) Methods : many methods are available for use but some are introduced below. public Socket accept() Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. e.g. ServerSocket server_socket = new ServerSocket(); Socket connection = server_socket.accept(); public void receive(DatagramPacket p) Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine. e.g. DatagramSocket DSocket = new DatagramSocket(5000); DatagramPacket receivePacket = new DatagramPacket(data, data.length); DSocket.receive(receivePacket); public void send(DatagramPacket p) Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host. e.g. DatagramPacket sendPacket = new DatagramPacket(data, data.length); DSocket.send(sendPacket);

Example: Stream Socket Connections (TCP: transmission control protocol ) The basic life cycle of a server is: ServerTCP.java ServerTCP.java A creation of a ServerSocket -- ServerSocket server = new ServerSocket(5000,100); Listen for incoming connection -- Socket connection = server.accept(); Receive data -- DataInputStream input = new DataInputStream(connection.getInputStream()); input.readUTF(); Send data -- DataOutputStream output = new DataOutputStream(connection.getOutputStream()); output.writeUTF(“connected!”); Close the connection -- connection.close(); Wait for the next connection. What a client does: ClientTCP.java ClientTCP.java Connect to a remote machine -- Socket client = new Socket(InetAddress.getLocalHost(),5000); Receive data -- DataInputStream input = new DataInputStream(client.getInputStream()); input.readUTF(); Send data -- DataOutputStream output = new DataOutputStream(client.getOutputStream()); output.writeUTF(“Thanks!”); Close a connection -- client.close(); Server Client 5000 Input.readUTF( ) Output.writeUTF Input.readUTF( ) Socket client Socket connection CloseWindowAndExit.java

Example: Connectionless Transmission with Datagrams (UDP: User Datagram Protocol ) The basic life cycle of a server is: ServerUDP.java ServerUDP.java A creation of a DatagramSocket -- DatagramSocket server_socket = new DatagramSocket(5000); Set up packet -- byte data[] = new byte[100]; DatagramPacket receivePacket = new DatagramPAcket(data, data.length); Wait for packet -- server_socket.receive(receivePacket); Process packet -- receivePacket.getAddess(), receivePacket.getPort(), (display or output received packet) receivePacket.getLength(), receivPacket.getData() Send packet back to client -- DatagramPacket sendPacket = new DatagramPacket(……); server_socket.send(sendPacket); Wait for next packet …… What a client does is similar to the server: ClientUDP.java ClientUDP.java Create a Datagrame socket -- DatagramSocket client_socket = new DatagramSocket(); Set up packet -- byte data[] = new byte[100]; receivePacket = new DatagramPacket(data, data.length); Wait for packet -- client_socket.receive(receivePacket); Process packet -- receivePacket.getAddess(), receivePacket.getPort(), (display or output received packet) receivePacket.getLength(), receivPacket.getData() Send packet back to client -- DatagramPacket sendPacket = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5000); client_socket.send(); Wait for next packet …… Server Client 5000 client_socket.send(sendPacket) client_socket.receive(receivPacket)server_socket.send(sendPacket) server_socket.receive(receivPacket) ? ?=receivePacket.getPort() CloseWindowAndExit.java

Exercises Ex1. Run server and client programs based on TCP and modify the server program so that you can output the internet address and port number the server is connected to. Ex2. (optional?) Modify server and client programs so that they can continue to communications without disconnection. Ex3. (optional?) Add “disconnection” button. When pressing the button, the connection between the server and client is disconnected. You can get the source files: ServerTCP.java ClientTCP.javaServerTCP.javaClientTCP.java