当前位置: 技术问答>java相关
一个老问题,但我始终没有彻底解决。还请大侠帮帮我,先谢过了。[分不够可以再加]
来源: 互联网 发布时间:2015-02-10
本文导语: 其实这个问题以前已经有请教过了,后来几经折腾,有了一点改进,但问题还是没有彻底解决。特再来请教。 我的要求是点击按钮即可实现服务器端口的打开或关闭。目前程序每第一次按下打开按钮,端口可以打开...
其实这个问题以前已经有请教过了,后来几经折腾,有了一点改进,但问题还是没有彻底解决。特再来请教。
我的要求是点击按钮即可实现服务器端口的打开或关闭。目前程序每第一次按下打开按钮,端口可以打开,再按下关闭按钮,端口也可以关闭,但仅限于第一次操作。此后打开关闭按钮便不再起作用,我想可能是线程未结束的原因造成的,可就算我使用不被推荐的stop方法,依然如此。请专家指明根源,谢谢了。
源码在此:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Clk_Server
{ 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);
ServerOpen=new ServerHandler();
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.start();
}
}
}
private JButton startButton;
private JScrollPane scrollPane;
private JTextArea textArea;
private ClkServer server;
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;
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("link link link ...");
// Create a new handler.
//StockQuoteHandler newHandler = new StockQuoteHandler(clientSocket,stockInfo,stockInfoTime);
//Thread newHandlerThread = new Thread(newHandler);
//newHandlerThread.start();
}
}
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);
}
}
}
我的要求是点击按钮即可实现服务器端口的打开或关闭。目前程序每第一次按下打开按钮,端口可以打开,再按下关闭按钮,端口也可以关闭,但仅限于第一次操作。此后打开关闭按钮便不再起作用,我想可能是线程未结束的原因造成的,可就算我使用不被推荐的stop方法,依然如此。请专家指明根源,谢谢了。
源码在此:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Clk_Server
{ 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);
ServerOpen=new ServerHandler();
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.start();
}
}
}
private JButton startButton;
private JScrollPane scrollPane;
private JTextArea textArea;
private ClkServer server;
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;
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("link link link ...");
// Create a new handler.
//StockQuoteHandler newHandler = new StockQuoteHandler(clientSocket,stockInfo,stockInfoTime);
//Thread newHandlerThread = new Thread(newHandler);
//newHandlerThread.start();
}
}
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);
}
}
}
|
你程序的问题是,线程是不可以重入的。所以你在线程退出后在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;
}
所以这样就行了。
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;
}