티스토리 뷰

 

 

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);
    }
  }
}








댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함