Please help! I've spend days and nights trying to figure this out. I am setting up 2 classes (Name_Server, Site) that can send/receive message to each other. I've created a Send class that does the sending and receive class that handle the receiving. In each of the communicationg classes (NS, Site), I create a send/receive thread base on what i need to do. For days now, I've encounter this problem that I am able to send and receive (i.e. when i print out the things inside these threads, they work) but not able to extract the data after the receive (i.e. when i print out the received message, it returns null. Below are the result and the relevent codes that I have. Thanks!
(Note: I have another class with main that starts a Name Server and Site 1)
Output
++++++++NS Start+++++++++
RECEIVED MESSAGE ---> null
*** SITE 1 Start ***
RECEIVING.......SITE 1;1111 TO 1111
SENDING.....SITE 1;1111 /192.168.1.100 1111
*** SITE 1 End ***
+++++++++NS End++++++++++
Codes
import java.net.*;
import java.io.*;
import java.util.*;
class Name_Server_Class implements Runnable
{
String Name;
int ID;
int Num_Site;
int Num_Msg;
int R_Port;
Directory_Class Dir[];
DatagramSocket R_DS;
String Address;
Thread T;
Site_Class Sites[];
Name_Server_Class (int R_Port, int Num_Site, int Num_Msg, Site_Class[] Sites)
{
try
{
this.Name = "NS";
this.ID = 0;
this.R_Port = R_Port;
this.Num_Site = Num_Site;
this.Num_Msg = Num_Msg;
InetAddress Local_Address = InetAddress.getLocalHost();
Address = Local_Address.getHostAddress();
this.Sites = Sites;
Dir = new Directory_Class[Num_Site];
// Setup server listening socket
R_DS = new DatagramSocket (R_Port);
// Setup first entry in Dir for NS itself
Directory_Class Temp = new Directory_Class (ID, Name, Address, R_Port);
Dir[0] = Temp;
}
catch (Exception e)
{
System.out.println ("NS : " + e);
};
T = new Thread (this, "NS"
;
T.start();
}
public void run()
{
System.out.println ("++++++++NS Start+++++++++"
;
// Setup Directory and send info back to sites
Gather_Site_Info ();
// Wait for Sites to die before shutting down Name Server
try
{
for (int i = 1; i < Num_Site; i++)
{
Sites.T.join();
}
}
catch (Exception e)
{
System.out.println ("NS-run : " + e);
}
System.out.println ("+++++++++NS End++++++++++"
;
}
// Gather messages from sites for registration
private void Gather_Site_Info ()
{
Receive_Class Receives[] = new Receive_Class[Num_Site];
String Message[] = new String[Num_Site];
for (int i = 1; i < Num_Site; i++)
{
Receives = new Receive_Class (R_Port, R_DS);
Message = Receives.Get_Message();
}
try
{
for (int i = 1; i < Num_Site; i++)
{
// Wait for each of the Receive to get here before moving on
System.out.println ("RECEIVED MESSAGE ---> " + Message);
Receives.T.join();
}
}
catch (Exception e)
{
System.out.println ("NS-Gather_Site_Info : " + e);
}
}
}
class Site_Class implements Runnable
{
String Name;
String ID;
int Num_Site;
int Num_Msg;
int R_Port;
int NS_Port;
Directory_Class Dir[];
InetAddress Address;
String NS_Address;
Thread T;
Site_Class (String Name, int Num_Site, int Num_Msg, String NS_Address, int NS_Port, int I_Port)
{
try
{
this.Name = Name;
this.Num_Site = Num_Site;
this.Num_Msg = Num_Msg;
this.Address = InetAddress.getLocalHost();
this.NS_Port = NS_Port;
this.NS_Address = NS_Address;
this.R_Port = I_Port; // Set receiving port to I_Port initially, change after getting info from directory
Dir = new Directory_Class[Num_Site];
}
catch (Exception e)
{
System.out.println ("SITE : " + e);
};
T = new Thread (this, Name);
T.start();
}
public void run()
{
System.out.println ("*** " + Name + " Start ***"
;
Site_Setup ();
System.out.println ("*** " + Name + " End ***"
;
}
// Use to communicate with NS to register and get directory
private void Site_Setup ()
{
String Message = Name + ";" + R_Port;
// System.out.println (Message);
Send (0, Message);
}
private void Send (int Dest_ID, String Message)
{
try
{
DatagramSocket DS = new DatagramSocket ();
if (Dest_ID == 0)
{
// Use predefined NS info
// System.out.println (Message + " " + NS_Address + " " + NS_Port);
Send_Class Send = new Send_Class (Message, NS_Address, NS_Port, DS);
Send.T.join ();
}
else
{
// Extract info from Directory
int k = 1;
while ((Dir[k].ID != Dest_ID) && (k < Num_Site))
{
k++;
}
if (k == Num_Site)
{
System.out.println ("ERROR, No such destination exist"
;
}
else
{
// Use Address, Port recorded to send
Send_Class Send = new Send_Class (Message, Dir[k].Address, Dir[k].Port, DS);
Send.T.join ();
}
}
DS.close ();
}
catch (Exception e)
{
System.out.println ("SITE-Send : " + e);
}
}
}
class Send_Class implements Runnable
{
Thread T;
DatagramSocket DS;
String Message;
String Dest_Address;
int Dest_Port;
Send_Class (String Message,String Dest_Address,int Dest_Port, DatagramSocket DS)
{
this.Message = Message;
this.Dest_Address = Dest_Address;
this.Dest_Port = Dest_Port;
this.DS = DS;
T = new Thread (this, "SEND"
;
T.start ();
}
public void run ()
{
try
{
byte Buffer[] = Message.getBytes ();
InetAddress Dest_IP = InetAddress.getByName (Dest_Address);
DatagramPacket DP = new DatagramPacket (Buffer, 0, Buffer.length, Dest_IP, Dest_Port);
DS.send (DP);
System.out.println ("SENDING....." + Message + " " + Dest_IP + " " + Dest_Port);
}
catch (Exception e)
{
System.out.println ("SEND : " + e);
};
}
}
class Receive_Class implements Runnable
{
Thread T;
String Message;
String R_Address;
DatagramSocket DS;
int R_Port;
boolean End = false;
Receive_Class (int R_Port, DatagramSocket DS)
{
this.R_Port = R_Port;
this.DS = DS;
T = new Thread (this, "RECEIVE"
;
T.start ();
}
public String Get_Message()
{
End = true;
return Message;
}
public void run ()
{
try
{
byte Buffer[] = new byte [1000];
DatagramPacket DP = new DatagramPacket (Buffer, Buffer.length);
DS.receive (DP);
R_Address = DP.getAddress().toString().substring(1);
byte R_Buffer[] = DP.getData();
Message = new String (R_Buffer, 0, R_Buffer.length);
Message = Message.trim();
System.out.println ("RECEIVING......." + Message + " TO " + DS.getLocalPort());
}
catch (Exception e)
{
System.out.println ("RECEIVE : " + e);
};
}
}
(Note: I have another class with main that starts a Name Server and Site 1)
Output
++++++++NS Start+++++++++
RECEIVED MESSAGE ---> null
*** SITE 1 Start ***
RECEIVING.......SITE 1;1111 TO 1111
SENDING.....SITE 1;1111 /192.168.1.100 1111
*** SITE 1 End ***
+++++++++NS End++++++++++
Codes
import java.net.*;
import java.io.*;
import java.util.*;
class Name_Server_Class implements Runnable
{
String Name;
int ID;
int Num_Site;
int Num_Msg;
int R_Port;
Directory_Class Dir[];
DatagramSocket R_DS;
String Address;
Thread T;
Site_Class Sites[];
Name_Server_Class (int R_Port, int Num_Site, int Num_Msg, Site_Class[] Sites)
{
try
{
this.Name = "NS";
this.ID = 0;
this.R_Port = R_Port;
this.Num_Site = Num_Site;
this.Num_Msg = Num_Msg;
InetAddress Local_Address = InetAddress.getLocalHost();
Address = Local_Address.getHostAddress();
this.Sites = Sites;
Dir = new Directory_Class[Num_Site];
// Setup server listening socket
R_DS = new DatagramSocket (R_Port);
// Setup first entry in Dir for NS itself
Directory_Class Temp = new Directory_Class (ID, Name, Address, R_Port);
Dir[0] = Temp;
}
catch (Exception e)
{
System.out.println ("NS : " + e);
};
T = new Thread (this, "NS"
T.start();
}
public void run()
{
System.out.println ("++++++++NS Start+++++++++"
// Setup Directory and send info back to sites
Gather_Site_Info ();
// Wait for Sites to die before shutting down Name Server
try
{
for (int i = 1; i < Num_Site; i++)
{
Sites.T.join();
}
}
catch (Exception e)
{
System.out.println ("NS-run : " + e);
}
System.out.println ("+++++++++NS End++++++++++"
}
// Gather messages from sites for registration
private void Gather_Site_Info ()
{
Receive_Class Receives[] = new Receive_Class[Num_Site];
String Message[] = new String[Num_Site];
for (int i = 1; i < Num_Site; i++)
{
Receives = new Receive_Class (R_Port, R_DS);
Message = Receives.Get_Message();
}
try
{
for (int i = 1; i < Num_Site; i++)
{
// Wait for each of the Receive to get here before moving on
System.out.println ("RECEIVED MESSAGE ---> " + Message);
Receives.T.join();
}
}
catch (Exception e)
{
System.out.println ("NS-Gather_Site_Info : " + e);
}
}
}
class Site_Class implements Runnable
{
String Name;
String ID;
int Num_Site;
int Num_Msg;
int R_Port;
int NS_Port;
Directory_Class Dir[];
InetAddress Address;
String NS_Address;
Thread T;
Site_Class (String Name, int Num_Site, int Num_Msg, String NS_Address, int NS_Port, int I_Port)
{
try
{
this.Name = Name;
this.Num_Site = Num_Site;
this.Num_Msg = Num_Msg;
this.Address = InetAddress.getLocalHost();
this.NS_Port = NS_Port;
this.NS_Address = NS_Address;
this.R_Port = I_Port; // Set receiving port to I_Port initially, change after getting info from directory
Dir = new Directory_Class[Num_Site];
}
catch (Exception e)
{
System.out.println ("SITE : " + e);
};
T = new Thread (this, Name);
T.start();
}
public void run()
{
System.out.println ("*** " + Name + " Start ***"
Site_Setup ();
System.out.println ("*** " + Name + " End ***"
}
// Use to communicate with NS to register and get directory
private void Site_Setup ()
{
String Message = Name + ";" + R_Port;
// System.out.println (Message);
Send (0, Message);
}
private void Send (int Dest_ID, String Message)
{
try
{
DatagramSocket DS = new DatagramSocket ();
if (Dest_ID == 0)
{
// Use predefined NS info
// System.out.println (Message + " " + NS_Address + " " + NS_Port);
Send_Class Send = new Send_Class (Message, NS_Address, NS_Port, DS);
Send.T.join ();
}
else
{
// Extract info from Directory
int k = 1;
while ((Dir[k].ID != Dest_ID) && (k < Num_Site))
{
k++;
}
if (k == Num_Site)
{
System.out.println ("ERROR, No such destination exist"
}
else
{
// Use Address, Port recorded to send
Send_Class Send = new Send_Class (Message, Dir[k].Address, Dir[k].Port, DS);
Send.T.join ();
}
}
DS.close ();
}
catch (Exception e)
{
System.out.println ("SITE-Send : " + e);
}
}
}
class Send_Class implements Runnable
{
Thread T;
DatagramSocket DS;
String Message;
String Dest_Address;
int Dest_Port;
Send_Class (String Message,String Dest_Address,int Dest_Port, DatagramSocket DS)
{
this.Message = Message;
this.Dest_Address = Dest_Address;
this.Dest_Port = Dest_Port;
this.DS = DS;
T = new Thread (this, "SEND"
T.start ();
}
public void run ()
{
try
{
byte Buffer[] = Message.getBytes ();
InetAddress Dest_IP = InetAddress.getByName (Dest_Address);
DatagramPacket DP = new DatagramPacket (Buffer, 0, Buffer.length, Dest_IP, Dest_Port);
DS.send (DP);
System.out.println ("SENDING....." + Message + " " + Dest_IP + " " + Dest_Port);
}
catch (Exception e)
{
System.out.println ("SEND : " + e);
};
}
}
class Receive_Class implements Runnable
{
Thread T;
String Message;
String R_Address;
DatagramSocket DS;
int R_Port;
boolean End = false;
Receive_Class (int R_Port, DatagramSocket DS)
{
this.R_Port = R_Port;
this.DS = DS;
T = new Thread (this, "RECEIVE"
T.start ();
}
public String Get_Message()
{
End = true;
return Message;
}
public void run ()
{
try
{
byte Buffer[] = new byte [1000];
DatagramPacket DP = new DatagramPacket (Buffer, Buffer.length);
DS.receive (DP);
R_Address = DP.getAddress().toString().substring(1);
byte R_Buffer[] = DP.getData();
Message = new String (R_Buffer, 0, R_Buffer.length);
Message = Message.trim();
System.out.println ("RECEIVING......." + Message + " TO " + DS.getLocalPort());
}
catch (Exception e)
{
System.out.println ("RECEIVE : " + e);
};
}
}