Sockets

In this worksheet, you will begin to explore sockets and network programming.

  1. What server listens at port 22? What port does HTTP use?
  2. Open a telnet session to http://www.cnn.com and type GET / following by return. What happens? What is all that stuff?
  3. List the 5 pieces of information that make a socket connection unique? What 3 pieces of information do you need to write code to make a socket connection to a machine?
  4. Why would you want to ever connect to a socket on the same machine?
  5. Write a simple Java client that mimics what you did with telnet above. It should open a socket to the HTTP port on machine www.cnn.com and then write "GET /\n" to the machine. Read the response until the channel is closed.
  6. Use the java.net.URL object to test whether a URL is valid (i.e., type in some good ones and some incorrect ones). It will throw MalformedURLException if necessary.
  7. Use java.net.URLConnection to re-implement the simple Java client above. There are methods like connect() and getContent() that you will find useful.