package j5;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
// telnetの原形となるプログラムT1.java
// このプログラムは,指定されたアドレスのポートに標準入出力を接続します
// 使い方 java j5.T1 サーバアドレス ポート番号
// 起動の例 java j5.T1 localhost 6000
// 接続後、サーバから接続を切るには quit を送信してください
// 終了にはコントロールC を入力してください
// ライブラリの利用
import java.net.Socket;
import java.net.UnknownHostException;
// T1クラス
// T1クラスは,ネットワーク接続の管理を行います
// StreamConnectorクラスを用いてスレッド処理を行います
public class T1 {
// ソケットの準備
protected Socket serverSocket;// 接続用ソケット
public OutputStream serverOutput;// ネットワーク出力用ストリーム
public BufferedInputStream serverInput;// 同入力用ストリーム
// openConnectionメソッド
// アドレスとポート番号からソケットを作りストリームを作成します
public void openConnection(String host, int port)
throws IOException, UnknownHostException {
serverSocket = new Socket(host, port);
serverOutput = serverSocket.getOutputStream();
serverInput = new BufferedInputStream(serverSocket.getInputStream());
}
// main_procメソッド
// ネットワークとのやりとりをするスレッドをスタートさせます
public void main_proc()
throws IOException {
try {
// スレッド用クラスStreamConnectorのオブジェクトを生成します
StreamConnector stdin_to_socket = new StreamConnector(System.in, serverOutput);
StreamConnector socket_to_stdout = new StreamConnector(serverInput, System.out);
// スレッドを生成します
Thread input_thread = new Thread(stdin_to_socket);
Thread output_thread = new Thread(socket_to_stdout);
// スレッドを起動します
input_thread.start();
output_thread.start();
} catch (Exception e) {
System.err.print(e);
System.exit(1);
}
}
// mainメソッド
// TCP コネクションを開いて処理を開始します
public static void main(String[] args) {
try {
T1 t = null;
t = new T1();
// 引数の指定がなかった場合はlocalhost
if (args.length == 0) args = new String[] { "localhost" ,"6000"};
t.openConnection(args[0], Integer.parseInt(args[1]));
t.main_proc();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
// StreamConnectorクラス
// ストリームを受け取り,両者を結合してデータを受け渡します
// StreamConnectorクラスはスレッドを構成するためのクラスです
class StreamConnector implements Runnable {
InputStream src = null;
OutputStream dist = null;
// コンストラクタ入出力ストリームを受け取ります
public StreamConnector(InputStream in, OutputStream out) {
src = in;
dist = out;
}
// 処理の本体
// ストリームの読み書きを無限に繰り返します
public void run() {
byte[] buff = new byte[1024];
while (true) {
try {
int n = src.read(buff);
if (n > 0)
dist.write(buff, 0, n);
} catch (Exception e) {
e.printStackTrace();
System.err.print(e);
System.exit(1);
}
}
}
}