Ejemplo n.º 1
0
  public static void main(String a[]) throws Exception {

    boolean bool = false;

    try {
      InputStreamReader in = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(in);

      do {
        Socket clientSocket = new Socket("localhost", 6789);
        TCPClient client = new TCPClient(clientSocket);
        client.talkOnSocket();

        System.out.println("Do you want tranfer another file : Y/N");
        String choise = br.readLine().trim();
        if (choise.equalsIgnoreCase("y")) {
          bool = true;
        } else if (choise.equalsIgnoreCase("n")) {
          bool = false;
        } else {
          bool = false;
          System.out.println("Invalid entry. Clsoing.");
        }
      } while (bool);
      in.close();
      br.close();

    } catch (Exception e) {
      System.out.println("Server might not be up and running....");
      System.exit(0);
    }
  }
Ejemplo n.º 2
0
  public Server() {
    try {
      ServerSocket ss = new ServerSocket(SERVER_PORT);

      Socket s = ss.accept();

      InputStream is = s.getInputStream();
      OutputStream out = s.getOutputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);

      String sss = br.readLine();
      System.out.println(sss);

      PrintWriter pw = new PrintWriter(out);
      pw.print("hello,我是服务器。");

      pw.close();
      br.close();
      isr.close();
      out.close();
      is.close();
      s.close();
      ss.close();
    } catch (UnknownHostException ue) {
      ue.printStackTrace();
    } catch (IOException oe) {
      oe.printStackTrace();
    }
  }
Ejemplo n.º 3
0
    private String getAddressXY(String x, String y) {
      try {
        StringBuilder text = new StringBuilder();

        String url = properties.getProperty("geocoderUrl");
        String param1 = properties.getProperty("geocoderUrlParam1");
        String param2 = properties.getProperty("geocoderUrlParam2");
        url = url + "?" + param1 + "=" + x + "&" + param2 + "=" + y;

        URL page = new URL(url);
        HttpURLConnection urlConn = (HttpURLConnection) page.openConnection();
        urlConn.connect();
        InputStreamReader in = new InputStreamReader((InputStream) urlConn.getContent());
        BufferedReader buff = new BufferedReader(in);
        String line = buff.readLine();

        while (line != null) {
          text.append(line);
          line = buff.readLine();
        }

        String result = text.toString();
        buff.close();
        in.close();

        return result;

      } catch (MalformedURLException e) {
        windowServer.txtErrors.append(e.getMessage() + "\n");
        return "";
      } catch (Exception e) {
        windowServer.txtErrors.append(e.getMessage() + "\n");
        return "";
      }
    }
