Пример #1
0
  /**
   * 向服务器发送命令行,给服务器端处理
   *
   * @param lines 命令行
   */
  public static void clientSend(String[] lines) {
    if (lines != null && lines.length <= 0) {
      return;
    }
    Socket socket = null;
    PrintWriter writer = null;
    try {
      socket = new Socket("localhost", port);

      writer =
          new PrintWriter(
              new BufferedWriter(
                  new OutputStreamWriter(
                      socket.getOutputStream(), EncodeConstants.ENCODING_UTF_8)));
      for (int i = 0; i < lines.length; i++) {
        writer.println(lines[i]);
      }

      writer.flush();
    } catch (Exception e) {
      FRContext.getLogger().error(e.getMessage(), e);
    } finally {
      try {
        writer.close();
        socket.close();
      } catch (IOException e) {
        FRContext.getLogger().error(e.getMessage(), e);
      }
    }
  }
  /**
   * Schreibe temp datei.
   *
   * @return the file
   */
  public File schreibeTempDatei() {
    File file = new File("Fragebogen", ".tmp");
    try {
      file = File.createTempFile("Fragebogen", ".tmp");
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
      int counter = 0;
      for (String s : tan) {
        counter++;
        out.write(s + "        ");

        if (counter % 3 == 0) {
          out.println();
          out.println();
          out.println();
          out.println();
          out.println();
        }
      }
      out.flush();
      return file;
    } catch (IOException e) {

      e.printStackTrace();
    }
    return file;
  }
Пример #3
0
  // メッセージをサーバーに送信する
  public void sendMessage(String msg) {
    try {
      OutputStream output = socket.getOutputStream();
      PrintWriter writer = new PrintWriter(output);

      writer.println(msg);
      writer.flush();
    } catch (Exception err) {
      msgTextArea.append("ERROR>" + err + "\n");
    }
  }
Пример #4
0
 java.util.List<Double> getHistory(String comp, int count) {
   int cmdID = commID++;
   // connect(user.getName(),user.getPassword());
   try {
     out.println(cmdID + ";" + "getch:" + comp + ":" + Integer.toString(count));
     out.flush();
     return (java.util.List<Double>) receiveReply(cmdID);
   } catch (Exception r) {
     r.printStackTrace();
   }
   return null;
 }
Пример #5
0
 String cancelShares(String user, String pass, int id, int sellid) {
   int cmdID = commID++;
   connect(user, pass);
   try {
     out.println(cmdID + ";cancel:" + id + ":" + sellid);
     out.flush();
     return (String) receiveReply(cmdID);
   } catch (Exception r) {
     r.printStackTrace();
   }
   return null;
 }
Пример #6
0
 String getChatHistory(String user, String pass) {
   int cmdID = commID++;
   connect(user, pass);
   try {
     out.println(cmdID + ";chath");
     out.flush();
     return (String) receiveReply(cmdID);
   } catch (Exception r) {
     r.printStackTrace();
   }
   return null;
 }
Пример #7
0
 String sendChat(String s, String user, String pass) {
   int cmdID = commID++;
   connect(user, pass);
   try {
     out.println(cmdID + ";chat:" + s.trim());
     out.flush();
     return (String) receiveReply(cmdID);
   } catch (Exception r) {
     r.printStackTrace();
   }
   return null;
 }
Пример #8
0
 void placeOrder(final User user, String cmd, Company comp, int qty, int id) {
   int cmdID = commID++;
   connect(user.getName(), user.getPassword());
   try {
     out.println(cmdID + ";" + cmd + ":" + comp.name + ":" + Integer.toString(qty) + ":" + id);
     out.flush();
     Shares pen = (Shares) receiveReply(cmdID);
     user.getPendingShares().add(pen);
     user.dataChanged();
   } catch (Exception r) {
     r.printStackTrace();
   }
 }
Пример #9
0
 public void disconnect() {
   int cmdID = commID++;
   this.connected = false;
   try {
     out.println(cmdID + ";logout");
     out.flush();
     in.close();
     out.close();
     socket.close();
   } catch (IOException ex) {
     System.err.println("Server stop failed.");
   }
 }
Пример #10
0
 String login(String domain, String user, String pass) {
   int cmdID = commID++;
   this.domain = domain;
   connect(user, pass);
   try {
     out.println(cmdID + ";login:"******":" + pass);
     out.flush();
     String rep = (String) receiveReply(cmdID);
     return rep.split(":")[0];
   } catch (Exception r) {
     r.printStackTrace();
   }
   return null;
 }
