Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.

Slides:



Advertisements
Similar presentations
Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
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.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Java Sockets Source:
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
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
28-Jun-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
Appendix F: Network Programming in Java ©SoftMoore ConsultingSlide 1.
Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
DBI Representation and Management of Data on the Internet.
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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
CS 11 java track: lecture 6 This week: networking basics Sockets Vectors parsing strings.
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.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
The InetAddress Class A class for storing and managing internet addresses (both as IP numbers and as names). The are no constructors but “class factory”
Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
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.
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.
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.
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.
URL Programming Mimi Opkins CECS277. What is a URL?  URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 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.
Advance Computer Programming Networking Basics – explores the java.net package which provides support for networking. – Also Called “programming for the.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Source: Java Sockets Source:
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
NETWORK PROGRAMMING CNET 441
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Java Network Programming
„Networking”.
CSCD 330 Network Programming
Sockets and URLs 13-Nov-18.
Basic Protocols 24-Nov-18.
Networking.
Sockets and URLs 3-Dec-18.
Basic Protocols 19-Feb-19.
CSCD 330 Network Programming
Programming TCP Sockets
Uniform Resource Locator: URL
Programming TCP Clients
Exceptions and networking
Presentation transcript:

Programming TCP Clients

InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in the IPV4. The InetAddress class is an object which store such type of data and has some functions to get the host name from the IP number or vice-versa It has no constructors, but an object factory. A new object of this class is created with –InetAddress n = InetAddress.getLocalHost() –InetAddress n = InetAddress.getByName(nombre) Following methods can be applied: –String nombre = n.getHostName() –String direccion = n.getHostAddress() See InetExample.java & Names.java

PURL URL = Uniform Resource Locator It is used to connect to a web server and donload the resources it offers The URL class

The URL A URL is a UNIFORM RESOURCE LOCATOR. It is a unique address of a resource a web server has released in the Internet. A URL consists of 3 principal components: – the protocol descriptor. The most used may be http which stands for HyperText Tranfer Protocol but there is also a File Transfer protocol (ftp), Gopher, File o News. – The hostname of the maschine which “serves” the resource –The name of the resource in that maschine With Java, it is possible to open an URL and read its content as it were stored in a file. For that, it is necessary to create an URL object with at least the 3 components named above.

