java - 与 Swing GUI 的 Java 套接字连接冻结
问题描述
我对java很陌生,我想在客户端和服务器之间建立一个套接字连接。没有 GUI,它工作得很好。但是现在我已经尝试为程序实现 GUI,每当我尝试打开套接字时,服务器端界面都会冻结,即使程序仍在运行,所以只有界面冻结。每当客户端完成连接时,服务器 GUI 将立即输出所有冻结的内容。我做了一些测试,我认为是方法,server.accept()
但我确定。
Client.java 代码
public void waiting(int port) {
try
{
ServerWindow.consolePrint("Server starting up...", 2);
System.err.print("hello world");
server=new ServerSocket(port);
ServerWindow.consolePrint("Server running!", 6);
ServerWindow.consolePrint("Waiting for a client to connect...", 2);
client= server.accept();
ServerWindow.consolePrint("Client connected!", 6);
server.close();
inFromClient=new BufferedReader(new InputStreamReader(client.getInputStream()));
outToClient=new DataOutputStream(client.getOutputStream());
communicate();
}
//catch code
}
public void communicate()
{
try {
outToClient.writeBytes("type /help to display all the commands"+'\n');
ServerWindow.consolePrint("Waiting for a command from the client...", 2);
stringReceived=inFromClient.readLine();
ServerWindow.consolePrint(stringReceived, 1);
client.close();
}
//catch code
摇摆图形用户界面代码
btnConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//if - else if formatting check
else {
server.waiting(Integer.parseInt(TxtPort.getText()));
}
//Input metod for printing string into the console
public static void consolePrint(String strToConsole, int n) {
SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
switch (n) {
case 1:
// CLIENT MESSAGE
StyleConstants.setForeground(style, Color.yellow);
try {
doc.insertString(doc.getLength(), formatter.format(date) + " - Client: ",style);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
StyleConstants.setForeground(style, Color.white);
try {
doc.insertString(doc.getLength(), strToConsole + "\n" ,style);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
break;
case 2:
// SERVER MESSAGE
StyleConstants.setForeground(style, Color.cyan);
try {
doc.insertString(doc.getLength(), formatter.format(date) + " - Server: ",style);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
StyleConstants.setForeground(style, Color.white);
try {
doc.insertString(doc.getLength(), strToConsole + "\n" ,style);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
break;
case 3:
// CLIENT ERROR MESSAGE
//code
break;
case 4:
// SERVER ERROR MESSAGE
try {
//code
break;
case 5:
// CLIENT SUCCESS MESSAGE
try {
//code
break;
case 6:
// SERVER SUCCESS MESSAGE
try {
//code
break;
default:
//code
}
}
MainS.java 代码
public static void main(String[] args) {
ServerWindow windowGUI = new ServerWindow();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
windowGUI.frmServer.setVisible(true);
windowGUI.frmServer.setResizable(false);
windowGUI.frmServer.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
我正在链接 2 张图片以显示只有 UI 冻结
解决方案
推荐阅读
- reactjs - 动态创建一个由 N 个 ListItemTexts 组成的 ListItem
- flask - 未找到应用程序。在视图函数中工作或推送应用程序上下文。使用数据库中的数据创建下拉列表
- vue.js - 在食谱项目中编辑成分
- codeigniter - 消息:数组到字符串的转换,错误 Codeigniter
- websocket - websocket掩码的安全方面
- python - Kivy 安卓粉碎 | 网址请求
- c# - 如何包含另一个相关对象以使用 Entity Framework 进行查询并将其显示在 DataGridView 中?
- assembly - RISC-V 中的高效矩阵乘法
- python - 将 for 循环转换为 while 循环。我的问题在最底部,因为我想不出更好的问题格式
- python - 谜语求解器 Python