要开一个线程来 接受消息。 public class ServerGUI extends javax.swing.JFrame implements Runnable private AtomicBoolean startState = new AtomicBoolean(false); public void run() { BufferedReader bf = null;//定义一个BufferedReader类型的读内容的引用 InputStreamReader inputSR = null;//定义一个可读输入流 try { welcomeSocket = new ServerSocket(Integer.valueOf(monitor_port.getText())); assert this.welcomeSocket.isBound(); if (this.welcomeSocket.isBound()) { System.out.println("SERVER inbound data port " + this.welcomeSocket.getLocalPort() + " is ready and waiting for client to connect..."); } } catch (SocketException e) { System.out.println("Unable to create socket."); } catch (IOException ioe) { System.out.println("Unable to read data from an open socket."); } try { while (true) { Socket connectionSocket; connectionSocket = welcomeSocket.accept(); //System.out.println("新连接建立!" + connectionSocket.getInetAddress()); inputSR = new InputStreamReader(connectionSocket.getInputStream()); BufferedReader inFromClient = new BufferedReader(inputSR); receive_Contents.append(inFromClient.readLine()); inputSR.close(); inFromClient.close(); connectionSocket.close(); } } catch (IOException e) { System.out.println("Unable to read data from an open socket."); } finally { try { welcomeSocket.close(); } catch (IOException e) { System.out.println("Unable to close an open socket."); } } } private void start_MonitorActionPerformed(java.awt.event.ActionEvent evt) { if (startState.compareAndSet(false, true)) { Thread t = new Thread(this); t.setDaemon(true); t.start(); } }
public class ServerGUI extends javax.swing.JFrame implements Runnable private AtomicBoolean startState = new AtomicBoolean(false); public void run() {
BufferedReader bf = null;//定义一个BufferedReader类型的读内容的引用
InputStreamReader inputSR = null;//定义一个可读输入流
try {
welcomeSocket = new ServerSocket(Integer.valueOf(monitor_port.getText()));
assert this.welcomeSocket.isBound();
if (this.welcomeSocket.isBound()) {
System.out.println("SERVER inbound data port "
+ this.welcomeSocket.getLocalPort()
+ " is ready and waiting for client to connect...");
}
} catch (SocketException e) {
System.out.println("Unable to create socket.");
} catch (IOException ioe) {
System.out.println("Unable to read data from an open socket.");
} try {
while (true) {
Socket connectionSocket;
connectionSocket = welcomeSocket.accept();
//System.out.println("新连接建立!" + connectionSocket.getInetAddress());
inputSR = new InputStreamReader(connectionSocket.getInputStream());
BufferedReader inFromClient = new BufferedReader(inputSR);
receive_Contents.append(inFromClient.readLine());
inputSR.close();
inFromClient.close();
connectionSocket.close();
}
} catch (IOException e) {
System.out.println("Unable to read data from an open socket.");
} finally {
try {
welcomeSocket.close();
} catch (IOException e) {
System.out.println("Unable to close an open socket.");
}
}
}
private void start_MonitorActionPerformed(java.awt.event.ActionEvent evt) {
if (startState.compareAndSet(false, true)) {
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
}
}