I have a Java code that is a chat.
The purpose of the code is for a client to connect, the server knows which client is connected and send a notice to all the clients informing who is connected and then start chatting with the clients that are connected.
The first client that connects sends the messages to the server, from the server to the second client and the second client receives the message.
When the second client sends a message, it also sends it to the server and the server sends it to the first client.
The problem is that the first client does not receive the second client's message.
Here is my code to see if anyone can help me with this:
Client:
public class PageFrameClient extends JPanel implements Runnable{
private JTextField field1;
private JLabel nick;
private JComboBox<String> ip;
private JTextArea fieldchat;
private static int n_ip=0;
private JButton mybutton;
public PageFrameClient(){
//------------MAKE THE CHAT FRAME-----------------
String nick_user=JOptionPane.showInputDialog("Nick Name: ");
JLabel n_nick=new JLabel("Nick: ");
add(n_nick);
nick=new JLabel();
nick.setText(nick_user);
add(nick);
JLabel text=new JLabel(" Online: ");
add(text);
ip=new JComboBox();
add(ip);
fieldchat=new JTextArea(12,20);
add(fieldchat);
field1=new JTextField(20);
add(field1);
mybutton=new JButton("Send");
//-------------------------------------------
SendText myevent=new SendText(field1,nick,ip,fieldchat);//esta clase es cuando aprietan el boton Enviar el crea concecion con el servidor para enviar mensages
mybutton.addActionListener(myevent);//Button to send all the data
add(mybutton);
Thread thread=new Thread(this);
thread.start();
}
@Override
public void run() {//este hilo es para reecivir respuestas del servidor
// TODO Auto-generated method stub
try {
ServerSocket s_client=new ServerSocket(9090);
SendPackage recivePackage;
while(true) {
//-------------STREAM FROM SERVER TO CLIENT---------------
Socket client=s_client.accept();
fieldchat.append("\nIs Accepted");
ObjectInputStream entryStream=new ObjectInputStream(client.getInputStream());
recivePackage=(SendPackage) entryStream.readObject();
fieldchat.append("\n Recive the msg");
//-------------RECIVE THE MESSAGE---------------
if(!recivePackage.getMessage().equals(" online")) {
fieldchat.append("\n"+ recivePackage.getNick() + ": "+ recivePackage.getMessage());
}
//----------------------------------------------
else {
//---------ADD IPS TO COMBOBOX------------
ArrayList<String>MenuIps=new ArrayList<String>();
MenuIps=recivePackage.getIps();
ip.removeAllItems();
for(String z:MenuIps) {
ip.addItem(z);
//----------------------------------------
fieldchat.append("\n"+ recivePackage.getIps().get(n_ip) +" Online");
n_ip ++;}
fieldchat.append("\n Ready to Close");
}
//-------------------------------------------------------
}
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Class that sends text to the server:
public class SendText implements ActionListener {
private JTextField field1;
private JComboBox ip;
private JLabel nick;
private JTextArea fieldchat;
private InetAddress address ;
public SendText(JTextField _field,JLabel _nick,JComboBox _ip,JTextArea _fieldchat) {
this.field1=_field;
this.nick=_nick;
this.ip=_ip;
this.fieldchat=_fieldchat;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fieldchat.append("\n You: "+ field1.getText());
try {
//--------SEND PACKAGE NICK , IP , MESSAGE TO SERVER-----------
Socket mysocket=new Socket("10.0.11.14",1500);//ip of server
System.out.println(InetAddress.getLocalHost());
SendPackage data =new SendPackage();
data.setNick(nick.getText());
data.setIp(ip.getSelectedItem().toString());
data.setMessage(field1.getText());
ObjectOutputStream data_package=new ObjectOutputStream(mysocket.getOutputStream());
data_package.writeObject(data);
mysocket.close();
//------------------------------------------------------------------
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
}
}
}
Server:
public class FrameServer extends JFrame implements Runnable{
private JTextArea textarea;
public FrameServer() {
setBounds(1200,300,280,350);
JPanel mypage= new JPanel();
mypage.setLayout(new BorderLayout());
textarea=new JTextArea();
mypage.add(textarea,BorderLayout.CENTER);
add(mypage);
setVisible(true);
Thread thread1=new Thread(this);
thread1.start();
}
@Override
public void run() {//Recive los mensages de los clientes
// TODO Auto-generated method stub
try {
ServerSocket server=new ServerSocket(1500);
String nick,ip,message;
ArrayList<String>listIp=new ArrayList<String>();
SendPackage recive_package=new SendPackage();
while(true) {
Socket mysocket=server.accept();
ObjectInputStream data_package=new ObjectInputStream(mysocket.getInputStream());
recive_package=(SendPackage) data_package.readObject();
nick=recive_package.getNick();
ip=recive_package.getIp();
message=recive_package.getMessage();
if(!message.equals(" online")) {
textarea.append("\n"+ nick +" send: '"+ message +"' to: "+ ip);
Socket sendRecipient=new Socket (ip,9090);//encia mensages al destinatario
ObjectOutputStream resendPackage =new ObjectOutputStream(sendRecipient.getOutputStream());
resendPackage.writeObject(recive_package);
System.out.println("Send The msg to ip :" + ip);
resendPackage.close();
sendRecipient.close();
mysocket.close(); } else {
//----------------DETECT ONLINE-------------------
InetAddress locate=mysocket.getInetAddress();
System.out.println("Inet:"+mysocket.getInetAddress().getHostAddress()+" Local: "+ mysocket.getLocalAddress().getHostAddress());
String RemoteIp= locate.getHostAddress();
System.out.println("Online "+ RemoteIp);
listIp.add(RemoteIp);
recive_package.setIps(listIp);
for(String z:listIp) {
System.out.println(z);
Socket sendRecipient=new Socket (z,9090);
ObjectOutputStream resendPackage =new ObjectOutputStream(sendRecipient.getOutputStream());
resendPackage.writeObject(recive_package);
resendPackage.close();
sendRecipient.close();
mysocket.close();
}
//-------------------------------------------------
}
}
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}