一个老问题,但我始终没有彻底解决。还请大侠帮帮我,先谢过了。[分不够可以再加] 可能是close把ServerOpen对象给清除了吧!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你程序的问题是,线程是不可以重入的。所以你在线程退出后在start是不行的。所以这样就行了。class ServerFrame extends JFrame implements ActionListener{ public ServerFrame() { JPanel p=new JPanel(); startButton =new JButton("´ò¿ª"); p.add(startButton); startButton.addActionListener(this); getContentPane().add(p,"South"); textArea=new JTextArea(8,40); scrollPane=new JScrollPane(textArea); getContentPane().add(scrollPane,"Center"); setTitle("·þÎñÆ÷"); setSize(500,300); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent evt) { Object source=evt.getSource(); if (source==startButton) { if (startButton.getText()!="´ò¿ª") { startButton.setText("´ò¿ª"); ServerOpen.shutoff(); } else { startButton.setText("¹Ø±Õ"); ServerOpen=new ServerHandler(); ServerOpen.start(); } } } private JButton startButton; private JScrollPane scrollPane; private JTextArea textArea; private ServerHandler ServerOpen; private Thread newHandlerThread;} 线程原来是不可以重入的。你的方法我试过了,可以打开关闭控制。不过还有一个问题请教你,在点击了关闭之后,服务器的socket端口的确是关闭了,可为什么原来已经打开的线程,还是处于打开状态。例如我将ThreadedEchoHandler方法加入后,点击关闭按钮,原来已经打开的socket还是可以通信,怎样做到将原来的一起关闭呢?import java.io.*;import java.net.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Clk_Server2{ public static void main(String[] args) { JFrame frame=new ServerFrame(); frame.show(); }}class ServerFrame extends JFrame implements ActionListener{ public ServerFrame() { JPanel p=new JPanel(); startButton =new JButton("打开"); p.add(startButton); startButton.addActionListener(this); getContentPane().add(p,"South"); textArea=new JTextArea(8,40); scrollPane=new JScrollPane(textArea); getContentPane().add(scrollPane,"Center"); setTitle("服务器"); setSize(500,300); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent evt) { Object source=evt.getSource(); if (source==startButton) { if (startButton.getText()!="打开") { startButton.setText("打开"); ServerOpen.shutoff(); } else { startButton.setText("关闭"); ServerOpen=new ServerHandler(); ServerOpen.start(); } } } private JButton startButton; private JScrollPane scrollPane; private JTextArea textArea; private ServerHandler ServerOpen; private Thread newHandlerThread;} class ServerHandler extends Thread //implements Runnable { private static final int SERVER_PORT=8189; private static final int MAX_CLIENTS=100; private boolean keepRunning=false; private ServerSocket listenSocket=null; public void run() { Socket clientSocket = null; int i = 1; try { listenSocket=new ServerSocket(SERVER_PORT,MAX_CLIENTS); while (true) { try { sleep(5); } catch(InterruptedException e) { System.err.println("Sleep InterruptedException : "+e); } // Accept a new client. clientSocket = listenSocket.accept(); System.out.println("Spawning " + i); new ThreadedEchoHandler(clientSocket, i).start(); i++; } } catch(IOException excpt) { System.err.println("Unable to listen on port "+SERVER_PORT+" : "+excpt); //shutoff(); //return; } } protected void status(boolean Status) { keepRunning = Status; } public void shutoff() { try { //System.out.println("Socket Closed _shutoff."); listenSocket.close(); } catch(IOException excpt) { System.err.println("Failed I/O: "+ excpt); } }}class ThreadedEchoHandler extends Thread{ public ThreadedEchoHandler(Socket i, int c) { incoming = i; counter = c; } public void run() { try { BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */); out.println( "Hello! Enter BYE to exit." ); boolean done = false; while (!done) { String str = in.readLine(); if (str == null) done = true; else { out.println("Echo (" + counter + "): " + str); if (str.trim().equals("BYE")) done = true; } } incoming.close(); } catch (Exception e) { System.out.println(e); } } private Socket incoming; private int counter;} to:lusxiao(lusxiao),分我一定会给你的,只不过不想让帖子这么快消失。3q! 因为你只是关闭了listenSocket,但clientSocket并没有被关闭。所以当点了关闭按钮后客户端还可以和ThreadedEchoHandler通信。解决方法: 关闭clientSocket,这样你就要保存每个客户端的clientSocket,一种方法是用clientSocket数组实现。 杀线程(不推荐)。明白? 一种实现方法:class ServerHandler extends Thread //implements Runnable { private static final int SERVER_PORT=8189; private static final int MAX_CLIENTS=100; private boolean keepRunning=false; private ServerSocket listenSocket=null; private Vector clientSocketVect=new Vector(MAX_CLIENTS); public void run() { int i = 1; Socket clientSocket=null; try { listenSocket=new ServerSocket(SERVER_PORT,MAX_CLIENTS); while (true) { try { sleep(5); } catch(InterruptedException e) { System.err.println("Sleep InterruptedException : "+e); } // Accept a new client. clientSocket = listenSocket.accept(); clientSocketVect.add(clientSocket); new ThreadedEchoHandler(clientSocket, i).start(); System.out.println("Spawning " + i); i++; } } catch(IOException excpt) { System.err.println("Unable to listen on port "+SERVER_PORT+" : "+excpt); //shutoff(); //return; } } protected void status(boolean Status) { keepRunning = Status; } public void shutoff() { try { //System.out.println("Socket Closed _shutoff."); listenSocket.close(); for (Enumeration e=clientSocketVect.elements() ; e.hasMoreElements() ;) { ((Socket) e.nextElement()).close(); } } catch(IOException excpt) { System.err.println("Failed I/O: "+ excpt); } }} java树的拆分 循环 想一个panel中添加多个按钮出错。。 很简单的小程序, 求解 怎样实现输入各种数据 请问正则表达式中的\ () ' {} 怎么表示? 问个专家级问题 我用JAVA调用VC++ OCX但是出现异常,请看看帮帮忙!! 如何在Map的pair中,存取两个字符串并进行操作和比较 谁来帮我看看这段代码。拉兄弟一把吧(多线程问题-在线等) From C++ To Java 怎样给回答我的问题的人送分? 编译一个很简单的程序出错,大家帮忙看看,多谢了。 帮忙找个工作。
所以这样就行了。
class ServerFrame extends JFrame implements ActionListener
{ public ServerFrame()
{ JPanel p=new JPanel();
startButton =new JButton("´ò¿ª");
p.add(startButton);
startButton.addActionListener(this);
getContentPane().add(p,"South");
textArea=new JTextArea(8,40);
scrollPane=new JScrollPane(textArea);
getContentPane().add(scrollPane,"Center");
setTitle("·þÎñÆ÷");
setSize(500,300);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if (source==startButton)
{
if (startButton.getText()!="´ò¿ª")
{
startButton.setText("´ò¿ª");
ServerOpen.shutoff();
}
else
{
startButton.setText("¹Ø±Õ");
ServerOpen=new ServerHandler();
ServerOpen.start();
}
}
} private JButton startButton;
private JScrollPane scrollPane;
private JTextArea textArea;
private ServerHandler ServerOpen;
private Thread newHandlerThread;
}
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Clk_Server2
{ public static void main(String[] args)
{
JFrame frame=new ServerFrame();
frame.show();
}
}class ServerFrame extends JFrame implements ActionListener
{ public ServerFrame()
{ JPanel p=new JPanel();
startButton =new JButton("打开");
p.add(startButton);
startButton.addActionListener(this);
getContentPane().add(p,"South");
textArea=new JTextArea(8,40);
scrollPane=new JScrollPane(textArea);
getContentPane().add(scrollPane,"Center");
setTitle("服务器");
setSize(500,300);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if (source==startButton)
{
if (startButton.getText()!="打开")
{
startButton.setText("打开");
ServerOpen.shutoff();
}
else
{
startButton.setText("关闭");
ServerOpen=new ServerHandler();
ServerOpen.start();
}
}
} private JButton startButton;
private JScrollPane scrollPane;
private JTextArea textArea;
private ServerHandler ServerOpen;
private Thread newHandlerThread;
} class ServerHandler extends Thread
//implements Runnable
{
private static final int SERVER_PORT=8189;
private static final int MAX_CLIENTS=100;
private boolean keepRunning=false;
private ServerSocket listenSocket=null;
public void run()
{ Socket clientSocket = null;
int i = 1;
try
{
listenSocket=new ServerSocket(SERVER_PORT,MAX_CLIENTS);
while (true)
{ try
{
sleep(5);
}
catch(InterruptedException e)
{ System.err.println("Sleep InterruptedException : "+e);
}
// Accept a new client.
clientSocket = listenSocket.accept();
System.out.println("Spawning " + i);
new ThreadedEchoHandler(clientSocket, i).start();
i++;
}
}
catch(IOException excpt)
{ System.err.println("Unable to listen on port "+SERVER_PORT+" : "+excpt);
//shutoff();
//return;
}
}
protected void status(boolean Status)
{
keepRunning = Status;
}
public void shutoff()
{ try
{ //System.out.println("Socket Closed _shutoff.");
listenSocket.close();
}
catch(IOException excpt)
{
System.err.println("Failed I/O: "+ excpt);
}
}
}class ThreadedEchoHandler extends Thread
{ public ThreadedEchoHandler(Socket i, int c)
{ incoming = i; counter = c; } public void run()
{ try
{ BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(incoming.getOutputStream(), true /* autoFlush */); out.println( "Hello! Enter BYE to exit." ); boolean done = false;
while (!done)
{ String str = in.readLine();
if (str == null) done = true;
else
{ out.println("Echo (" + counter + "): " + str); if (str.trim().equals("BYE"))
done = true;
}
}
incoming.close();
}
catch (Exception e)
{ System.out.println(e);
}
} private Socket incoming;
private int counter;
}
解决方法:
关闭clientSocket,这样你就要保存每个客户端的clientSocket,一种方法是用clientSocket数组实现。
杀线程(不推荐)。
明白?
class ServerHandler extends Thread
//implements Runnable
{
private static final int SERVER_PORT=8189;
private static final int MAX_CLIENTS=100;
private boolean keepRunning=false;
private ServerSocket listenSocket=null;
private Vector clientSocketVect=new Vector(MAX_CLIENTS); public void run()
{
int i = 1;
Socket clientSocket=null;
try
{
listenSocket=new ServerSocket(SERVER_PORT,MAX_CLIENTS);
while (true)
{ try
{
sleep(5);
}
catch(InterruptedException e)
{ System.err.println("Sleep InterruptedException : "+e);
}
// Accept a new client.
clientSocket = listenSocket.accept();
clientSocketVect.add(clientSocket);
new ThreadedEchoHandler(clientSocket, i).start();
System.out.println("Spawning " + i);
i++;
}
}
catch(IOException excpt)
{ System.err.println("Unable to listen on port "+SERVER_PORT+" : "+excpt);
//shutoff();
//return;
}
} protected void status(boolean Status)
{
keepRunning = Status;
}
public void shutoff()
{ try
{ //System.out.println("Socket Closed _shutoff.");
listenSocket.close();
for (Enumeration e=clientSocketVect.elements() ; e.hasMoreElements() ;) {
((Socket) e.nextElement()).close();
}
}
catch(IOException excpt)
{
System.err.println("Failed I/O: "+ excpt);
}
}
}