Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”

Slides:



Advertisements
Similar presentations
Referring to Java API Specifications
Advertisements

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.
Network Programming Chapter 11 Lecture 6. Networks.
Java Sockets Source:
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
1 Java Networking – Part I CS , Spring 2008/9.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
System Programming Practical session 10 Java sockets.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Java sockets. From waiting.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
1 Java Networking – part I CS , Winter 2007/8.
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
Network Programming in Java David Meredith Aalborg University.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
DBI Representation and Management of Data on the Internet.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
Servlet Communication Other Servlets, HTML pages, objects shared among servlets on same server Servlets on another server with HTTP request of the other.
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 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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
NETWORK PROGRAMMING.
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.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols,
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
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.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
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 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.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
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.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
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.
Working with URLs CSIE, National University of Tainan.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
Source: Java Sockets Source:
MCA – 405 Elective –I (A) Java Programming & Technology
Networking with Java 2.
Network Programming Introduction
„Networking”.
CSI 4118 – UNIVERSITY OF OTTAWA
CSCD 330 Network Programming
CSCD 330 Network Programming
Socket Programming 2: Application Layer.
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”

Networking Networking Basics Usually computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP): When you write Java programs that communicate over the network, you are programming at the application layer. Typically, you don't need to concern yourself with the TCP and UDP layers. Instead, you can use the classes in the java.net package. These classes provide system-independent network communication.

Networking TCP When two applications want to communicate to each other reliably, they establish a connection and send data back and forth over that connection. TCP guarantees that data sent from one end of the connection actually gets to the other end and in the same order it was sent. Otherwise, an error is reported. TCP provides a point-to-point channel for applications that require reliable communications. TCP (Transmission Control Protocol) is a connection-based protocol that provides a reliable flow of data between two computers.

Networking UDP The UDP protocol provides for communication that is not guaranteed between two applications on the network. UDP sends independent packets of data, called datagrams, from one application to another. Many firewalls and routers have been configured not to allow UDP packets. Sometimes reliability provided by TCP is not necessary (eg. Time Server) and you can use UDP which doesn’t have overhead related to establishing connection.

Networking Ports Generally a computer has a single physical connection to the network. All data destined for a particular computer arrives through that connection. Data transmitted over the Internet is accompanied by addressing information that identifies the computer and the port for which it is destined. Ports are identified by a 16-bit number, which TCP and UDP use to deliver the data to the right application.

Networking URL URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. The resource name is the complete address to the resource. The format of the resource name depends entirely on the protocol used. Creating a URL (everything is an object !): URL DMCS = new URL(" Relative URL: URL indexDMCS = new URL(DMCS,"/index.html"); Other constructors: URL("http", " URL("http", 80, "

Networking MalformedURLException Each of the four URL constructors throws a MalformedURLException if the arguments to the constructor refer to a null or unknown protocol. try { URL myURL = new URL(...) } catch (MalformedURLException e) {... // exception handler code... } URLs are "write-once" objects. Once you've created a URL object, you cannot change any of its attributes (protocol, host name, filename, or port number).

Networking Reading from URL import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL(" BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); //HTML from yahoo in.close(); } }

Networking Connecting to a URL When you connect to a URL, you are initializing a communication link between your Java program and the URL over the network. try { URL yahoo = new URL(" URLConnection yahooConnection = yahoo.openConnection(); } catch (MalformedURLException e) { // new URL() failed... } catch (IOException e) { // openConnection() failed... }

Networking Reading from URLConnection import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL yahoo = new URL(" URLConnection yc = yahoo.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }

Networking Reading from URL versus reading from URLConnection You can use either way to read from a URL. Reading from a URLConnection instead of reading directly from a URL might be more useful. This is because you can use the URLConnection object for other tasks (like writing to the URL) at the same time.

Networking Writing to a URLConnection To write to an URL you have to do following steps: 1.Create a URL. 2.Open a connection to the URL. 3.Set output capability on the URLConnection. 4.Get an output stream from the connection. This output stream is connected to the standard input stream of the cgi-bin (eg.) script on the server. 5.Write to the output stream. 6.Close the output stream.

Networking Writing to a URLConnection(2)... String stringToReverse = URLEncoder.encode(args[0]); URL url = new URL(" URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter( connection.getOutputStream()); out.println("string=" + stringToReverse); //backward script waits for data in the above form out.close();...

Networking Sockets A socket is one endpoint of a two-way communication link between two programs running on the network. Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request. On the client-side: The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to rendezvous with the server on the server's machine and port.

Networking Sockets(2) If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.

Networking Reading from and writing to a Socket To do is you should perform five basic steps: 1.Open a socket. 2.Open an input stream and output stream to the socket. 3.Read from and write to the stream according to the server's protocol. 4.Close the streams. 5.Close the socket.

Networking Reading from and writing to a Socket - example import java.io.*; import java.net.*; //Socket public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; Verte 

Networking Reading... (2) try { echoSocket = new Socket(„comp", 7); //Echo Server port out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: comp."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: comp."); System.exit(1); } Verte 

Networking Reading... (3) BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); //The precedence is important. You should first close all streams // connected to the socket and than socket itself. }

Networking Reading from URL - socket version import java.net.*; import java.io. String hostname = " int port = 80; String filename = "/index.html"; Socket s = new Socket(hostname, port); InputStream sin = s.getInputStream(); BufferedReader fromServer = new BufferedReader(new InputStreamReader(sin)); OutputStream sout = s.getOutputStream(); PrintWriter toServer = new PrintWriter(new OutputStreamWriter(sout)); verte-->

Networking Reading from URL - socket version(2) toServer.print("GET " + filename + " HTTP/1.0\n\n"); toServer.flush(); for(String l = null; (l = fromServer.readLine()) != null; ) System.out.println(l); toServer.close(); fromServer.close(); s.close();

Networking Server Side of Socket Creating a server socket: try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port: 4444"); System.exit(-1); }

Networking Server Side of Socket(2) If the server successfully connects to its port, then the ServerSocket object is successfully created and the server continues to the next step - accepting a connection from a client: Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) {// accept failed} The accept method waits until a client starts up and requests a connection on the host and port of this server. When a connection is requested and successfully established, the accept method returns a new Socket object which is bound to a new port.

Networking Server Side of Socket(3) After the server successfully establishes a connection with a client, it communicates with the client: PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));

Networking Supporting multiple clients Server can service clients simultaneously through the use of threads - one thread per each client connection. The basic flow of logic in such a server is this: while (true) { accept a connection ; create a thread to deal with the client ; }

Networking Datagrams A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed. The java.net package contains classes to help you write Java programs that use datagrams to send and receive packets over the network: DatagramSocket DatagramPacket

Networking Sending Datagrams... byte[] buf = new byte[256]; InetAddress address = InetAddress.getByName(„ DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445); socket.send(packet);...

Networking Receiving and sending Datagrams... Datagram Socket socket = new DatagramSocket(4445); byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet);...