我有两个程序,我希望它们连接到我的学校服务器afs1.njit.edu,但它永远不会连接。这就是我有文件的地方。我应该运行两个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);
}
}
}
答案 0 :(得分:1)
如果您尝试从家中连接到学校的计算机,那么您可能会遇到防火墙。通常,虽然并非总是如此,但允许从机器启动的连接,但只允许在某些端口上连接到机器。你可以设置你的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但如果您确定自己的逻辑且需要测试套接字,则必须并排运行它们。