?? echoclient.java
字號:
//EchoClient.java
import java.io.*;
import java.net.*;
public class EchoClient
{
//服務(wù)器端的服務(wù)端口。
public static final int SERVERPORT = 9999;
public static void main(String[] args)
{
try
{
//建立連接套接字。
Socket s = new Socket("localhost", SERVERPORT);
System.out.println("socket = " + s);
//新建網(wǎng)絡(luò)連接的輸入流。
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
//新建網(wǎng)絡(luò)連接的自動刷新的輸出流。
PrintWriter out = new PrintWriter
( new BufferedWriter(new OutputStreamWriter(
s.getOutputStream())), true);
//先使用System.in構(gòu)造InputStreamReader,再構(gòu)造BufferedReader。
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string, Enter BYE to exit! ");
while (true)
{
//讀取從控制臺輸入的字符串,并向網(wǎng)絡(luò)連接輸出,即向服務(wù)器端發(fā)送數(shù)據(jù)。
out.println(stdin.readLine());
//從網(wǎng)絡(luò)連接讀取一行,即接收服務(wù)器端的數(shù)據(jù)。
String str = in.readLine();
//如果接收到的數(shù)據(jù)為空(如果直接按Enter,不是空數(shù)據(jù)),則退出循環(huán),關(guān)閉連接。
if (str == null) break;
System.out.println(str);
}
s.close();
}
catch (IOException e)
{
System.err.println("IOException");
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -