2014-10-19 1 views
0

나는 두 개의 프로그램을 가지고 있고 그것들을 내 학교 서버 afs1.njit.edu에 연결하고 싶지만 결코 연결하지 않는다. 그것이 내가 파일을 가지고있는 곳입니다. 서로 다른 프로그램을 실행하기 위해 두 개의 ssh 프로그램을 실행해야합니까? 테스트 방법을 잘 모르겠습니다. (이들은 온라인에서 간단한 ezxample 내가 내 코드를 작성하기 전에 테스트하고 싶습니다) 나는 그들을 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 개의 셸 (ssh)을 사용할 수 있지만 필요하지 않습니다. 백그라운드에서 수신기를 실행 (명령 끝에 & 넣기) 한 다음 송신자를 실행할 수 있습니다. 그러나 2 쉘을 실행하는 것이 더 쉽습니다. 특히 프로그램이 sysout으로 보내는 경우와 같이.

디버그/로그 출력에 System.out (또는 System.err)을 사용하는 경우 예외를 사용할 때 당기고 싶지 않으면 e.printStackTrace (System.out)을 권장합니다 이것을위한 도서관에서. 대부분의 로깅 프레임 워크에는 logger.error ("message", 예)가 있고 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하지만 논리를 확신하고 소켓을 테스트해야 할 경우 나란히 실행해야합니다.

+0

그게, 2 쉘 같은 서버가 당신에게 감사합니다 –

관련 문제