我有两个程序,我希望它们连接到我的学校服务器 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 来隧道传输数据包,但您也可以同时运行这 2 个程序。
如果您在同一台计算机上运行这两个程序,它们应该能够找到彼此,假设: 1:允许您打开套接字 2:套接字尚未被另一个程序占用 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?
在没有套接字连接的情况下,您可以做的一件事是使用 PipedInputStream 和 PipedOutputStream 来测试逻辑。 http://docs.oracle.com/javase/7/docs/api/java/io/PipedInputStream.html但如果您确定自己的逻辑并且需要测试套接字,则必须并排运行它们。
关于java - 如何在ssh上测试客户端和服务器程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26456071/