Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在ssh上测试客户端和服务器程序?_Java_Web Services_Sockets_Ssh_Client Server - Fatal编程技术网

Java 如何在ssh上测试客户端和服务器程序?

Java 如何在ssh上测试客户端和服务器程序?,java,web-services,sockets,ssh,client-server,Java,Web Services,Sockets,Ssh,Client Server,我有两个程序,我想让它们连接到我的学校服务器afs1.njit.edu,但它从未连接。那是我保存文件的地方。我应该运行两个ssh程序来分别运行不同的程序吗?不确定如何测试它们。(这是一个来自在线的简单ezxample,我想在编写代码之前进行测试)我对它们进行了手工编译,但它们从未连接 singleSocketserver.java public static void main(String[] args) { try{ socket1 = new ServerSocket(port

我有两个程序,我想让它们连接到我的学校服务器afs1.njit.edu,但它从未连接。那是我保存文件的地方。我应该运行两个ssh程序来分别运行不同的程序吗?不确定如何测试它们。(这是一个来自在线的简单ezxample,我想在编写代码之前进行测试)我对它们进行了手工编译,但它们从未连接

singleSocketserver.java

    public static void main(String[] args) {
try{
  socket1 = new ServerSocket(port);
  System.out.println("SingleSocketServer Initialized");
  int character;

  while (true) {
    connection = socket1.accept();

    BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
    InputStreamReader isr = new InputStreamReader(is);
    process = new StringBuffer();
    while((character = isr.read()) != 13) {
      process.append((char)character);
    }
    System.out.println(process);
    //need to wait 10 seconds for the app to update database
    try {
      Thread.sleep(10000);
    }
    catch (Exception e){}
    TimeStamp = new java.util.Date().toString();
    String returnCode = "SingleSocketServer repsonded at "+ TimeStamp + (char) 13;
    BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
    OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
    osw.write(returnCode);
    osw.flush();
 }
}
catch (IOException e) {}
  try {
    connection.close();
  }
  catch (IOException e) {}
  }
}
SocketClient.java

    public class SocketClient {

public static void main(String[] args) {
/** Define a host server */
String host = "afs1.njit.edu";
/** Define a port */
int port = 19999;

StringBuffer instr = new StringBuffer();
String TimeStamp;
System.out.println("SocketClient initialized");

try {
  /** Obtain an address object of the server */
  InetAddress address = InetAddress.getByName(host);
  /** Establish a socket connetion */
  Socket connection = new Socket(address, port);
  /** Instantiate a BufferedOutputStream object */
  BufferedOutputStream bos = new BufferedOutputStream(connection.
      getOutputStream());

  /** Instantiate an OutputStreamWriter object with the optional character
   * encoding.
   */
  OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");


  TimeStamp = new java.util.Date().toString();
  String process = "Calling the Socket Server on "+ host + " port " + port +
      " at " + TimeStamp +  (char) 13;

  /** Write across the socket connection and flush the buffer */
  osw.write(process);
  osw.flush();

   /** Instantiate a BufferedInputStream object for reading
  /** Instantiate a BufferedInputStream object for reading
   * incoming socket streams.
   */

  BufferedInputStream bis = new BufferedInputStream(connection.
      getInputStream());
  /**Instantiate an InputStreamReader with the optional
   * character encoding.
   */

  InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

  /**Read the socket's InputStream and append to a StringBuffer */
  int c;
  while ( (c = isr.read()) != 13)
    instr.append( (char) c);

  /** Close the socket connection. */
  connection.close();
  System.out.println(instr);
 }
catch (IOException f) {
  System.out.println("IOException: " + f);
}
catch (Exception g) {
  System.out.println("Exception: " + g);
}
}

}

如果你试图从家里连接到学校的计算机,那么你可能会碰到防火墙。通常,虽然不总是允许从机器启动连接,但只允许在某些端口上连接到机器。您可以将ssh设置为通过隧道传输数据包,但也可以将这两个程序相邻运行

如果在同一台计算机上运行两个程序,则它们应该可以找到另一个程序,前提是: 1:您可以打开插座 2:程序尚未使用套接字 3:防火墙不会阻止这些端口

要在学校机器上同时运行这两个脚本,可以使用2个shell(ssh),但这不是必需的。您可以在后台运行接收器(在命令末尾放一个&),然后运行发送器。但是,运行2个shell更容易,特别是如果程序像您的一样发送到sysout

如果您使用System.out(或System.err)进行调试/日志输出,那么在使用异常时,如果您不想为此引入库,我建议使用e.printStackTrace(System.out)。大多数日志框架都有一个logger.error(“message”,ex),commons.lang也有一个异常打印机

在没有套接字连接的情况下,可以使用PipedInputStream和PipedOutputStream来测试逻辑。但是,如果您确信自己的逻辑,并且需要测试套接字,那么您必须同时运行它们