1 / 18

Programming TCP Clients

Programming TCP Clients. Version 2008. 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 methods to get the host name and IP number

jesse
Download Presentation

Programming TCP Clients

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming TCP Clients Version 2008

  2. 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 methods to get the host name and IP number • 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

  3. Host Addressing in TCP/IP Networks Network Address Host Address

  4. 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 machine which “serves” the resource • The name of the resource in that machine • 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.

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

  6. PURL The URL • Constructors for a URL object: URL aRUL = new URL(“http://www.arminco.com/index.html”); URL aURL = new URL(“ftp”;”www.arminco.com”,”mails.html”); URL aURL = new URL(“http”,”www.arminco.com”,80,”index.html”); • 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 class : getProtocol(), getHost(), getPort(), getFile(), openConnection() • See PURL.java for an example

  7. The TCP Client’s Socket • TCP socket: an abstraction representing one end in a communication link between two programs. • When an application wants to send/receive data to/from a TCP/IP network it has to open one • Socket is always bound to a port (sometimes not evident for the programmer, of client application. • Port identifies a socket inside a computer • In order to establish a TCP/IP communication with a server, a client has to previously know 1- the port number, 2- the host address. With this information client creates socket to try a rendezvous with the server program already running.

  8. TCP Client in Java (1) • For trying a rendezvous in Java client we must create an object of the Socket class. Socket(String host, int port) Socket csocket = new Socket(“hostname”,7); • Host address 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, or 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

  9. TCP Client in Java (2) • After the socket is created we can open an InputStream and an OuputStream from that socket in order to read data from an write data into the server. • We will start with text-oriented information exchange (protocol). • We will use a PrintWriter to send (print) information and a BufferedReader to recieve (read) information PrintWriter out = new PrintWriter( csocket.getOutputStream(), true); BufferedReader In = new BufferedReader(new InputStreamReader(csocket.getInputStream())); out.print(“hello”); out.println(“how are you ?”); String linea = in.readLine(); • readLine is a blocking sentence. For using it we must be sure the server will send an eol mark.

  10. A Client for the date server Client 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. No data can be read anymore (the result would be null). 2- read data 1- connect 13

  11. 3 Examples DateClientBasic : This examples shows a basic date client with the basic instructions. Normally this only will work inside a local area network and will crash when something goes wrong. DateClinetProxy : has the necessary instructions to connect to the outside world using a proxy socks server. DateClient : an application as it would be programmed for real use: very flexible and checking everything Proxy socks server Server applicaiton Client application LAN

  12. A Client for the echo server Client Echo server EchoClient 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- write data 3- read answer 1- connect 7

  13. A pop client for mail retrieving Client mail pop server PopClientW.java The client first connects to the server on port 110. Then the client can “dialogue” with the server following the protocol described in the rfc 1939 (internet protocol descriptions) See http://www.ietf.org/rfc.html Go 2- follow protocol 1- Connect 110

  14. A SMTP client for mail sending Client mail SMTP server ClienteSMTP 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- follow protocol 1- Connect 25

  15. The Sockets constructors • Socket(String host, int port) The port must be in the range 1-65,535 • Socket(InetAddress host, int port)The same but with an InetAddress object as parameter • Socket(String host, int port, InetAddress 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). If null is given, default address is used. Sometimes it is necessary to specify the local port (firewalls). If 0 is given the system will assign a random available port number. Numbers from 1 to 1025 should not be used as they are reserved for “well known services” like echo, telnet finger, ftp.

  16. 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

  17. Sockets.java 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()

  18. Socket originated Exceptions • Many of the Socket constructors and methods throw a checked exception, mostly from a type extended from IOException. 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)

More Related