[IT]/컴퓨터
[JAVA][예제] Daytime 프로토콜 예제 - Beginning Java Networking
jamesku
2012. 9. 29. 23:54
Beginning Java Networking
Chad Darby, www.wrox.com
표준 인터넷 프로토콜을 이용한 클라이언트 예제
// DaytimeClient import java.net.*; import java.io.*; public class DaytimeClient { public static void main(String[] args) { String sHostName; // Get the name of the server from the command line. // No entry, use tock.usno.navy.mil if (args.length > 0) { sHostName = args[0]; } else { sHostName = "tock.usno.navy.mil"; } try { // Open a socket to Port 13. // Prepare to receive the Daytime information. Socket oSocket = new Socket(sHostName, 13); InputStream oTimeStream = oSocket.getInputStream(); StringBuffer oTime = new StringBuffer(); int iCharacter; // Fetch the Daytime information. while ((iCharacter = oTimeStream.read()) != -1) { oTime.append((char) iCharacter); } // Convert Daytime to a string and output. String sTime = oTime.toString().trim(); System.out.println("It is " + sTime + " at " + sHostName + "."); // Close the stream and the socket. oTimeStream.close(); oSocket.close(); } catch(UnknownHostException e) { System.err.println(e); } catch(IOException e) { System.err.println(e); } } }