Java Sockets question

Soldato
Joined
9 Dec 2004
Posts
5,700
Location
Dorset
I have two servers that run other stuff, but in the meantime I'd like them to talk to each other in the background;

Server1:

Code:
public static void main (String [] args) throws Exception {    
        try {
        System.out.println("Server starting....");
        
        DatagramSocket socket = new DatagramSocket(3591);
        
        System.out.println("Opening socket 3591 for incoming/outgoing connections");
          
        String poll = null;
        String time = null;
        String server_id = null;
        int server2Port = 3592;
        boolean server2_up = true;
        boolean running = true;
        
        while (running == true){
        Thread.currentThread().sleep(3000);
        poll = "1";
        String combined = poll;
        socket.setSoTimeout(10000);
        byte [] message = poll.getBytes();
        InetAddress hostname = InetAddress.getLocalHost();
        DatagramPacket sendreq = new DatagramPacket(message, combined.length(), hostname, server2Port);
        socket.send(sendreq);
        System.out.println("Polling server2....");

And the second server, Server2;

Code:
public static void main (String [] args) throws Exception { 
        System.out.println("Starting server2....");
        boolean server2up = true;
        try {
            while (server2up == true){
        
        DatagramSocket socket = new DatagramSocket(3592);
//buffer for polling messages
            byte[] buffer = new byte[10000];
            int server3_port = 3593;
            int server1_port = 3591;
            InetAddress hostname = InetAddress.getLocalHost();

                DatagramPacket request = new DatagramPacket(buffer, buffer.length);
                // Receive: server will be blocked here until a request arrives
                socket.receive(request);
                System.out.println("Message recieved...");
                String poll = new String(request.getData());
               
                //System.out.println(poll);
                
                if (poll == "1"){
                    System.out.println("Message recieved..." + poll);
                    String response = "2";
                    byte [] message = response.getBytes();
                    DatagramPacket reply = new DatagramPacket(message, message.length, 
                        hostname, request.getPort());
                    socket.send(reply);
                
            }
            }
        }
                catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
    }

What happens is that Server1 happily sends a request to Server2, which it recieves and can display. Yet just before the IF statement in Server2 it dies and the error I catch says;

Address already in use

Am I totally missing the point here with the sockets? I'm sure I'm trying to do it the correct way :(

Server1 opens socket 3591, Server2 opens socket 3592, they chat to and from the respective sockets. At the moment both servers are running on one machine (hence getLocalHost().

Any ideas would be great :(
 
I should probably mention what this is for.

I'm trying to keep 3 chat servers talking to each other so as when one crashes, they other's are aware and can assume some kind of master status.
Currently I'm working on the basis that Server1 polls Server2, Server2 polls Server3 and Server3 polls Server1. When one crashes the Server polling it times out and will alter some data in a database (which is used for client connection details when they log in).

Do you think there is a simpler method for me to do this? Perhaps RMI? The reason I'm using sockets is a) Because I suck at programming and I know this more than RMI b) I understand sockets gives a finer level of control over things like Timeout's etc (which would be essential going by my plan).
 
you need to design your chat system a bit better, plonking everything in a main method isnt the way you should do it. you might also want to think about multiple threads for chatting, as you can only have one connection per socket. if you need more help explain what you want a bit more, and il paste some code which may help. i done a multithreaded java messenger for my final year project :P
 
Back
Top Bottom