我创建了多聊天应用程序。
当前问题
当我将课程Chatinterface
编译2次时,会打开2个客户端。现在当我尝试通过在文本区域中键入文本消息进行通信时。另一个客户端不显示消息,反之亦然。
有人可以更正我的代码吗?并告诉我发生了什么。
请不要苛刻我只是初学者。
请注意我已提供服务器代码以获取详细说明。
package stack;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
class Chatinterface implements ActionListener,Runnable {
private JFrame chatwindow;
JTextArea typemsg;
JTextArea displaymsg;
JButton sendbutton;
JButton logoutbutton;
Socket client;
BufferedReader read;
PrintWriter write;
String getmsg;
String gettext;
public Chatinterface(){
try {
client = new Socket("localhost",3420);
write = new PrintWriter(client.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
displaygui();
}
private void displaygui() {
chatwindow = new JFrame();
chatwindow.setResizable(false);
chatwindow.setBounds(100, 100, 450, 300);
chatwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
chatwindow.getContentPane().setLayout(springLayout);
typemsg = new JTextArea();
springLayout.putConstraint(SpringLayout.NORTH, typemsg, 171, SpringLayout.NORTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, typemsg, 10, SpringLayout.WEST, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, typemsg, -10, SpringLayout.SOUTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, typemsg, 340, SpringLayout.WEST, chatwindow.getContentPane());
chatwindow.getContentPane().add(typemsg);
displaymsg = new JTextArea();
displaymsg.setWrapStyleWord(true);
displaymsg.setEditable(false);
springLayout.putConstraint(SpringLayout.NORTH, displaymsg, 10, SpringLayout.NORTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, displaymsg, 0, SpringLayout.WEST, typemsg);
springLayout.putConstraint(SpringLayout.SOUTH, displaymsg, 160, SpringLayout.NORTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, displaymsg, 0, SpringLayout.EAST, typemsg);
chatwindow.getContentPane().add(displaymsg);
sendbutton = new JButton("Send");
springLayout.putConstraint(SpringLayout.NORTH, sendbutton, -76, SpringLayout.SOUTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, sendbutton, 5, SpringLayout.EAST, typemsg);
springLayout.putConstraint(SpringLayout.SOUTH, sendbutton, -27, SpringLayout.SOUTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, sendbutton, 82, SpringLayout.EAST, typemsg);
chatwindow.getContentPane().add(sendbutton);
sendbutton.addActionListener(this);
sendbutton.setActionCommand("S");
logoutbutton = new JButton("Logout");
springLayout.putConstraint(SpringLayout.NORTH, logoutbutton, 50, SpringLayout.NORTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, logoutbutton, -65, SpringLayout.EAST, sendbutton);
springLayout.putConstraint(SpringLayout.SOUTH, logoutbutton, 108, SpringLayout.NORTH, chatwindow.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, logoutbutton, 0, SpringLayout.EAST, sendbutton);
chatwindow.getContentPane().add(logoutbutton);
logoutbutton.addActionListener(this);
logoutbutton.setActionCommand("L");
chatwindow.setVisible(true);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Chatinterface method = new Chatinterface();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t1 = new Thread();
t1.start();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("S"))
{
String gettext2;
gettext2 = typemsg.getText();
write.println(gettext2);
displaymsg.append(gettext2+"\n");
typemsg.setText("");
}
if(arg0.getActionCommand().equals("L")){
System.exit(0);
}
}
@Override
public void run() {
String readmsg;
try {
read = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
readmsg = read.readLine();
while(readmsg!=null)
{
displaymsg.append(readmsg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以下是每当连接新客户端时调用的类处理程序。
package stack;
import java.io.*;
import java.net.*;
public class Handler implements Runnable {
Socket s;
String getmsg = null;
String sendmsg;
public Handler(Socket s) {
this.s = s;
}
public void run() {
BufferedReader reader1 = null;
PrintWriter writer1 = null;
try {
reader1 = new BufferedReader(new InputStreamReader(
s.getInputStream()));
writer1 = new PrintWriter(s.getOutputStream(), true);
sendmsg = reader1.readLine();
while (sendmsg!=null) {
writer1.println(sendmsg);
}
reader1.close();
writer1.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是主服务器类
package stack;
import java.net.*;
public class Servera {
public static void main(String[] args) throws Exception {
Socket s;
ServerSocket server = new ServerSocket(3420);
while(true)
{
s = server.accept();
Handler handler1 = new Handler(s);
Thread t1 = new Thread(handler1);
t1.start();
}
}
}
答案 0 :(得分:0)
您的服务器未收到客户端发送的消息。为此您需要使用 write.flush()在客户端中刷新OutputStream 以下是代码更改:
if(arg0.getActionCommand().equals("S"))
{
String gettext2;
gettext2 = typemsg.getText();
write.println(gettext2);
write.flush()
displaymsg.append(gettext2+"\n");
typemsg.setText("");
}
此外,您从一个客户端向另一个客户端发送消息的逻辑也不正确。根据您的代码,服务器会将消息发送到已收到消息的同一客户端。这需要维护客户端套接字池和然后将消息广播给所有客户。希望这些回答你的问题。 如果正确,请接受答案。