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:
And the second server, Server2;
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
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