The URL Constructors for a URL object: –URL aRUL = new URL(“ –URL aURL = new URL(“http”;” –URL aURL = new URL(“http”,” While creating a URL object an exception of the MalformedURLException class may be generated. Because of this, some measurements must be taken (use a try-catch) try { URL miURL = new URL(....); } catch(MalFormedURLException e) { // code for reacting to the exception } Methods available for objects of the : getProtocol(), getHost(), getPort(), getFile(), openConnection()

The URL If we know beforehand that the content of a URL is text, we can retrieve it with the following piece of program: import java.net.*; import java,io.*; public class LeerURL { public static void main(String args[]) { try { URL miURL = new URL(“ URLConnection c = miURL.openConnection(); BufferedReader in = new BufferedReader ( new InputStreamReader(c.getInputStream())); String line; while ((line = in.readLine() != null) System.out.prinln(line); c.close(); catch(MalFormedURLException e) { System.out.pritnln(e);} }

The TCP Client’s Socket A socket is an abstraction representing one end in a communication link between two programs. A socket is always bound to port (although sometimes this will be not evident for the programmer). The port is the way an application identifies itself with the TCP/IP layer A server which runs on a server is associated to certain port. This means it listen at a certain to requests from the clients whishing to establish a communication. In order to establish a communication for a client it is necessary for it to previously know 1- the port number, 2- the host address. Having this information the client tries a rendezvous where the client is already running and listening.

TCP Client in Java (1) For trying a rendezvous in Java (with a TCP server, which could have been written in any language) we must create an object of the Socket class Socket calling; String host = “a_host_address”; calling = new Socket(host,7); A host address can be given as an IP number or name: dichato.dcc.uchile.cl or In the first case Java will do the DNS lookup first. The creation of a socket is a blocking statement. This means that the execution of the program will block until the creation returns a socket connected to the server or a null if it fails. A failure to create a socket may arise if there is no such host in the internet, if the host is not currently reachable, of if there is no server listening to that port on that host. The is a timeout for this sentence. A failure will throw an Exception. It is therefore necessary to program the socket creation within a try-and-catch block

TCP Client in Java (2) If the socket is created then we can open an inputStream and an ouputStream from that socket in order to read data from an write data into the server. PrintWriter out = new PrintWriter(calling.getOutputStream(), true); BufferedReader In = new BufferedReader(new InputStreamReader(calling.getInputStream())); getInputStream & getOutputStream open byte oriented data streams. Printwriter & BufferedReader are “filters” which convert bytes into text (string with end-of-line marks) and vice-versa. out.print(“hello”); out.println(“how are you ?”); String linea = in.readLine(); Print, println and readLine are blocking sentences. For using readLine we must be sure the server will send and eol mark. How can we program our own echo client ?.

The date server just waits until someone tries a rendezvous and answers with the current date of the server. Then the server breaks the communication (3). This means the client must try rendezvous (1) and then read the response of the server (2). Date server 13 Client A Client for the date server DateClient

The echo server waits until someone makes a rendezvous. The it reads “line-by-line” what the client sends and answers with the same. The connection must be broken by the client. A Client for the echo server Echo server 7 Client EchoClient

The server waits for a request, analyses it and then sends a (variable) set of lines. After that the server breakes the communication A finger client Finger server 7 Client 1 Finger [-l] [ 3 Finger

This protocol is described in the rfc 1939 (internet protocol descriptions) See A pop client for mail retrieving mail pop server 110 Client 1 cliente_pop \ 3 Cliente_pop

A SMTP client for mail sending mail SMTP server 25 Client 1 We will provide a graphic interface for sending Mails from a local computer ClienteSMTP

A generic client in Java ? import java.io.*; import java.net.*; public class Cliente { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; if (args.length != 2) System.out.println(“Use: java GClient ”); int nport = Integer.parseInt(args[1]); try { echoSocket = new Socket(args[0], nport); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: "+ args[0]); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: "+args[0]); System.exit(1); } > }

The Sockets constructors Socket(String host, int port) The port must be in the range 1-65,535 Socket(Inet Address host, int port) The same but with an InetAddress object as parameter Socket(String host, int port, String localHost, int localport) Every TCP communication consists of a local and remote host and a local and remote port. This constructor allows us to specify all them. Specifying local address makes only sense when local computer is multihomed (more than one address). Null = default address. Sometimes it is necessary to specify the local port (firewalls). 0 = the system will assign a random port number from the still available. Numbers from 1 to 1025 should not be used as they are reserved for “well known services” like echo, telnet finger, ftp.

More Socket methods in Java InetAddress getInetAddress() returns the IP address of the remote host to which the socket is connected int getPort() returns the port number to which the socket at the other extreme is bound InetAddress getLocalAddress() returns the IP address of the local host int getLocalPort() returns the port number to to which the socket is bound. void setSoTimeout(int timeout) sets timeout in milliseconds for a read operation on this socket. 0 = no timeout, this can block the operation indefinitely. If the reading operation is not completed in that time an InterruptedIOException is thrown int getSoTimeout() returns the timeout of the socket

More Socket methods in Java void setTcpNoDelay(boolean on) Disables/Enables using the Nagel’s algorithm which makes TCP more efficient by delaying the writing (sending) of small amounts of data until there is enough data to send. This may introduce some unacceptable delays for some applications. boolean getTcpNoDelay() returns whether the Nagel’s algorithm is working or not void setSoLinger(boolean on, int val) allows to set a linger time-out (in milliseconds). Linger is the time the socket communication remains “open” by the system after the program closes it. This will allow to receive packages for confirmation which are still delayed and avoid the using of the same port on the same machine for some 4 min. int getSoLinger () returns the current linger setting or –1 if not set. void setSendBufferSize(int size) int getSendBufferSize() void setReceiveBufferSize(int size) int getReceiveBufferSize() Sockets.java

Socket originated Exceptions Many of the Socket constructors and methods throw an exception. These instructions should be programmed inside a try-and-catch block Most of the thrown exceptions are objects from a subclass of the IOException class –BindException: the requested local port or address could not be used. Typically when the port is already used or it is a system port or the local address is not a valid one. –ConnectException: connection refused because there was no server listening to that port on the remote host. –NoRouteToHostException: remote host could not be reached typically because of network problems or a firewall UnknownHostException: the given host address is not valid (DNS Lookup filed)