Пример #11
0
 User getUserDetails(String user, String pass) {
   int cmdID = commID++;
   connect(user, pass);
   try {
     usrD = 1;
     out.println(cmdID + ";gud");
     out.flush();
     User vv = (User) receiveReply(cmdID);
     usrD = 0;
     return vv;
   } catch (Exception r) {
     r.printStackTrace();
   }
   return null;
 }
Пример #12
0
 /**
  * Writes the figures to the specified output stream.
  * This method applies the specified drawingTransform to the drawing, and draws
  * it on an image of the specified getChildCount.
  * 
  * All other write methods delegate their work to here.
  */
 public void write(OutputStream out, java.util.List<Figure> figures,
         AffineTransform drawingTransform, Dimension imageSize) throws IOException {
     
     this.drawingTransform = (drawingTransform == null) ? new AffineTransform() : drawingTransform;
     this.bounds = (imageSize == null) ? 
         new Rectangle(0,0,Integer.MAX_VALUE,Integer.MAX_VALUE) :
         new Rectangle(0, 0, imageSize.width, imageSize.height);
     
     XMLElement document = new XMLElement("map");
     
     // Note: Image map elements need to be written from front to back
     for (Figure f: new ReversedList<Figure>(figures)) {
         writeElement(document, f);
     }
     
     // Strip AREA elements with "nohref" attributes from the end of the
     // map
     if (! isIncludeNohref) {
         for (int i=document.getChildrenCount() - 1; i >= 0; i--) {
             XMLElement child = (XMLElement) document.getChildAtIndex(i);
             if (child.hasAttribute("nohref")) {
                 document.removeChildAtIndex(i);
             }
         }
     }
     
     
     // Write XML content
     PrintWriter writer = new PrintWriter(
             new OutputStreamWriter(out, "UTF-8")
             );
     //new XMLWriter(writer).write(document);
     for (Object o : document.getChildren()) {
         XMLElement child = (XMLElement) o;
         new XMLWriter(writer).write(child);
     }
     
     // Flush writer
     writer.flush();
 }
Пример #13
0
 private String unpackException(Throwable ex) {
   PrintWriter pw = null;
   try {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     pw = new PrintWriter(new OutputStreamWriter(baos));
     ex.printStackTrace(pw);
     pw.flush();
     String str = baos.toString();
     int ind = 0;
     int cnt = 0;
     while (ind != -1 && cnt < 5) {
       ind = str.indexOf("\n", ind + 1);
       cnt++;
     }
     if (ind != -1) {
       str = str.substring(0, ind);
     }
     str = str.replaceAll("\n", "<BR>");
     return str;
   } finally {
     pw.close();
   }
 }
Пример #14
0
 /**
  * debug.
  *
  * @param str the message to issue.
  */
 public static void debug(final String str) {
   if (debugger != null) {
     debugger.println(str);
     debugger.flush();
   }
 }
Пример #15
0
  // The main procedure
  public static void main(String args[]) {
    String s;

    initGUI();

    while (true) {
      try { // Poll every ~10 ms
        Thread.sleep(10);
      } catch (InterruptedException e) {
      }

      switch (connectionStatus) {
        case BEGIN_CONNECT:
          try {
            // Try to set up a server if host
            if (isHost) {
              hostServer = new ServerSocket(port);
              socket = hostServer.accept();
            }

            // If guest, try to connect to the server
            else {
              socket = new Socket(hostIP, port);
            }

            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            changeStatusTS(CONNECTED, true);
          }
          // If error, clean up and output an error message
          catch (IOException e) {
            cleanUp();
            changeStatusTS(DISCONNECTED, false);
          }
          break;

        case CONNECTED:
          try {
            // Send data
            if (toSend.length() != 0) {
              out.print(toSend);
              out.flush();
              toSend.setLength(0);
              changeStatusTS(NULL, true);
            }

            // Receive data
            if (in.ready()) {
              s = in.readLine();
              if ((s != null) && (s.length() != 0)) {
                // Check if it is the end of a trasmission
                if (s.equals(END_CHAT_SESSION)) {
                  changeStatusTS(DISCONNECTING, true);
                }

                // Otherwise, receive what text
                else {
                  appendToChatBox("INCOMING: " + s + "\n");
                  changeStatusTS(NULL, true);
                }
              }
            }
          } catch (IOException e) {
            cleanUp();
            changeStatusTS(DISCONNECTED, false);
          }
          break;

        case DISCONNECTING:
          // Tell other chatter to disconnect as well
          out.print(END_CHAT_SESSION);
          out.flush();

          // Clean up (close all streams/sockets)
          cleanUp();
          changeStatusTS(DISCONNECTED, true);
          break;

        default:
          break; // do nothing
      }
    }
  }