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

问题描述:

我有两个程序,我希望他们连接到我的学校服务器afs1.njit.edu,但它永远不会连接。那是我拥有这些文件的地方。我应该运行两个ssh程序来运行不同的程序吗?不确定如何测试它们。 (这些是我在编写代码之前想要测试的来自在线的一个简单例子)我将它们编译为serperately并且它们从不连接。如何在ssh上测试客户端和服务器程序?

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:插座是不是已经采取anothe程序 3:防火墙不会阻止这些端口。

要在学校机器上运行,你可以使用2个shell(ssh),但这不是必须的。您可以在后台运行接收器(在命令末尾放置一个&),然后运行发件人。但是运行2个shell比较容易,特别是如果程序像你一样发送到sysout。

几个指针,如果你使用System.out(或System.err)进行调试/日志输出,当你消耗一个异常时,如果你不想拉动,我推荐e.printStackTrace(System.out)在这个库中。大多数日志记录框架都有一个logger.error(“message”,ex),commons.lang也有一个异常打印机。

How can I convert a stack trace to a string?

有一两件事可以做,以测试你的逻辑,没有socket连接,是使用的PipedInputStream和的PipedOutputStream。 http://docs.oracle.com/javase/7/docs/api/java/io/PipedInputStream.html但是如果你确定你的逻辑并且需要测试套接字,你就必须并排运行它们。

+0

就是这样,2个相同的服务器谢谢你 – 2014-10-20 14:44:05