可能是close把ServerOpen对象给清除了吧!!!
解决方案 »
- 紧急求救:java程序没有错误,但是在NetBeans中运行出现下面的错误?
- 如何求小于该数的质数
- JPanel使用GridLayout布局不能显示panel中加入的标签?
- 求教帖子~懂swing和GUI的进~高手救命~
- 用FileWriter写文件的问题
- 各位前辈,问一问题,什么时候会用到 DataInputStrean,PrintWrite
- 怎么调用Borland JBuilder的Look And Feel?
- 借着人气,请大家帮我看看访问Access异常问题,多谢~!
- 启动JBoss 3.0的时候,报告端口被占用,Why?
- 用jb如何编写给指定信箱发送e_mail,各位大哥帮帮忙,我快不行了
- 编译一个很简单的程序出错,大家帮忙看看,多谢了。
- 帮忙找个工作。
所以这样就行了。
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);
}
}
}