⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sclient.java

📁 工厂--消费者模式,客户端代码.可用于发送请求,并接收服务端的响应
💻 JAVA
字号:

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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -