Comp 343/443 program 2: port prober The second program is a port prober. When started with the command line java prober ulam.math.luc.edu 2000 2100 then connection will be attempted to every port on ulam.math.luc.edu between 2000 and 2100. When a connection is accepted, the first 1K of data will be read from remotehost, if any. (That is, a read of up to 1K will be attempted; it may block after returning much less than that. You should, however, continue reading until a timeout occurs; the read() may simply have returned less than 1K because the smaller amount was immediately available.) You will need to implement timeouts both for the reading (because remotehost may not in fact be sending anything, depending on the network service in question) and for the connecting. Timeout values in the range 2000-4000 milliseconds can be used. 4000 milliseconds times 100 ports is, of course, 400 seconds, or seven minutes in the worst case, but most ports respond more promptly. Generally a connection is refused immediately, but if remotehost is unavailable or if a given port is blocked the connection timeout may be substantial. Print something for each port; nonactive port ranges can be summarized eg with ports 3489-3621: no connections Making connections with timeout is demonstrated in quikconnect.java. To do a read with timeout, the technique is similar but a two-step process. First, you set the socket timeout with s.setSoTimeout(timeoutval) (timeoutval is again in milliseconds); you need to catch SocketException but you can treat that as a fatal error. You can do this at any point in the program (eg within the copy(Socket to, Socket from) function, if you have one). To read data from a socket s, with or without timeout, you actually read from s.getInputStream(), of type InputStream. An example of this appears in tstalks.java, where s.getInputStream() is then converted to an InputStreamReader and then to a BufferedReader. If you've set s.setSoTimeout(), either before or after calling s.getInputStream(), then any input-related read operation on the inputstream (or its descendants) (eg readLine()) will, if nothing is received over the network during the timeout interval, throw the SocketTimeoutException exception. Note that this is a SocketTimeoutException even though the underlying socket is not evident and it looks like you're reading from a generic InputStream. You can catch a SocketTimeoutException from any read-type call on an InputStream that derived from a Socket, but this exception will never be *thrown* unless you have executed s.setSoTimeout(val) at some point. You do NOT need threads with this program; just check each port in turn. (If you were allowing *very* long timeouts, threads would allow ports to be queried in parallel, but that's not an issue here.) I would get started with the stalkc.java file (or tstalks.java), but copy the connection stuff (including the connect-with-timeout code) from quikconnect.java. Also note that stalkc.java creates a new connection and then *writes* data; portscan.java will create a new connection and then *read*. You do not need to do any fancy block-oriented reads; you can just use the InputStream.read() method. You first get the associated stream: InputStream is = sock.getInputStream(); At this point you can read and write, eg int readsize = is.read(buf); A readsize of -1 or 0 means you're done. So does a timeout. Note that it is the socket and not the InputStream that has the timeout interval set: sock.setSoTimeout();