Ejemplo n.º 4
0
  public BoardList() {
    URL url = null;
    HttpURLConnection http_url_connection = null;
    int response_code;
    String response_message;
    InputStreamReader in = null;
    BufferedReader reader = null;
    ParserDelegator pd = null;
    try {
      // httpでhtmlファイルを取得する一連の処理
      url = new URL("http://menu.2ch.net/bbsmenu.html");
      http_url_connection = (HttpURLConnection) url.openConnection();
      http_url_connection.setRequestMethod("GET");
      http_url_connection.setInstanceFollowRedirects(false);
      http_url_connection.setRequestProperty("User-Agent", "Monazilla/1.00");
      response_code = http_url_connection.getResponseCode();
      response_message = http_url_connection.getResponseMessage();
      in = new InputStreamReader(http_url_connection.getInputStream(), "SJIS");
      reader = new BufferedReader(in);

      pd = new ParserDelegator();
      pd.parse(reader, cb, true);
      in.close();
      reader.close();
      http_url_connection.disconnect();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }
Ejemplo n.º 5
0
  /**
   * 请求xml数据
   *
   * @param url
   * @param soapAction
   * @param xml
   * @return
   */
  public static String sendXMl(String url, String soapAction, String xml) {
    HttpURLConnection conn = null;
    InputStream in = null;
    InputStreamReader isr = null;
    OutputStream out = null;
    StringBuffer result = null;
    try {
      byte[] sendbyte = xml.getBytes("UTF-8");
      URL u = new URL(url);
      conn = (HttpURLConnection) u.openConnection();
      conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
      conn.setRequestProperty("SOAPAction", soapAction);
      conn.setRequestProperty("Content-Length", sendbyte.length + "");
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setConnectTimeout(5000);
      conn.setReadTimeout(5000);

      out = conn.getOutputStream();
      out.write(sendbyte);

      if (conn.getResponseCode() == 200) {
        result = new StringBuffer();
        in = conn.getInputStream();
        isr = new InputStreamReader(in, "UTF-8");
        char[] c = new char[1024];
        int a = isr.read(c);
        while (a != -1) {
          result.append(new String(c, 0, a));
          a = isr.read(c);
        }
      }

    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (conn != null) {
        conn.disconnect();
      }
      try {
        if (in != null) {
          in.close();
        }
        if (isr != null) {
          isr.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return result == null ? null : result + "";
  }
 private static String readStreamToString(InputStream in, String encoding) throws IOException {
   StringBuffer b = new StringBuffer();
   InputStreamReader r = new InputStreamReader(in, encoding);
   int c;
   while ((c = r.read()) != -1) {
     b.append((char) c);
   }
   return b.toString();
 }
Ejemplo n.º 7
0
  public ErrorInfo listPolicy(String polname, String token) throws IOException, RestException {

    String realm = "/";
    String data = null;
    ErrorInfo ei = null;
    InputStreamReader iss = null;
    BufferedReader br = null;
    HttpURLConnection urlc = null;
    InputStream inputStream = null;

    try {
      data =
          "policynames="
              + URLEncoder.encode(polname, "UTF-8")
              + "&realm="
              + URLEncoder.encode(realm, "UTF-8")
              + "&submit="
              + URLEncoder.encode("Submit", "UTF-8");
    } catch (UnsupportedEncodingException e) {
      System.out.println("OpenssoHelper: " + e.getMessage());
      e.printStackTrace();
    }

    if (data != null) {
      try {
        r.Connect(new URL(url + ssoadm_list));
      } catch (MalformedURLException e) {
        System.out.println("OpenssoHelper: " + e.getMessage());
        e.printStackTrace();
      }

      urlc = (HttpURLConnection) r.c;
      urlc.addRequestProperty("Cookie", "iPlanetDirectoryPro=\"" + token + "\"");
      r.Send(urlc, data);

      String answer = null;
      int status = 0;
      inputStream = urlc.getInputStream();
      iss = new InputStreamReader(inputStream);
      br = new BufferedReader(iss);
      answer = BrToString(br);
      status = urlc.getResponseCode();
      if (answer != null) {
        ei = new ErrorInfo(answer, status);
      }
      br.close();
      iss.close();
      inputStream.close();
      urlc.disconnect();
    }

    return ei;
  }
Ejemplo n.º 8
0
    private String transportHttpMessage(String message) {
      URL serverurl;
      HttpURLConnection connection;

      try {
        serverurl = new URL(this.ServerUrl);
        connection = (HttpURLConnection) serverurl.openConnection();
        connection.setDefaultUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }

      OutputStreamWriter outstream;
      try {
        outstream = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        outstream.write(message);
        outstream.flush();
        /*
        PrintWriter writer = new PrintWriter(outstream);
        writer.write(message);
        writer.flush();
        */
        outstream.close();
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }

      StringBuilder strbuilder = new StringBuilder();
      try {
        InputStreamReader instream = new InputStreamReader(connection.getInputStream(), "UTF-8");
        BufferedReader reader = new BufferedReader(instream);

        String str;
        while ((str = reader.readLine()) != null) strbuilder.append(str + "\r\n");

        instream.close();
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }

      connection.disconnect();
      return strbuilder.toString();
    }
Ejemplo n.º 9
0
  public ErrorInfo doLogin() throws IOException, RestException {

    String data = null;
    ErrorInfo ei = null;
    InputStreamReader iss = null;
    BufferedReader br = null;
    HttpURLConnection urlc = null;
    InputStream inputStream = null;

    try {
      data =
          "username="******"UTF-8")
              + "&password="******"UTF-8");
    } catch (UnsupportedEncodingException e) {
      System.out.println("OpenssoHelper: " + e.getMessage());
      e.printStackTrace();
    }

    if (data != null) {
      try {
        r.Connect(new URL(url + authenticate));
      } catch (MalformedURLException e) {
        System.out.println("OpenssoHelper: " + e.getMessage());
        e.printStackTrace();
      }

      urlc = (HttpURLConnection) r.c;
      r.Send(urlc, data);

      String answer = null;
      int status = 0;

      inputStream = urlc.getInputStream();
      iss = new InputStreamReader(inputStream);
      br = new BufferedReader(iss);
      answer = BrToString(br);
      status = urlc.getResponseCode();
      if (answer != null) {
        ei = new ErrorInfo(answer, status);
      }
      br.close();
      iss.close();
      inputStream.close();
      urlc.disconnect();
    }

    return ei;
  }
Ejemplo n.º 10
0
 public static String getContent(URL url) throws IOException {
   String content = "";
   InputStream inst = url.openStream();
   InputStreamReader insr = new InputStreamReader(inst);
   BufferedReader buff = new BufferedReader(insr);
   while (true) {
     String line = buff.readLine();
     if (line != null) content += line + "\n";
     else break;
   }
   inst.close();
   insr.close();
   buff.close();
   return content;
 }
Ejemplo n.º 11
0
 private boolean checkServer(int port, boolean ssl) {
   try {
     URL url = new URL("http://127.0.0.1:" + port);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setRequestMethod("GET");
     conn.connect();
     int length = conn.getContentLength();
     StringBuffer text = new StringBuffer();
     InputStream is = conn.getInputStream();
     InputStreamReader isr = new InputStreamReader(is);
     int size = 256;
     char[] buf = new char[size];
     int len;
     while ((len = isr.read(buf, 0, size)) != -1) text.append(buf, 0, len);
     isr.close();
     if (programName.equals("ISN")) return !shutdown(port, ssl);
     return true;
   } catch (Exception ex) {
     return false;
   }
 }
Ejemplo n.º 12
0
  public void Connect() throws IOException {
    try {
      InetAddress addr = InetAddress.getByName(server);
      Socket socket = new Socket(addr, port);

      in = new InputStreamReader(socket.getInputStream());
      out = new OutputStreamWriter(socket.getOutputStream());

      char c[] = new char[LINE_LENGTH];

      boolean authed = false;

      while (true) {
        if (in.read(c) > 0) {
          String msg = new String(c).trim();

          if (!authed) {
            if (msg.endsWith("No Ident response")) {

              System.out.println("...sending identity...");
              out.write(String.format("NICK %s\r\n", nickname));
              out.write(String.format("USER %s %s %s :s\r\n", nickname, host, server, realname));
              out.flush();

              authed = true;

              for (IRCAuthListener a : authListeners) {
                a.afterAuthentication(this);
              }
            }
          }

          if (msg.startsWith("PING")) {
            System.out.println("Sending pong...");
            out.write("PONG\r\n");
            out.flush();
          }

          for (IRCRawDataListener d : rawDataListeners) {
            d.processRawData(this, msg);
          }

          c = new char[LINE_LENGTH];
        } else {
          System.out.println("Oops, if you see this we just got kicked or connection timed out...");
          break;
        }
      }
    } catch (IOException ioe) {
      throw (ioe);
    }
  }
Ejemplo n.º 13
0
  @Override
  public void run() {
    System.out.println("Starting Listener");
    int character;
    try {
      socket1 = new ServerSocket(port);
      game.gui.lock("Waiting for other player");
      connection = socket1.accept();

      bis = new BufferedInputStream(connection.getInputStream());
      isr = new InputStreamReader(bis);

      while ((character = isr.read()) != 13) {
        strb.append((char) character);
      }

      // if received CLOSEMESSAGE -> end listening and do something
      if (strb.toString().equals(Online.CLOSEMESSAGE)) {
        System.out.println("Looks like we have ended...");
      }

      // check incoming message
      System.out.println(strb);

      Figure f = game.parseFigure(strb.toString());
      Position p = game.parsePosition(strb.toString());
      game.playerPlay(f, p);

      game.switchPlayers();
      game.gui.unlock();
      connection.close();
      System.out.println("moved, your turn!");
      socket1.close();

    } catch (IOException e) {
      System.out.println("Unexpected exception on server side" + e);
    }
  }
Ejemplo n.º 14
0
  public void print(IWContext iwc) throws Exception {

    if (doPrint(iwc)) {

      if (getMarkupLanguage().equals("HTML")) {

        // if (getInterfaceStyle().equals("something")){

        // }

        // else{

        try {

          URL url = new URL(getURL());

          String returnString = "";

          // URL url = new URL("http://localhost/servlet/plainTextModule");

          URLConnection uc = url.openConnection();

          uc.setDoOutput(true);

          uc.setDoInput(true);

          uc.setUseCaches(false);

          uc.setRequestProperty("Content-type", "text/html");

          // uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");

          /*DataOutputStream dos = new DataOutputStream(uc.getOutputStream());

          dos.writeBytes(qry);

          dos.flush();

          dos.close();*/

          InputStreamReader in = new InputStreamReader(uc.getInputStream());

          int chr = in.read();

          while (chr != -1) {

            returnString = returnString + (String.valueOf((char) chr));

            chr = in.read();
          }

          println(returnString);

          // clean up!

          in.close();

        } catch (MalformedURLException e) {

          throw new IOException(e.getMessage());
        }

        // }

      } else if (getMarkupLanguage().equals("WML")) {

      }

    } else {

      super.print(iwc);
    }
  }