Java-Socket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket server= new ServerSocket( 2000 ); System.out.println( "服務器準備就緒" ); System.out.println( "服務器信息:" +server.getInetAddress()+ " P:" +server.getLocalPort()); //等待客戶端連接(無限循環) for (;;) { //得到客戶端 Socket client = server.accept(); //加入異步線程 ClientHandler clientHandler = new ClientHandler(client); //啟動線程 clientHandler.start(); } } //客戶端消息處理 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( "新客戶端連接:" +socket.getInetAddress() + " P:" +socket.getPort()); try { //得到打印流,用於數據輸出;服務器回送數據 PrintStream socketOutput= new PrintStream(socket.getOutputStream()); //得到輸入流,用於接收數據 BufferedReader socketInput= new BufferedReader( new InputStreamReader( socket.getInputStream())); do { //客戶端拿到數據 String str=socketInput.readLine(); if ( "bye" .equalsIgnoreCase( str)){ flag= false ; //回送 socketOutput.println( "bye" ); } else { //打印到屏幕,並且回送數據長度 System.out.println(str); socketOutput.println( "回送:" +str.length()); } } while (flag); socketInput.close(); socketOutput.close(); } catch (Exception e){ System.out.println( "連接異常斷開" ); } finally { //連接關閉 try { socket.close(); } catch (IOException e){ e.printStackTrace(); } } System.out.println( "客戶端已經退出:" +socket.getInetAddress() + " P:" +socket.getPort()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import java.io.*; import java.net.Inet4Address; import java.net.InetSocketAddress; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException { Socket socket= new Socket(); //超时时间 socket.setSoTimeout( 3000 ); //连接本地,端口2000;超时时间3000ms socket.connect( new InetSocketAddress(Inet4Address.getLocalHost(), 2000 ), 3000 ); System.out.println( "已发起服务器连接,并且进入后续流程" ); System.out.println( "客户信息:" +socket.getLocalAddress()+ " P:" +socket.getLocalPort()); System.out.println( "服务器信息:" +socket.getInetAddress()+ " P:" +socket.getPort()); try { //发送接收数据 todo(socket); } catch (Exception e){ System.out.println( "异常关闭" ); } //释放资源 socket.close(); System.out.println( "客户端已退出~" ); } private static void todo(Socket client) throws IOException{ //构建键盘输入流 InputStream in=System.in; BufferedReader input= new BufferedReader( new InputStreamReader(in)); //得到Socket输出流,并转换为打印流 OutputStream outputStream=client.getOutputStream(); PrintStream socketPrintStream= new PrintStream(outputStream); //得到Socket输入流,并转换为BufferedReader InputStream inputStream=client.getInputStream(); BufferedReader socketBufferedReader= new BufferedReader( new InputStreamReader(inputStream)); boolean flag= true ; do { //键盘读取一行 String str = input.readLine(); //发送到服务器 socketPrintStream.println(str); String echo = socketBufferedReader.readLine(); if ( "bye" .equalsIgnoreCase(echo)) { flag= false ; } else { System.out.println(echo); } } while (flag); //資源釋放 socketPrintStream.close(); socketBufferedReader.close(); } } |
Build Project之后生成
在窗体中运行
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步