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 serverNamein 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 for more details on using the URL class.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net