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?
# 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();
}
}
}
}