java 求助:怎样在下面服务器程序中加一段代码:以实现,运行服务器后弹出一个Frame ,有个按钮可以关闭程
importjava.net.*;importjava.io.*;importjava.net.URL;importjava.sql.*;importjava.awt.*...
import java.net.*;
import java.io.*;
import java.net.URL;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class TestFrame extends Frame {
Panel p = new Panel();
TextArea msg = new TextArea();
Button button1 = new Button();
TestFrame(){
setTitle("服务器");
setSize(550,300);
addWindowListener(new WindowAdapter(){
public void windowCLosing(WindowEvent e){
System.exit(0);
}
}) ;
//add(msg);
p.add(button1);
setVisible(true);
//show();
}
}
public class MyMultiServer{
public static void main(String[] args) throws IOException {
//TestFrame tf = new TestFrame();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int ss = Integer.parseInt(s);
int clientNo=1;
ServerSocket server=new ServerSocket(10000);
//if(ss==1){server.close();}
//else{
try{
System.out.println("服务器程序启动,开始监听客户的请求...");
while(true){
Socket socket=server.accept();
System.out.println("接受客户"+clientNo+":"+socket.getInetAddress()+"的连接请求,开始通信...");
new MySingleServer(socket,clientNo);
clientNo++;
if(clientNo>2)
{server.close();}
}
}
catch(IOException e){
System.err.println(e);
}
finally{
server.close();
}
// }
}
}
class MySingleServer extends Thread {
private Socket socket;
private int clientNo;
// InsertRecord ir = new InsertRecord();
public MySingleServer(Socket s,int No){
socket=s;
clientNo=No;
start();
}
public void run(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(true){
String line = in.readLine();
//char[] lineChar = line.toCharArray();
// out.println("your line is:"+line);
System.out.println("从客户"+clientNo+"接收到数据:"+line);
if(true)break;
else{
if(line.trim().equals("stop")){
System.out.println("stop");
}
//ir.insert(line);
}
}
}
catch(IOException e){
System.err.println(e);
}
finally{
System.out.println("与客户"+clientNo+"通信结束!");
try {
socket.close();
}
catch (IOException e){
System.out.println("socket未能正确关闭!");
}
}
}
} 展开
import java.io.*;
import java.net.URL;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class TestFrame extends Frame {
Panel p = new Panel();
TextArea msg = new TextArea();
Button button1 = new Button();
TestFrame(){
setTitle("服务器");
setSize(550,300);
addWindowListener(new WindowAdapter(){
public void windowCLosing(WindowEvent e){
System.exit(0);
}
}) ;
//add(msg);
p.add(button1);
setVisible(true);
//show();
}
}
public class MyMultiServer{
public static void main(String[] args) throws IOException {
//TestFrame tf = new TestFrame();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int ss = Integer.parseInt(s);
int clientNo=1;
ServerSocket server=new ServerSocket(10000);
//if(ss==1){server.close();}
//else{
try{
System.out.println("服务器程序启动,开始监听客户的请求...");
while(true){
Socket socket=server.accept();
System.out.println("接受客户"+clientNo+":"+socket.getInetAddress()+"的连接请求,开始通信...");
new MySingleServer(socket,clientNo);
clientNo++;
if(clientNo>2)
{server.close();}
}
}
catch(IOException e){
System.err.println(e);
}
finally{
server.close();
}
// }
}
}
class MySingleServer extends Thread {
private Socket socket;
private int clientNo;
// InsertRecord ir = new InsertRecord();
public MySingleServer(Socket s,int No){
socket=s;
clientNo=No;
start();
}
public void run(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(true){
String line = in.readLine();
//char[] lineChar = line.toCharArray();
// out.println("your line is:"+line);
System.out.println("从客户"+clientNo+"接收到数据:"+line);
if(true)break;
else{
if(line.trim().equals("stop")){
System.out.println("stop");
}
//ir.insert(line);
}
}
}
catch(IOException e){
System.err.println(e);
}
finally{
System.out.println("与客户"+clientNo+"通信结束!");
try {
socket.close();
}
catch (IOException e){
System.out.println("socket未能正确关闭!");
}
}
}
} 展开
3个回答
展开全部
定义一个boolean isRunning = true; 点击按钮把isRunning设成false,程序里的while(true) 换成while(isRunning)就行
追问
能再具体点吗,谢谢。
牵扯到 局部变量的定义 ,我自己试着都 不很成功啊
追答
你的程序我没怎么仔细看,好像里面有些问题。主要是改MySingleServer,用你原来的程序象征性的改了下
class MySingleServer extends Thread {
private boolean isRunning = true;
private Socket socket;
private int clientNo;
// InsertRecord ir = new InsertRecord();
public MySingleServer(Socket s,int No){
socket=s;
clientNo=No;
start();
}
public void stopServer() {
isRunning = false;
}
public void run(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(isRunning){
String line = in.readLine();
//char[] lineChar = line.toCharArray();
// out.println("your line is:"+line);
System.out.println("从客户"+clientNo+"接收到数据:"+line);
if(true)break;
else{
if(line.trim().equals("stop")){
System.out.println("stop");
}
//ir.insert(line);
}
}
}
catch(IOException e){
System.err.println(e);
}
finally{
System.out.println("与客户"+clientNo+"通信结束!");
try {
socket.close();
}
catch (IOException e){
System.out.println("socket未能正确关闭!");
}
}
}
展开全部
程序看着太恼火了,我就不看程序了,想要弹出对话框关闭程序可以使用
int result = JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
if(result == JOptionPane.OK_OPTION){
System.exit(0);//强制关闭程序
}
int result = JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
if(result == JOptionPane.OK_OPTION){
System.exit(0);//强制关闭程序
}
更多追问追答
追问
不行 啊,关键我的服务器有个 while(true)的监听在里面,要保证关闭前监听是正常的啊
追答
你是放在什么地方的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.exit(0);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询