请看代码
不知为何只要建立Socket连接,就会阻塞住,界面出不来。
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;public class ServerForm extends javax.swing.JFrame {
private ServerSocket serverSocket;
/** Creates new form ServerForm */
public ServerForm() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(93, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(104, Short.MAX_VALUE))
); pack();
}// </editor-fold> /**
* @param args the command line arguments
*/
public static void main(String args[]) {
new ServerForm().setVisible(true);
System.out.println("oh ~~yeah!");
try {
ServerSocket ss = new ServerSocket(7777);
while (true) {
Socket s = ss.accept();
new ServerThread(s).start();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("oh ~~no!");
} // Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration }
不知为何只要建立Socket连接,就会阻塞住,界面出不来。
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;public class ServerForm extends javax.swing.JFrame {
private ServerSocket serverSocket;
/** Creates new form ServerForm */
public ServerForm() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(93, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(104, Short.MAX_VALUE))
); pack();
}// </editor-fold> /**
* @param args the command line arguments
*/
public static void main(String args[]) {
new ServerForm().setVisible(true);
System.out.println("oh ~~yeah!");
try {
ServerSocket ss = new ServerSocket(7777);
while (true) {
Socket s = ss.accept();
new ServerThread(s).start();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("oh ~~no!");
} // Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration }
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.zhang7nan;import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author Administrator
*/
public class ServerThread extends Thread { Socket s;
OutputStream os = null;
InputStream is = null; public ServerThread(Socket s) {
this.s = s;
} @Override
public void run() { try {
os = s.getOutputStream();
is = s.getInputStream(); //send welcome msg
os.write("welcome to connect the server!".getBytes());
byte[] bytes = new byte[1024];
int k = is.read(bytes);
System.out.println(new String(bytes, 0, k));
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
} public void cut() {
try {
is.close();
os.close();
s.close();
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
问题就是每次运行的时候Socket服务器可以建立~但是SWing写的界面就出不来~
我觉得有可能是阻塞了~但是我找不到问题的根本~
不要在Swing的事件模型中进行。
要学会使用多线程技术,进行swing的编程工作。
ServerForm里没有IO操作~
while (true) {
Socket s = ss.accept();
new ServerThread(s).start();
}
换成:
new Thread(new Runnable()
{
public void run()
{
while (true) {
Socket s = ss.accept();
new ServerThread(s).start();}
}).start();没编译环境,没实验过,不过思想就是这样,具体语法有问题的话,自己修改一下。
曾经也遇到过这类问题,原因就是Socket的IO操作和Swing界面发生阻塞,使用多线程可以解决就是把Socket的IO操作另起一个线程,不要和Swing在同一个线程中,这样界面就不会卡死 while (true) {
Socket s = ss.accept();
new ServerThread(s).start();
}改为用多线程实现就应该oK了试试吧