O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Contacting a Server

String serverName = "www.timothyfisher.com";
											Socket sock = new Socket(serverName, 80);

In this phrase, we connect to a server via TCP/IP using Java’s Socket class. During construction of the sock instance in our phrase, a socket connection will be made to the server specified by serverName—in this case, www.timothyfisher.com and port 80.

Whenever a Socket is created, you must be sure to close the socket when you are finished with it by calling the close() method on the Socket instance you are working with.

Java supports other ways of connecting to a server that we won’t discuss details of here. For example, you could use the URL class to open a URL and read from it. See the phrase “Reading a Web Page via HTTP” in this chapter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required