simpleclient.java

来自「《JAVA分布式程序设计》一书的源代码。」· Java 代码 · 共 59 行

JAVA
59
字号
import java.io.*;import java.net.*;/**  * @(#)SimpleClient.java * @author Qusay H. Mahmoud */ public class SimpleClient {      public final static int REMOTE_PORT = 5000;  public static void main(String argv[]) throws Exception {    Socket cl = null, cl2=null;    BufferedReader is = null;    DataOutputStream os = null;    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));    String userInput = null;    String output = null;    // Open connection to the server on port REMOTE_PORT	    try {      cl = new Socket("machine name",REMOTE_PORT);      is = new BufferedReader(new InputStreamReader(cl.getInputStream()));      os = new DataOutputStream(cl.getOutputStream());    } catch(UnknownHostException e1) {       System.out.println("Unknown Host: "+e1);    } catch (IOException e2) {       System.out.println("Erorr io: "+e2);    }    // write the url to the compute engine    try {	          System.out.print("Please input a keyword: ");      userInput = stdin.readLine();      os.writeBytes(userInput+"\n");    } catch (IOException ex) {       System.out.println("error writing to server..."+ex);    }	    // receive results from the compute engine    try {      output = is.readLine();      System.out.println("Got from server: "+output);    } catch(IOException e) {       e.printStackTrace();    }    // close input stream, output stream and connection    try {      is.close();      os.close();      cl.close();    } catch (IOException x) {       System.out.println("Error writing...."+x);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?