1

Is there a way to get a websocket server to connect to another websocket server? I wrote this snippet in Java but it doesn't work. I don't get any errors or exceptions, it just waits forever to connect.

@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();
        }
    }

Basically, I want this server to initialize a new websocket connection to another server at another address. However, the program stops when it tries to make a new connection. Is there a flaw in my thinking or is this kind of connection impossible?

Ciri
  • 371
  • 1
  • 6
  • 19

1 Answers1

0

You may find this useful. This is one of my older socket programs which I used to communicate between client and server. I have attached both of client and code for the program which would send XML files. You will however, need to edit a few things in order to get her workin' for you. Play this with file and get a feel for sockets and apply it to your program. Happy Learnings my friend!

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();
        }
    }
  • Thank you for the reply. This is, however, not what I am looking for. I'm developing applications which run on OpenShift and I cannot use ServerSockets, unless I forward the ports I am binding. Using WebSockets I can communicate on the port 8080 (which is available) but I can't get 2 applications (servers) to talk to each other. – Ciri Jun 19 '14 at 21:48
  • Sorry I cannot help then. I'm not familiar with OpenShift. Good luck! –  Jun 20 '14 at 14:17