import java.io.*;
import java.net.*; // networkを用いる
/*******************************************************************/
/* EchoClient */
/* 一度に1バイトを EchoServer に送り戻ってきた値などを表示する。 */
/*******************************************************************/
class EchoClient extends Thread
{
static final int ECHO_PORT = 7;
byte characterToSend;
int numBytes;
String serverName;
/* Constructor */
EchoClient(byte characterToSend, int numBytes, String serverName)
{
this.characterToSend = characterToSend;
this.serverName = serverName;
this.numBytes = numBytes;
System.out.println("characterToSend: " + characterToSend);
System.out.println(" numBytes: " + numBytes);
System.out.println(" serverName: " + serverName);
System.out.println(" echoData: ");
}
public void run()
{
Socket s = null;
try { // サーバーに接続する
s = new Socket(InetAddress.getByName(serverName), ECHO_PORT);
InputStream in = s.getInputStream(); // 入出力ストリーム
OutputStream out = s.getOutputStream(); // を作る
byte[] b = new byte[1];
b[0] = this.characterToSend;
for(int i = 0; i < numBytes; i++) { // 指定された数だけ
out.write(b); // 送り
in.read(b); // 受けとり
System.out.print(b[0] + ";"); // それを表示する
}
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
finally {
try {
s.close();
}
catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
/****************************************/
/* main */
/****************************************/
public static void main(String[] args)
{
byte characterToSend = (byte)'A';
String serverName = "localhost";
int numBytes = 128;
(new EchoClient(characterToSend, numBytes, serverName)).start();
}
}
|