Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread and Datagrams

Status
Not open for further replies.

hunghsun

Programmer
Aug 25, 2002
31
US
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 (&quot;NS-run : &quot; + e);
}

System.out.println (&quot;+++++++++NS End++++++++++&quot;);
}

// 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 (&quot;RECEIVED MESSAGE ---> &quot; + Message);
Receives.T.join();
}
}
catch (Exception e)
{
System.out.println (&quot;NS-Gather_Site_Info : &quot; + 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 (&quot;SITE : &quot; + e);
};

T = new Thread (this, Name);
T.start();
}

public void run()
{
System.out.println (&quot;*** &quot; + Name + &quot; Start ***&quot;);

Site_Setup ();

System.out.println (&quot;*** &quot; + Name + &quot; End ***&quot;);
}

// Use to communicate with NS to register and get directory
private void Site_Setup ()
{
String Message = Name + &quot;;&quot; + 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 + &quot; &quot; + NS_Address + &quot; &quot; + 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 (&quot;ERROR, No such destination exist&quot;);
}
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 (&quot;SITE-Send : &quot; + 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, &quot;SEND&quot;);
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 (&quot;SENDING.....&quot; + Message + &quot; &quot; + Dest_IP + &quot; &quot; + Dest_Port);
}
catch (Exception e)
{
System.out.println (&quot;SEND : &quot; + 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, &quot;RECEIVE&quot;);
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 (&quot;RECEIVING.......&quot; + Message + &quot; TO &quot; + DS.getLocalPort());
}
catch (Exception e)
{
System.out.println (&quot;RECEIVE : &quot; + e);
};
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top