Answer the question
In order to leave comments, you need to log in
Java message sending not working
I wrote a client-server in Java. But when I send a message from the client to the server, it comes, but if I try to do it a second time, the message will not be sent. And if the client first writes a message, then the server responds, then the next message written from the client will be sent. That is, the client and the server send a message to each other only in turn and nothing more. How to fix it?
Here is the part of the code that is responsible for connecting and sending messages:
try {
while(true) {
connection = new Socket(InetAddress.getByName(ip), port);
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
JOptionPane.showMessageDialog(null, (String)input.readObject());
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == send) {
sendData(inputchat.getText());
createOutput();
}
}});
}
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (HeadlessException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
}
Answer the question
In order to leave comments, you need to log in
Well, most likely everything is very simple, and you have one thread responsible for both receiving and sending messages. Make two separate threads, this is the standard pattern.
And in general, they would show the whole code, otherwise it’s so unclear.
public static void main(String[] args) {
//new Thread(new Server()).start();
new Thread(new Main("Beer chat")).start();;
}
public Main(String name) {
super(name);
add(logo);
add(lab);
add(log);
add(login);
add(author);
add(ipserv);
setLayout(new FlowLayout());
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == login) {
username = log.getText();
System.out.println(username);
lab.hide();
log.hide();
login.hide();
add(inputchat);
add(send);
add(exit);
try {
//output.flush();
output.writeObject("Client connected: " + username);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == exit) {
sendData("disconnected");
System.exit(0);
}
}});
}
public void run() {
try {
while(true) {
connection = new Socket(InetAddress.getByName(ip), port);
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
JOptionPane.showMessageDialog(null, (String)input.readObject());;
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == send) {
sendData(inputchat.getText());
createOutput();
}
}});
}
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (HeadlessException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
}
}
public static void sendData(Object obj) {
try {
//output.flush();
output.writeObject(username + ": " + obj);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createOutput() {
try {
output = new ObjectOutputStream(connection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question