ネットクライアント
URL url = getCodeBase( ); // アプレット自身の基底URL取得 String host = url.getHost( ); int port = 8005;
iimport java.applet.*; // Applet
import java.awt.*; // Font, TextField, TextArea, Label, Color, Button
import java.awt.event.*; // ActionListener, ActionEvent
import java.io.*; // InputStream, InputStreamReader, BufferedReader etc
import java.net.*; // Socket, URL, URLConnection, UnknownHostException etc
public class netClient extends Applet {
TextArea InputArea; // 内容入力エリア
Button SendButton; // 送信ボタン
// 初期化処理 -----------------------------------------------------------------------
public void init( ) {
setLayout(null); // 自由レイアウト設定
// 内容入力エリア
InputArea = new TextArea(2, 40); add(InputArea);
InputArea.setBounds(10, 10, 220, 70);
// 送信ボタン
SendButton = new Button("送信");
SendButton.setBounds(50, 100, 50, 20);
add(SendButton,null);
add(InputArea,null);
SendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
send_actionPerformed( e); } });
}
// 送信処理 ----------------
void send_actionPerformed(ActionEvent e) {
Socket socket = null;
PrintStream netoutput;
try {
URL url = getCodeBase( ); // アプレット自身の基底URL取得
String host = url.getHost( );
int port = 8005; // ポート 8005
try {
socket = new Socket(host, port); // ソケット
} catch (UnknownHostException e1) {
System.out.println("Not able to connect, sorry \n");
}
// ソケットから出力ストリームを作成
OutputStream outputstream = socket.getOutputStream( );
// 出力ストリームからデータ出力ストリーム作成
netoutput = new PrintStream(outputstream);
// 内容送信
netoutput.println(InputArea.getText()); // Stringデータを UTF 形式で出力
netoutput.println("end");
netoutput.flush( ); // バッファ内データを強制的に出力
netoutput.close( ); // ネット経由出力ストリームクローズ
socket.close( ); // ソケットクローズ
}catch(IOException e2) { // 例外処理
}
}
}