Multicast chat program

Hi,

The problem I am having is when checking to see if two specific users are using the chat program as I want them to have a separate multicast session. The if statements work and the client sends the message. The server just doesn't seem to relay it back to the client in either case.

The code for the client part is below:

if (userName.startsWith ("Steve") || userName.startsWith ("David"))

{

MulticastSocket socket = new MulticastSocket (4447);

InetAddress address = InetAddress.getByName ("233.0.0.13");

socket.joinGroup (address);

}

else

{

MulticastSocket socket = new MulticastSocket (4449);

InetAddress address = InetAddress.getByName ("232.0.0.13");

socket.joinGroup (address);

}

And the server part:

if (received.startsWith ("Steve") || received.startsWith ("David"))

{

InetAddress group = InetAddress.getByName ("233.0.0.13");

packet = new DatagramPacket (buf, buf.length, group, 4447);

socket.send (packet);

}

else

{

InetAddress group = InetAddress.getByName ("232.0.0.13");

packet = new DatagramPacket (buf, buf.length, group, 4449);

socket.send (packet);

}

Does anyone have any ideas?

[1275 byte] By [steveoelliotta] at [2007-11-26 17:08:11]
# 1
Your Multicast Sockets can never be used. Have a look at the scope in which they are declared.
ejpa at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 2
I have declared them before these parts of the programs. Everything works finr when I do not use the if statements.
steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 3
> I have declared them before these parts of the programsAnd you've redeclared them in the 'if' and 'else' blocks. Don't.
ejpa at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 4

Chat Client Full Code

public class MulticastChatClient extends MulticastChatClientGUI

{

public ButtonHandler bHandler;

DatagramSocket socket;

String ipAddress;

String userName;

String passWord;

String msgToBuf;

String logMsg= "";

String checkBye;

Boolean authenticatedUser;

String deNullString (String s)

{

//Function to get rid of garbage at the end of the buffer prior to

//displaying it on the screen and saving to file.

String result = "";

int i = 0;

while (s.charAt(i) != '\0')

{

result = result + s.charAt(i);

i ++;

}

return result;

}

public MulticastChatClient (String title)

{

super (title);

bHandler = new ButtonHandler();

sendButton.addActionListener( bHandler );

try

{

socket = new DatagramSocket ();

}

catch (IOException e)

{

}

}

private class ButtonHandler implements ActionListener

{

public void actionPerformed (ActionEvent event) //throws IOException

{

// Get a datagram socket

try

{

byte[] buf = new byte[128];

InetAddress address = InetAddress.getByName (ipAddress);

//Put username before the message the user sends

msgToBuf=userName + " Says >> ";

//Populate buffer with text in the txArea

msgToBuf=msgToBuf + txArea.getText();

buf = msgToBuf.getBytes();

//Open up port 4448 and prepare to send the packet

DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4448);

System.out.println ("About to send packet");

//Send the packet to the server

socket.send(packet);

System.out.println ("Packet sent");

}

catch (IOException e)

{

//Error handling

System.err.println("Couldn't get I/O for the connection to the server");

System.exit(1);

}

}

}

public void run () throws IOException

