|
//------------------------------------------------------------------------
public void run()
{
//create socket
socket = new DatagramSocket(5000);
//create arrays: one for addresses, 50
//slots long, which means the server can serve 50 users.
//slot 0 is not used.
address = new InetAddress[51];
port = new int[51];
int k;
for(k = 0; k<=50; ++k)//set the arrays to default values
{
address[k] = null;
}
//this is how much space has been taken in the arrays.
//listen for packet, receive it, then do stuff.
while(b)
{
//receive message from client.
byte[] buf = new byte[3];
DatagramPacket packet = new DatagramPacket(buf,buf.length);
socket.receive(packet);
//get the address of the client that sent the message.
InetAddress current_address = packet.getAddress();
//get string from packet.
String message_receive = new String(packet.getData(),0 ,packet.getLength());
//test the packet's message.
if(message_receive.equals("add"))
{
boolean bool = true;
int c;
for(c=0; c<=50;++c)
{
if((address[c] == null) && (bool = true))
{
address[c] = current_address;
bool = false;
}
}
}
else if(message_receive.equals("sub"))
{
int c;
for(c=0; c<=50;++c)
{
if(address[c] == current_address)
{
address[c] = null;
}
}
}
}
socket.close();
}//end while loop/listener
}//end method run().
//------------------------------------------------------------------------
what you see above is a second server I made to run simultaneously with the first.
what it does is, when the client first logs on, it sends a message to this second server, and the second server puts the address into an array.
when i start the two servers, they work fine. however, when i start the client, and try to access the array of addresses, I get this error:
"
Exception in thread "main" java.lang.NullPointerException
"
pretty much what this second server is supposed to do is store the addresses so that my first server, when add the feature to do so, will be able to use the array of Inetaddresses from the second server,sending a message to every address stored in the array.
i plan to do this by writing the firstserver to make a datagram for all 50 addresses in the second server's array.
however, because of the error above, am I doing something wrong in accessing my array from the second server?
|