2014-10-19 93 views
0

我有兩個程序,我希望他們連接到我的學校服務器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); 
} 
} 

}

回答

1

,如果你想從你家連接到學校的計算機,那麼你可能會打一個防火牆。通常,雖然並非總是如此,但允許從機器發起的連接,但只能在某些端口上連接到機器。你可以設置你的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