📄 client.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.io.*;import java.net.*;/** * * @author admin */public class client { // private String strHost; private static ServerSocket servSock; private static InetAddress host ; private static final int PORT = 5000; private static Socket link ; private static BufferedReader in ; private static PrintWriter out ; private static BufferedReader keyboard ; public static void main(String args[]){ try{ //host = InetAddress.getLocalHost(); //strHost = args[0] ; System.out.println("Opening Port.....\n"); try{ servSock = new ServerSocket(PORT); }catch(IOException e){ System.out.println("Unable to attach to port"); System.exit(1); } host = InetAddress.getByName("169.254.235.236"); link = new Socket(host,PORT); in = new BufferedReader(new InputStreamReader(link.getInputStream())); out = new PrintWriter(link.getOutputStream(),true); keyboard = new BufferedReader(new InputStreamReader(System.in)); String message, response; do{ System.out.print("Enter message(QUIT to exit)"); message = keyboard.readLine(); Socket client = servSock.accept(); ClientHandler handler = new ClientHandler(client); handler.start(); out.println(message); response = in.readLine(); System.out.println(response); }while(!message.equals("QUIT")); }catch(UnknownHostException e){ System.out.println("Host ID not found!"); } catch(IOException e){ e.printStackTrace(); } finally{ try{ if (link != null){ System.out.println("Closing down connection"); link.close(); } } catch(IOException e){ e.printStackTrace(); } } } static class ClientHandler extends Thread{ private Socket client ; private BufferedReader in ; private PrintWriter out ; public ClientHandler(Socket socket){ client = socket ; try{ in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(),true); }catch(IOException e){ e.printStackTrace(); } } public void run(){ try{ String received ; do{ received = in.readLine(); System.out.println(received); out.println("ECHO : " + received); }while(!received.equals("QUIT")); }catch(IOException e){ e.printStackTrace(); } finally{ try{ if (client != null){ System.out.println("Closing down connection"); client.close(); } }catch(IOException e){ e.printStackTrace(); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -