gpt4 book ai didi

带有 GUI 的 java ChatServer

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:42 27 4
gpt4 key购买 nike

我正在尝试使用 GUI 组件实现聊天服务器。我实现了 3 个部分(服务器、客户端和 GUI 组件)。

以下是我遇到的几个问题-

  • 我无法用 GUI 代码包装代码。
  • 与客户交谈时,只有当每个人都只输入一行并按回车键时,它才会起作用。

代码:

第一个 GUI 组件:

public class ChatServer extends javax.swing.JFrame {
String str;

public ChatServer() {
initComponents();
screen.setEditable(false);
}

private void sendActionPerformed(java.awt.event.ActionEvent evt) {
str = enter.getText();
enter.setText("");
screen.append(str+"\n");

}

public static void main(String args[]) {
new ChatServer().setVisible(true);
}

private javax.swing.JTextPane enter;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea screen;
private javax.swing.JButton send;

}

它看起来像:

enter image description here

这是我的服务器代码:

public class Server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(23);
System.out.println(InetAddress.getLocalHost()+" hazir");
while(true){
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostName() + " baglandi");
new ServerPart(s).start();
}
}

}

public class ServerPart extends Thread {
private Socket s;
public ServerPart(Socket s){
this.s=s;
}

@Override
public void run() {

try {

PrintStream ps = new PrintStream(s.getOutputStream());

ps.println("Hello" + s.getInetAddress().getHostName());

String gelen;

while(true){

Scanner sc = new Scanner(s.getInputStream());

gelen = sc.nextLine();

if(gelen.trim().equalsIgnoreCase("bye"))
break;


System.out.println("Client: " + gelen);

BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );

ps.println("Server: " + input.readLine());

}

s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}

客户端代码:

public class Client extends Thread {
private Socket s;
public Client(Socket s){
this.s=s;
}

@Override
public void run() {
try {

PrintStream ps = new PrintStream(s.getOutputStream());

Scanner sc = new Scanner(s.getInputStream());

ps.println("Hello" + s.getInetAddress().getHostName());

String gelen;

while(true){

BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );

gelen = sc.nextLine();

if(gelen.trim().equalsIgnoreCase("bye"))
break;

System.out.println("Client: " + gelen);
ps.println("Server: " + input.readLine());


}

s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {

Socket s = new Socket("192.168.1.173", 23);

new Client(s);

}

}

如果你能帮助我,我将不胜感激。

最佳答案

您正在使用 PrintStream 类,方法为 ps.readline();。如果您正在开发聊天应用程序,那么这种方法将不起作用,因为 readline 方法在发现换行符或文件结尾时终止流,或者在回车即回车时终止流。所以我更喜欢使用.. datainputstream 和 dataoutputstream --

Socket sc = new Socket("address",port);
DataOutputStream daos;
DataInputStream dis;
dis = new DataInputStream(sc.getInputStream());
daos= new DataOutputStream(sc.getOutputStream());

关于带有 GUI 的 java ChatServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7175990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com