Hi
I haven't been here in a while and hadn't looked at java for 18 months, up until 3 weeks ago. I have two semesters (16 weeks) of Java behind me which i struggled with. That said I have been handed a java project out of the blue in college to complete(even though we are not doing Java this year).
For all intensive purposes this is a chat application. I have done the majority of the work on the program and can get it to work opening different instances to communicate with each other on different machines. That’s Great.
Effectively i have the client and server running on the same application.
I open the application and select start server to listen for traffic on a specific port (hard coded in for the moment) associated to the machines IP address. At this point it freezes But wait.
I can then kick of a second instance of the program and type in the the message i want to send. Hit the send button and it works, kind of(the issue here i understand and can fix). I have a very good idea of what I want to do from here to get it completed.
My problem is this! I want to be able to use the single instance of the program to start the server and sent messages to itself or, to a different Machine running the same application. This I feel I can do providing I can prevent it freezing when I start up the server. I have tried all sorts of Throwables but still no joy. At this point I have spend the last week just doing trial and error and learning nothing, which is not good.
I have attached the code below, any help would be greatly appreciated.
package chat.itb.college;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Chattie extends JFrame{
private static final long serialVersionUID = 1L;
protected static final String Double = null;
private JPanel contentPane;
private JTextField ipaddress;
private JTextField portnumber;
private JTextField conversationtext;
private JTextField senttext;
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Chattie frame = new Chattie();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
public Chattie() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 590, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
final JButton startserver = new JButton("Start Server");
startserver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
DatagramSocket udpServerSocket = null;
try {
udpServerSocket = new DatagramSocket(7777);
} catch (SocketException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while(true)
{
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
udpServerSocket.receive(receivePacket);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String clientMessage = (new String(receivePacket.getData())).trim();
conversationtext.setText("FROM CLIENT:" + clientMessage);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
sendData = ("Received: \""+clientMessage+"\" from "+IPAddress.getHostName()).getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
try {
udpServerSocket.send(sendPacket);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
startserver.setBounds(12, 39, 110, 25);
contentPane.add(startserver);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
JButton stopbutton = new JButton("Stop Server");
stopbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
stopbutton.setBounds(12, 77, 110, 25);
contentPane.add(stopbutton);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
JButton send = new JButton("Send");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DatagramSocket udpClientSocket = null;
try {
udpClientSocket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InetAddress IPAddress = null;
try {
IPAddress = InetAddress.getByName (ipaddress.getText());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String clientRequest = senttext.getText();
sendData = clientRequest.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 7777);
try {
udpClientSocket.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
udpClientSocket.receive(receivePacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String serverReply = new String(receivePacket.getData());
conversationtext.setText("FROM SERVER:" + serverReply.trim());
udpClientSocket.close();
}
});
send.setBounds(12, 195, 110, 45);
contentPane.add(send);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
JLabel ipLabel = new JLabel("Server Address");
ipLabel.setBounds(134, 4, 97, 22);
contentPane.add(ipLabel);
getContentPane().add (ipLabel);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
ipaddress = new JTextField();
ipaddress.setText("Enter Address");
ipaddress.setBounds(237, 4, 102, 22);
contentPane.add(ipaddress);
ipaddress.setColumns(10);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
JLabel portLabel = new JLabel("Port Number");
portLabel.setBounds(351, 4, 80, 22);
contentPane.add(portLabel);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
portnumber = new JTextField();
portnumber.setText("Enter Port no.");
portnumber.setColumns(10);
portnumber.setBounds(443, 4, 104, 22);
contentPane.add(portnumber);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
conversationtext = new JTextField();
conversationtext.setHorizontalAlignment(SwingConst ants.TRAILING);
conversationtext.setBounds(134, 39, 413, 147);
contentPane.add(conversationtext);
conversationtext.setColumns(10);
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
senttext = new JTextField();
senttext.setColumns(10);
senttext.setBounds(134, 195, 413, 45);
contentPane.add(senttext);
}
}