1、客戶(hù)端
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
// 超時(shí)時(shí)間
socket.setSoTimeout(3000);
// 連接本地,端口2000;超時(shí)時(shí)間3000ms
socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(), 2000), 3000);
System.out.println("已發(fā)起服務(wù)器連接,并進(jìn)入后續(xù)流程~");
System.out.println("客戶(hù)端信息:" + socket.getLocalAddress() + " P:" + socket.getLocalPort());
System.out.println("服務(wù)器信息:" + socket.getInetAddress() + " P:" + socket.getPort());
try {
// 發(fā)送接收數(shù)據(jù)
todo(socket);
} catch (Exception e) {
System.out.println("異常關(guān)閉");
}
// 釋放資源
socket.close();
System.out.println("客戶(hù)端已退出~");
}
private static void todo(Socket client) throws IOException {
// 構(gòu)建鍵盤(pán)輸入流
InputStream in = System.in;
BufferedReader input = new BufferedReader(new InputStreamReader(in));
// 得到Socket輸出流,并轉(zhuǎn)換為打印流
OutputStream outputStream = client.getOutputStream();
PrintStream socketPrintStream = new PrintStream(outputStream);
// 得到Socket輸入流,并轉(zhuǎn)換為BufferedReader
InputStream inputStream = client.getInputStream();
BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
boolean flag = true;
do {
// 鍵盤(pán)讀取一行
String str = input.readLine();
// 發(fā)送到服務(wù)器
socketPrintStream.println(str);
// 從服務(wù)器讀取一行
String echo = socketBufferedReader.readLine();
if ("bye".equalsIgnoreCase(echo)) {
flag = false;
} else {
System.out.println(echo);
}
} while (flag);
// 資源釋放
socketPrintStream.close();
socketBufferedReader.close();
}
}
2、服務(wù)端
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(2000);
System.out.println("服務(wù)器準(zhǔn)備就緒~");
System.out.println("服務(wù)器信息:" + server.getInetAddress() + " P:" + server.getLocalPort());
// 等待客戶(hù)端連接
for (; ; ) {
// 得到客戶(hù)端
Socket client = server.accept();
// 客戶(hù)端構(gòu)建異步線(xiàn)程
ClientHandler clientHandler = new ClientHandler(client);
// 啟動(dòng)線(xiàn)程
clientHandler.start();
}
}
/**
* 客戶(hù)端消息處理
*/
private static class ClientHandler extends Thread {
private Socket socket;
private boolean flag = true;
ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
super.run();
System.out.println("新客戶(hù)端連接:" + socket.getInetAddress() +
" P:" + socket.getPort());
try {
// 得到打印流,用于數(shù)據(jù)輸出;服務(wù)器回送數(shù)據(jù)使用
PrintStream socketOutput = new PrintStream(socket.getOutputStream());
// 得到輸入流,用于接收數(shù)據(jù)
BufferedReader socketInput = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
do {
// 客戶(hù)端拿到一條數(shù)據(jù)
String str = socketInput.readLine();
if ("bye".equalsIgnoreCase(str)) {
flag = false;
// 回送
socketOutput.println("bye");
} else {
// 打印到屏幕。并回送數(shù)據(jù)長(zhǎng)度
System.out.println(str);
socketOutput.println("回送:" + str.length());
}
} while (flag);
socketInput.close();
socketOutput.close();
} catch (Exception e) {
System.out.println("連接異常斷開(kāi)");
} finally {
// 連接關(guān)閉
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("客戶(hù)端已退出:" + socket.getInetAddress() +
" P:" + socket.getPort());
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。