2014-06-18 2 views
1

다른 웹 소켓 서버에 연결할 웹 소켓 서버를 얻을 수있는 방법이 있습니까? Java로이 스 니펫을 작성했지만 작동하지 않습니다. 오류나 예외가 발생하지 않으며, 영원히 연결될 때까지 기다립니다.다른 웹 소켓 서버에 연결을 초기화하는 웹 소켓 서버

@OnMessage 
    public void message(Session session, String msg){ 
     String URL = "ws://wildfly2-ciri.rhcloud.com:8000/echo"; 
     try { 
      System.out.println("**1 Got new message: " + msg); 
      String forward = "This is WildFly 1: " + msg; 
      System.out.println("**1 Init new session"); 
      Session newSession = session.getContainer().connectToServer(Client.class, URI.create(URL)); 
      System.out.println("**1 Sending to wildfly2"); 
      newSession.getBasicRemote().sendText(forward); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

기본적으로이 서버는 다른 주소의 다른 서버에 대한 새로운 websocket 연결을 초기화해야합니다. 그러나 새 연결을 만들려고하면 프로그램이 중지됩니다. 내 생각에 결함이 있거나 이런 종류의 연결이 불가능합니까?

답변

0

유용 할 수 있습니다. 이것은 클라이언트와 서버 사이에서 통신하는 데 사용했던 이전 소켓 프로그램 중 하나입니다. XML 파일을 보낼 프로그램을위한 클라이언트와 코드를 모두 첨부했습니다. 그러나 그녀는 당신을 위해 일하기 위해 몇 가지 사항을 편집해야합니다. 이것을 파일로 재생하고 소켓에 대한 느낌을 얻고 프로그램에 적용하십시오. Happy Learnings 내 친구!

public static void main(String[] args) throws IOException { 
    Socket socket = null; 
    String host = "127.0.0.1"; 

    socket = new Socket(host, 4444); 

    File file = new File("C:\\testXML.xml"); 
    // Get the size of the file 
    long length = file.length(); 
    if (length > Integer.MAX_VALUE) { 
     System.out.println("File is too large."); 
    } 
    byte[] bytes = new byte[(int) length]; 
    FileInputStream fis = new FileInputStream(file); 
    BufferedInputStream bis = new BufferedInputStream(fis); 
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream()); 

    int count; 

    while ((count = bis.read(bytes)) > 0) { 
     out.write(bytes, 0, count); 
    } 

    out.flush(); 
    out.close(); 
    fis.close(); 
    bis.close(); 
    socket.close(); 
    } 
} 

public class Server { 

/** 
* @param args the command line arguments 
*/ 
    public static void main(String[] args) throws IOException { 
     ServerSocket serverSocket = null; 

     try { 
      serverSocket = new ServerSocket(4444); 
     } catch (IOException ex) { 
      System.out.println("Can't setup server on this port number. "); 
     } 

     Socket socket = null; 
     InputStream is = null; 
     FileOutputStream fos = null; 
     BufferedOutputStream bos = null; 
     int bufferSize = 0; 

     try { 
      socket = serverSocket.accept(); 
     } catch (IOException ex) { 
      System.out.println("Can't accept client connection. "); 
     } 

     try { 
      is = socket.getInputStream(); 

      bufferSize = socket.getReceiveBufferSize(); 
      System.out.println("Buffer size: " + bufferSize); 
     } catch (IOException ex) { 
      System.out.println("Can't get socket input stream. "); 
     } 

     try { 
      fos = new FileOutputStream("C:\\xxxXXXXxxx.txt"); 
      bos = new BufferedOutputStream(fos); 

     } catch (FileNotFoundException ex) { 
      System.out.println("File not found. "); 
     } 

     byte[] bytes = new byte[bufferSize]; 

     int count; 

     while ((count = is.read(bytes)) > 0) { 
      bos.write(bytes, 0, count); 
     } 

     bos.flush(); 
     bos.close(); 
     is.close(); 
     socket.close(); 
     serverSocket.close(); 
     } 
    } 
+0

답장을 보내 주셔서 감사합니다. 그러나 이것은 내가 찾고있는 것이 아닙니다. OpenShift에서 실행되는 응용 프로그램을 개발 중이며 바인딩하는 포트를 전달하지 않는 한 ServerSockets를 사용할 수 없습니다. 웹 소켓 사용하기 8080 번 포트 (사용 가능)에서 통신 할 수 있지만 서로 통신하기 위해 2 개의 응용 프로그램 (서버)을 사용할 수는 없습니다. – Serban

+0

미안하지만 나는 도울 수 없다. OpenShift에 익숙하지 않습니다. 행운을 빕니다! –