queryconnect.java

来自「地图显示实例」· Java 代码 · 共 71 行

JAVA
71
字号

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

public class QueryConnect {
  static Socket s;
  static DataInputStream in;
  static DataOutputStream out;
  
  public boolean connect(String data) {
    boolean success = false;
    System.out.println("connecting...\n" + data + "\n");
    try {
      s = new Socket("franang.umiacs.umd.edu", 8080);
      in = new DataInputStream(s.getInputStream());
      out = new DataOutputStream(s.getOutputStream());
      out.writeBytes("POST /~xhan/cgi/query HTTP/1.0\n");
      out.writeBytes("Content-type: text/plain\n");
      out.writeBytes("Content-length: " + data.length() + "\n\n");
      out.writeBytes(data);
      /* descard the first 8 lines HTTP header */
      for (int i = 0; i < 8; i++) in.readLine();
      success = true;
    }
    catch (IOException e1) {
      System.out.println("Exception:" + e1);
      try {in.close(); out.close(); }
      catch (IOException e2) {};
      success = false;
    }
    return success;
  }
  public String nextLine() {
    String line = null;
    try {
      line = in.readLine();
      if (line == null) { in.close(); out.close(); s.close(); };
//      else System.out.println("line: " + line);
    }  
    catch (IOException e) {  System.out.println("Exception:" + e); }
    return line;
  }

  public static void main(String arg[]) {
    boolean success = false;
    String data = "YES!!!!!!!!!!";
    try {
      s = new Socket("franang.umiacs.umd.edu", 8080);
      in = new DataInputStream(s.getInputStream());
      out = new DataOutputStream(s.getOutputStream());
      out.writeBytes("POST /~xhan/cgi/query HTTP/1.0\n");
      out.writeBytes("Content-type: text/plain\n");
      out.writeBytes("Content-length: " + data.length() + "\n\n");
      out.writeBytes(data + "\n");
      success = true;
      String line = in.readLine();
      while (line != null) {
	System.out.println("line: " + line);
	line = in.readLine();
      }
    }  
    catch (IOException e1) {
      System.out.println("Exception:" + e1);
      try {in.close(); out.close(); }
      catch (IOException e2) {};
      success = false;
    };
  }
}
	

⌨️ 快捷键说明

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