{

//Welcome user, ask for their name

userName = JOptionPane.showInputDialog(null,"Please enter your name...",

"Welcome To Chat V1.0",JOptionPane.QUESTION_MESSAGE);

//Password verification

//Promt user to enter password

passWord = JOptionPane.showInputDialog(null,"Please enter password...",

"User Authentication",JOptionPane.QUESTION_MESSAGE);

if(passWord.equals ("letmein"))

{

//Correct password

authenticatedUser = true;

}

else

{

//Wrong password

authenticatedUser = false;

//Inform the user and close the application

JOptionPane.showMessageDialog(null, "Wrong Password, Chat program ended",

"Authenticaion Failure",JOptionPane.INFORMATION_MESSAGE);

System.exit(1);

}

//User To Enter IP Address

ipAddress = JOptionPane.showInputDialog(null, "Thanks " +userName+ "... Please Enter Server IP",

"Connection Details",JOptionPane.QUESTION_MESSAGE);

// set up connection to multicast server

if (userName.startsWith ("Steve") || userName.startsWith ("David"))

{

MulticastSocket socket = new MulticastSocket (4447);

InetAddress address = InetAddress.getByName ("233.0.0.13");

socket.joinGroup (address);

}

else

{

MulticastSocket socket = new MulticastSocket (4449);

InetAddress address = InetAddress.getByName ("232.0.0.13");

socket.joinGroup (address);

}

while (true)

{

// send request

byte[] buf = new byte[128];

DatagramPacket packet = new DatagramPacket (buf, buf.length);

socket.receive (packet);

System.out.println ("Received packet");

// display response

String received = new String (packet.getData());

System.out.println ("Remote packet: " + received);

logMsg = logMsg + "\n" + deNullString (received);

checkBye = txArea.getText();

if (checkBye.equals ("Bye"))

{

try

{

//Create log file ClientMsg.log, outFile is used to operate on it

BufferedWriter outFile = new BufferedWriter (new FileWriter ("ClientMsg.log"));

outFile.write (logMsg);

outFile.close();

}

catch (IOException i){}

//Tell user the program is about to close

JOptionPane.showMessageDialog(null, "You terminated connection, Chat program ended",

"Session Info",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

rxArea.setText (rxArea.getText() + "\n" + deNullString (received));

}

}

Chat Server Full Code:

public class MulticastChatServer

{

public static void main(String[] args) throws java.io.IOException

{

//Open up socket and listen on port 4448

DatagramSocket socket = new DatagramSocket (4448);

DatagramPacket packet;

//Variable to hold all messages

String logMsg="";

System.out.println ("Starting Multicast Chat Server");

//Loop forever

while (true)

{

try

{

//Setup a 128 Byte array

byte[] buf = new byte[128];

// receive packet

packet = new DatagramPacket (buf, buf.length);

socket.receive (packet);

InetAddress address = packet.getAddress ();

int port = packet.getPort ();

//Convert buffer into a string

String received = new String (packet.getData());

//Add the IP address to the packet received

//received = "(" + address + ") " + received;

//Print received message on the console

System.out.println (received);

//Put the data into the buffer

buf = received.getBytes ();

//Write message to the message log variable

logMsg = logMsg + "\n" + received;

try

{

//Create log file ClientMsg.log, outFile is used to operate on it

BufferedWriter outFile = new BufferedWriter (new FileWriter ("ServerMsg.log"));

//Write the contents of logMsg to the file

outFile.write (logMsg);

//Close the file

outFile.close();

}

catch (IOException i){}

// multicast it

if (received.startsWith ("Steve") || received.startsWith ("David"))

{

System.out.println ("met");

InetAddress group = InetAddress.getByName ("233.0.0.13");

packet = new DatagramPacket (buf, buf.length, group, 4447);

socket.send (packet);

System.out.println ("met2");

}

else

{

InetAddress group = InetAddress.getByName ("232.0.0.13");

packet = new DatagramPacket (buf, buf.length, group, 4449);

socket.send (packet);

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

}

steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 5
I have given the full code in case that helps
steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 6
Replies #1 and #3 continue to apply.
ejpa at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 7

When i don't bother using the if statement to check who is chatting and just use the commands nested in the first condition of the if statement it works fine.

In the if statement I am telling it what port to use, on what address and sending the packet. If they are not all in the if statement then the message will not be sent.

steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 8
See reply #1.
ejpa at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 9
How would you go about resolving this. Please bear in mind I have very little experience and that may well be why I am coming across rather stupid.Thankyou for all your help so far.
steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 10

Steve

You're redeclaring 'socket' inside those if/else scopes, and so those blocks only operate on that local 'socket', which then disappears at the closing }. Just change this:

MulticastSocket socket = new MulticastSocket(4449); // or 4447 or whatever

to:

socket = new MulticastSocket(4449); // or 4447 or whatever

both times.

ejpa at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 11
I now get cannot resolve symbol method joinGroup java.net.InetAddress when I compile.
steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 12
So change the declaration of the original 'socket' from DatagramSocket to MulticastSocket.
ejpa at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...
# 13
Thankyou so much all is now good.Steve...
steveoelliotta at 2007-7-8 23:36:01 > top of Java-index,Core,Core APIs...