13.07.2015 Views

Lab #3: Introduction to Java Sockets - PoliformaT - UPV

Lab #3: Introduction to Java Sockets - PoliformaT - UPV

Lab #3: Introduction to Java Sockets - PoliformaT - UPV

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

P3­4 Computer Networks <strong>Lab</strong>ora<strong>to</strong>ry3.How <strong>to</strong> obtain information about the establishedconnection?The Socket class has several methods that allow <strong>to</strong> obtain data onthe connection established between the client and the server. Among themwe can mention getLocalAddress() and getInetAddress(), thatreturns the IP local and remote address, respectively, and getLocalPort()and getPort(), that returns the local and remote ports, respectively. Next,the mentioned methods are detailed briefly. You can consult moreinformation of them in the Sun Webpage, that is referenced at the beginningof the practice• public int getPort(): it returns the remote port <strong>to</strong> whichSocket is connected.• public InetAddress getInetAddress(): it returns remote IPaddress of which Socket is connected.• public int getLocalPort(): it returns the local port <strong>to</strong> whichthe Socket is associated.• public InetAddress getLocalAddress(): It returns the localIP address local <strong>to</strong> which the Socket is associated.Exercise 3:Modify the client “ClientTCP2.java” of the previous exercise in order <strong>to</strong>show the information in the screen of the client on the established connection(local and remote IP address and port numbers).Test it four times in a row, connect with the “zoltar.redes.upv.es” server inport 25 and verify which values are modified. Interpret the obtained result.


<strong>Lab</strong> <strong>#3</strong>: <strong>Introduction</strong> <strong>to</strong> <strong>Java</strong> <strong>Sockets</strong> P3­54.How read the received data trough the connection?In order <strong>to</strong> read the data that are being received through the Socketwe will use the method getInputStream of the Socket class. Thismethod returns an object of the InputStream type (flow of input bytes),which adjusts correctly <strong>to</strong> the TCP philosophy of transmission oriented <strong>to</strong>continuous flow of data. However applications usually need <strong>to</strong> exchangelines of text.The Scanner class is a “bridge” from the flows of bytes <strong>to</strong> those ofcharacter lines: read bytes and encodes them in<strong>to</strong> characters. Data can thenbe read using several methods that make programmers life easier like:Scanner input = new Scanner(s.getInputStream());where “s” is the Socket object defined previously.When a program reads from a Scanner, the text is extracted from abuffer instead of acessing directly <strong>to</strong> the input stream. When the bufferbecomes empty, it is filled again with as much text as it is possible, eventhough not everything be needed. This will cause that the future readings becarried out more quickly.In our case, using the method nextLine() of the Scanner class wewill be able <strong>to</strong> read the answers of the server as complete text lines. Thismethod reads a line of text and it returns it as a String.Exercise 4:Rename the client “ClientTCP2.java” of the previous exercise as“ClientSMTP.java”. Modify it in such a way it is always connect <strong>to</strong> theport 25 and shows the first line of text received from the server.5.How <strong>to</strong> send data through the connectionTo write through a Socket also is more efficient <strong>to</strong> use a class thatprovides certain capacity of s<strong>to</strong>rage (buffering). For this reason, <strong>to</strong> writethrough a Socket, we will use an object of the classjava.io.PrintWriter connected <strong>to</strong> the output flow of the Socket. This


<strong>Lab</strong> <strong>#3</strong>: <strong>Introduction</strong> <strong>to</strong> <strong>Java</strong> <strong>Sockets</strong> P3­7the efficiency, output stream tries <strong>to</strong> fill its buffer as much as it is possiblebefore sending the data, but as the client does not have more data <strong>to</strong> send (atthe moment) its request will never be sent.The solution <strong>to</strong> this problem comes from the method flush() of thePrintWriter class. This method forces <strong>to</strong> the data be sent although thebuffer is still not full. In case of doubt about if it is or not necessary <strong>to</strong> use it,it is better <strong>to</strong> invoke it, since doing a unnecessary flush consumes fewresources, but <strong>to</strong> not use it when it is needed can produce blocking situationsin the program.Exercise 6:Modify the client SMTP so it uses the method flush and verify that now itworks correctly.We can empty the buffer in an au<strong>to</strong>matic manner when writing on it(without having <strong>to</strong> use explicitly the method flush). For it we needed twothings:• The construc<strong>to</strong>r of the PrintWriter class must be used as it hasbeen used before, with a second additional parameter equal <strong>to</strong> true.• The writing must be done by the method println(), instead of themethod print. In addition the method println adds the end ofline then we will not need <strong>to</strong> write it as part of the order <strong>to</strong> be send <strong>to</strong>the client (unlike as we have done in the previous program).We are going <strong>to</strong> modify our client <strong>to</strong> verify this operation.Ejercicio 7:Modify the SMTP client <strong>to</strong> use the method println instead of print(remove what is not needed, like the sequence \r\n and flush). Verify thatit works returning <strong>to</strong> establish connection with the zoltar server in port 25.


<strong>Lab</strong> <strong>#3</strong>: <strong>Introduction</strong> <strong>to</strong> <strong>Java</strong> <strong>Sockets</strong> P3­9allows us <strong>to</strong> connect <strong>to</strong> us, means that there is a server listening in it. If itrejects the connection, then the port is closed. In this case of UPD, theexploration is not <strong>to</strong>o easy, because it is a non connected service, thereforeif we don't receive an answer we will don't what had happened with ourmessage.In order <strong>to</strong> create the explorer of ports we are going use the Socketclass, that already we have already used in the previous exercises. Since wehave seen, its construc<strong>to</strong>r can send two different exceptions:• UnknownHostException, is generated when the name of thecomputer with that we want <strong>to</strong> create the connection, cannot be solved <strong>to</strong>an IP address.• IOException, is generated when the connection cannot be establishedby any other reason, like for example that there is not a server listeningin the specified port in the parameters of Socket.We will use this second exception <strong>to</strong> create the explorer of ports, so ifwhile we sweep the ports of our equipment generates this exception, we willknow that the port is closed. If it is not generated, then there is a server inthat port. As the habitual thing is that most of the ports be closed, ourexplorer must show on the screen a message only when an open port isfound. On the contrary, usually it leaves so many messages then it will bedifficult <strong>to</strong> find out all the open ports.Exercise 8:Type and complete the following explorer of ports. Test it. Which ports areopen? Which services corresponds <strong>to</strong> those ports? (in /etc/services thereis a detailed listing of the services ordered by number of port).import java.net.*;import java.io.*;public class LowPortScanner {public static void main(String[] args) {String host = "localhost";for (int port = 1; port < 1024; port ++) {try {// COMPLETE THE CODE}


P3­10 Computer Networks <strong>Lab</strong>ora<strong>to</strong>rycatch (UnknownHostException e) {// COMPLETE THE CODE}catch (IOException e) {// COMPLETE THE CODE}} // end for} // end main} // end PortScannerExercise 9:Remove from your computer all the files you have created during thispractice.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!