Programming TCP Clients Inet Address Class An IP

  • Slides: 20
Download presentation
Programming TCP Clients

Programming TCP Clients

Inet. Address Class • An IP address identifies uniquely a host in the internet,

Inet. Address Class • An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in the IPV 4. The Inet. Address 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 – Inet. Address n = Inet. Address. get. Local. Host() – Inet. Address n = Inet. Address. get. By. Name(nombre) • Following methods can be applied: – String nombre = n. get. Host. Name() – String direccion = n. get. Host. Address() • See Inet. Example. java & Names. java

Host Addressing in TCP/IP Networks Network Address Host Address

Host Addressing in TCP/IP Networks Network Address Host Address

The URL class • URL = Uniform Resource Locator • It is used to

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

The URL • A URL is a UNIFORM RESOURCE LOCATOR. It is a unique

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 Hyper. Text 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 a. RUL = new URL(“http: //www.

The URL Constructors for a URL object: –URL a. RUL = new URL(“http: //www. arminco. com/index. html”); –URL a. URL = new URL(“http”; ”www. arminco. com”, ”index. html”); –URL a. URL = new URL(“http”, ”www. arminco. com”, 80, ”index. html”); • While creating a URL object an exception of the Malformed. URLException class may be generated. Because of this, some measurements must be taken (use a try-catch) try { } } URL mi. URL = new URL(. . ); catch(Mal. Formed. URLException e) { // code for reacting to the exception • Methods available for objects of the : get. Protocol(), get. Host(), get. Port(), get. File(), open. Connection()

The URL If we know beforehand that the content of a URL is text,

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 Leer. URL { public static void main(String args[]) { try { URL mi. URL = new URL(“http: //www. dcc. uchile. cl”); URLConnection c = mi. URL. open. Connection(); Buffered. Reader in = new Buffered. Reader ( new Input. Stream. Reader(c. get. Input. Stream())); String line; while ((line = in. read. Line() != null) System. out. prinln(line); c. close(); catch(Mal. Formed. URLException e) { System. out. pritnln(e); } } }

The TCP Client’s Socket • A socket is an abstraction representing one end in

The TCP Client’s Socket • A socket is an abstraction representing one end in a communication link between two programs. • When an application wants to send/receive data to/from the net it has to open one • A socket is always bound to a port (although sometimes this will be not evident for the programmer, especially if the program is a client one). The port is the way an application identifies a certain socket with the TCP/IP layer • A server which runs on a server is associated also opens a socket bound to a certain port and start listening to requests from the clients whishing to establish a communication. • In order to establish a communication a client has to previously know 1 - the port number, 2 - the host address. With this information the client tries a rendezvous where the server is already running and listening.

TCP Client in Java (1) • For trying a rendezvous in Java (with a

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(String host, int port) Socket csocket = new Socket(“hostname”, 7); • A host address can be given as an IP number or name: dichato. dcc. uchile. cl or 192. 24. 80. 40. 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. There is a timeout (default) for this instruction. • A failure will throw a checked 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

TCP Client in Java (2) • If the socket is created then we can open an input. Stream and an ouput. Stream from that socket in order to read data from an write data into the server. Print. Writer out = new Print. Writer(csocket. get. Output. Stream(), true); Buffered. Reader In = new Buffered. Reader(new Input. Stream. Reader(calling. get. Input. Stream())); • Both get. Input. Stream & get. Output. Stream open byte oriented data streams. Printwriter & Buffered. Reader 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. read. Line(); • read. Line is a blocking sentence. For using it we must be sure the server will send an eol mark.

A Client for the date server Protocol: The date server just waits until someone

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

A Client for the echo server Protocol: The echo server waits until someone makes

A Client for the echo server Protocol: The echo server waits until someone makes a rendezvous. Then, it reads “line-by-line” what the client sends and answers with the same. The connection must be broken by the client. So the client must 1) try a rendezvous, 2) send a line, 3) read the answer. Then repeat 2 & 3 until the client user brakes the dialogue 2 3 Echo server 7 1 Echo. Client

A finger client The server waits for a request, gets a command line from

A finger client The server waits for a request, gets a command line from the client, analyses it and then sends a (variable) set of lines, according to the query and the status. After that, the server breakes the communication. The client has to 1) try a rendezvous, 2) send the query string, 3) read lines until the communication is broken. Finger [-l] [<username>]@host 3 Finger server 7 1 Finger Client

A pop client for mail retrieving This protocol is described in the rfc 1939

A pop client for mail retrieving This protocol is described in the rfc 1939 (internet protocol descriptions) See http: //www. ietf. org/rfc. html cliente_pop <host> <username> <password> 3 mail pop server 110 1 Cliente_pop Client

A SMTP client for mail sending We will provide a graphic interface for sending

A SMTP client for mail sending We will provide a graphic interface for sending mails from a local computer to an open STMP server (most of them are open for computers inside a LAN. The protocol consists in a dialogue for identifying sender, receiver, subjet and body of the mail. 2 mail SMTP server 25 1 Cliente. SMTP Client

A generic client in Java ? import java. io. *; import java. net. *;

A generic client in Java ? import java. io. *; import java. net. *; public class Cliente { public static void main(String[] args) throws IOException { } } Socket echo. Socket = null; Print. Writer out = null; Buffered. Reader in = null; if (args. length != 2) System. out. println(“Use: java GClient <host> <port> ”); int nport = Integer. parse. Int(args[1]); try { echo. Socket = new Socket(args[0], nport); out = new Print. Writer(echo. Socket. get. Output. Stream(), true); in = new Buffered. Reader(new Input. Stream. Reader(echo. Socket. get. Input. Stream())); } catch (Unknown. Host. Exception 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); } <<Process protocol according to port number>>

The Sockets constructors • Socket(String host, int port) The port must be in the

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 Inet. Address object as parameter • Socket(String host, int port, String local. Host, 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 • Inet. Address get. Inet. Address() returns the IP

More Socket methods in Java • Inet. Address get. Inet. Address() returns the IP address of • • • the remote host to which the socket is connected int get. Port() returns the port number to which the socket at the other extreme is bound Inet. Address get. Local. Address() returns the IP address of the local host int get. Local. Port() returns the port number to to which the socket is bound. void set. So. Timeout(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 Interrupted. IOException is thrown int get. So. Timeout() returns the timeout of the socket

More Socket methods in Java • void set. Tcp. No. Delay(boolean on) Disables/Enables using

More Socket methods in Java • void set. Tcp. No. Delay(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 get. Tcp. No. Delay() returns whether the Nagel’s algorithm is working or not • void set. So. Linger(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 get. So. Linger () returns the current linger setting or – 1 if not set. • • void int set. Send. Buffer. Size(int size) get. Send. Buffer. Size() set. Receive. Buffer. Size(int size) get. Receive. Buffer. Size() Sockets. java

Socket originated Exceptions • Many of the Socket constructors and methods throw an exception.

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 –Bind. Exception: 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. –Connect. Exception: connection refused because there was no server listening to that port on the remote host. –No. Route. To. Host. Exception: remote host could not be reached typically because of network problems or a firewall • Unknown. Host. Exception: the given host address is not valid (DNS Lookup filed)