SSブログ

DISCARD(廃棄)サーバ [プログラム三昧]このエントリーを含むはてなブックマーク#

古くなって捨てられていたコンピュータを再生した話ではありません。

私のDISCARD(破棄)サーバ

某所での実験を追試しようとしましたが、通信相手のDISCARD(破棄)サーバというのを作成しなくてはならないことがわかりました。 どうも、データをどんどん受信してしまい、自分からは何も出さないサーバのことらしいです。 ちょいと、作ってしまおう。

/*
 * --------------------------------------------
 * File         : TcpDiscardServer.java
 * Package      : org.noritan.tcpdiscard
 * Copyright    : Copyright (c) 2008 noritan.org
 * Organization : noritan.org
 * Created      : 2008/09/28
 * --------------------------------------------
 */
package org.noritan.tcpdiscard;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * This TcpDiscardServer class is a so-called DISCARD
 * server.  All received data will be discarded.
 * 
 * @author noritan
 */
public class TcpDiscardServer implements Runnable {

  private int port;

  /**
   * Construct a TcpDiscardServer object with
   * a specified port numer.
   *
   * @param pot
   *   Port number to wait for a requests.
   */
  public TcpDiscardServer(int port) {
    this.port = port;
  }

  /**
   * Server process content.
   * Served as a single thread DISCARD server.
   * 
   * @see java.lang.Runnable#run()
   */
  @Override
  public void run() {
    try {
      ServerSocket server = new ServerSocket(port);
      try {
      for (;;) {
        Socket socket = server.accept();
        System.out.println(
          "Connected by " + socket.getInetAddress()
        );
        System.out.println("SEND BUFFER="+socket.getSendBufferSize());
        try {
            discard(socket.getInputStream());
        } catch (IOException ex) {
            System.out.println("Failed to get streams");
        } finally {
            try {
                socket.close();
            } catch (IOException ex) {
                // do nothing for close
            }
            System.out.println("Socket closed");
        }
      }
    } catch (IOException ex) {
      System.out.println("Failed to accept a socket");
      ex.printStackTrace();
    } finally {
      try {
        server.close();
      } catch (IOException ex) {
        // do nothing for close.
      }
    }
    } catch (IOException ex) {
      System.out.println("Failed to open a server");
      ex.printStackTrace();
    }
  }

  /**
   * Discards all data at the inputStream
   * 
   * @param inputStream
   *   InputStream arrives data to be discarded.
   */
  private void discard(InputStream inputStream) {
    long count = 0;
    try {
      while (inputStream.read() >= 0) {
        count++;
      }
    } catch (IOException ex) {
      System.out.println("Error on InputStream");
      ex.printStackTrace();
    }
    System.out.println("RECEIVED=" + count);
  }

  /**
   * A main method as an application.
   * 
   * @param args
   *   Command line option not in use.
   */
  public static void main(String[] args) {
    new Thread(new TcpDiscardServer(30049)).start();
  }
}

横着をしているので、クライアント一台にしか対応していません。 通信の終了時には、総受信バイト数だけを表示します。


nice!(0)  コメント(1)  トラックバック(0)  このエントリーを含むはてなブックマーク#

nice! 0

コメント 1

hamayan

正直linuxマシンでも用意して、DISCARDとかDAYTIMEとかECHOとかのサービスを許可する方が良いんですねれどネ。

あ!、あといずれnmapとかも使う事になるのかもしれませんね。これは自作できない。

by hamayan (2008-09-28 21:19) 

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

トラックバックの受付は締め切りました