伺服器端代碼:
公共類伺服器{
公共靜態無效主(字串[] args){
ServerSocket 伺服器;
嘗試{
伺服器 = 新的 ServerSocket(1111);
套接字套接字 = server.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
System.out.println(br.readLine());
br.close();
套接字.close();
伺服器.close();
}catch(異常e){
System.out.println(e);
}
}
}
客戶代碼:
公共類別客戶端{
公共靜態無效主(字串[] args){
插座插座;
列印流 ps;
嘗試 {
套接字 = 新套接字(“127.0.0.1”,1111);
System.out.println("連線成功...");
System.out.println("請向伺服器輸入一些文字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
字串內容=空;
嘗試 {
內容= br.readLine();
} catch (異常 e) {
}
ps = new PrintStream(socket.getOutputStream(),true,"UTF-8");
ps.print(內容);
套接字.close();
} catch (異常 e) {
System.out.println(e);
}
}
}