sclient.java

来自「工厂--消费者模式,客户端代码.可用于发送请求,并接收服务端的响应」· Java 代码 · 共 63 行

JAVA
63
字号

import java.net.*;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: lizh
 * Date: 2006-8-26
 * Time: 16:27:45
 * To change this template use File | Settings | File Templates.
 */
public class SClient {
  public final static String STR_ADDRESS = "127.0.0.1";
  //public final static String STR_ADDRESS = "10.17.192.206";
  //public final static String STR_ADDRESS = "10.17.192.125";

  public static void main(String args[]) {
    SClient sClient = new SClient();
    sClient.execute();
  }

  public void execute() {
    Socket socket = null;
    InputStream is = null;
    OutputStream os = null;
    String strOut = "hello\n";
    String strIn = "";
    try {
      socket = new Socket(STR_ADDRESS, SServer.PORT);
      //socket = new Socket(STR_ADDRESS, 3333);
      os = socket.getOutputStream();
      is = socket.getInputStream();
      DataInputStream dis = new DataInputStream(is);
      os.write(strOut.getBytes());
      os.flush();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      strIn = br.readLine();
      System.out.println(strIn);
    }
    catch (Exception e) {
      System.out.println("1--\n"+e);
    }
    finally {
      try {
        if (is != null) {
          is.close();
        }
        if (os != null) {
          os.close();
        }
        if (socket != null) {
          socket.close();
        }
      }
      catch (Exception e) {
        System.out.println(e);
      }
    }

  }
}

⌨️ 快捷键说明

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