求救!小弟写了一个java的服务端部分,但是无法正常关闭。
2017-01-03 05:24
253 查看
写了一个socket服务端程序,但是加入界面之后,程序关闭之后端口一直无法关闭。好像是socket端口关闭有问题,求修正方法源程序如下code=Java]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
public class Se1 extends WindowAdapter implements ActionListener {
private ServerSocket server;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
JFrame f;
JPanel p;
JButton b1, b2, b3;
JTextField tf;
public Se1() {
f = new JFrame("Socked Server");
b1 = new JButton("Agree!");
b1.setActionCommand("agree");
b1.addActionListener(this);
b2 = new JButton("Disagree!");
b2.setActionCommand("disagree");
b2.addActionListener(this);
b3 = new JButton("Connect!");
b3.setActionCommand("connect");
b3.addActionListener(this);
p = new JPanel();
p.add(b1);
p.add(b2);
p.add(b3);
f.getContentPane().add(p, "North");
tf = new JTextField();
f.getContentPane().add(tf, "Center");
f.setSize(400, 150);
f.setVisible(true);
try {
server = new ServerSocket(10000);
while (true) {
socket = server.accept();
String RemoteIp = socket.getInetAddress().getHostAddress();
String RemotePort = " : " + socket.getLocalPort();
tf.setText("有学生想申请使用投影仪! IP: " + RemoteIp + " Port"
+ RemotePort);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
String line = in.readLine();
String[] student = line.split(",");
tf.setText("该学生名字: " + student[0] + " 座位号: " + student[1]
+ "\n" + " 是否允许该学生使用投影仪?");
// System.out.println("该学生名字 : " + line);
// out.println("your message received " );
// String st = JOptionPane.showInputDialog("permit or not ?");
// System.out.println("是否允许该学生使用投影仪?");
// BufferedReader st = new BufferedReader(new
// InputStreamReader(System.in));
//
// out.println(st.readLine());
//
// out.close();
// in.close();
// socket.close();
}
} catch (IOException ex) {
out.println("出错");
}
}
public static void main(String[] args) {
Server be = new Server();
// be.Server();
}
public void actionPerformed(ActionEvent e) {
String s1 = "同意请求!", s5 = "本机连接";
String s2 = "拒绝请求!";
String s3 = "yes", s4 = "no";
if (e.getActionCommand() == "agree") {
tf.setText(s1);
out.println(s3);
// out.close();
} else if (e.getActionCommand() == "disagree") {
tf.setText(s2);
out.println(s4);
// out.close();
} else if (e.getActionCommand() == "connect") {
java.lang.Process pro = null;
Runtime rt = Runtime.getRuntime();
tf.setText("本机打开投影仪");
String s = "\"C:\\NSC.exe\"";
try {
pro = rt.exec(s);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/*public void windowClosed(WindowEvent e) {
try {
in.close();
out.close();
socket.close();
} catch (IOException ex) {
out.println("出错");
}
System.exit(0);
}*/
}
[/code]
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
public class Se1 extends WindowAdapter implements ActionListener {
private ServerSocket server;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
JFrame f;
JPanel p;
JButton b1, b2, b3;
JTextField tf;
public Se1() {
f = new JFrame("Socked Server");
b1 = new JButton("Agree!");
b1.setActionCommand("agree");
b1.addActionListener(this);
b2 = new JButton("Disagree!");
b2.setActionCommand("disagree");
b2.addActionListener(this);
b3 = new JButton("Connect!");
b3.setActionCommand("connect");
b3.addActionListener(this);
p = new JPanel();
p.add(b1);
p.add(b2);
p.add(b3);
f.getContentPane().add(p, "North");
tf = new JTextField();
f.getContentPane().add(tf, "Center");
f.setSize(400, 150);
f.setVisible(true);
try {
server = new ServerSocket(10000);
while (true) {
socket = server.accept();
String RemoteIp = socket.getInetAddress().getHostAddress();
String RemotePort = " : " + socket.getLocalPort();
tf.setText("有学生想申请使用投影仪! IP: " + RemoteIp + " Port"
+ RemotePort);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
String line = in.readLine();
String[] student = line.split(",");
tf.setText("该学生名字: " + student[0] + " 座位号: " + student[1]
+ "\n" + " 是否允许该学生使用投影仪?");
// System.out.println("该学生名字 : " + line);
// out.println("your message received " );
// String st = JOptionPane.showInputDialog("permit or not ?");
// System.out.println("是否允许该学生使用投影仪?");
// BufferedReader st = new BufferedReader(new
// InputStreamReader(System.in));
//
// out.println(st.readLine());
//
// out.close();
// in.close();
// socket.close();
}
} catch (IOException ex) {
out.println("出错");
}
}
public static void main(String[] args) {
Server be = new Server();
// be.Server();
}
public void actionPerformed(ActionEvent e) {
String s1 = "同意请求!", s5 = "本机连接";
String s2 = "拒绝请求!";
String s3 = "yes", s4 = "no";
if (e.getActionCommand() == "agree") {
tf.setText(s1);
out.println(s3);
// out.close();
} else if (e.getActionCommand() == "disagree") {
tf.setText(s2);
out.println(s4);
// out.close();
} else if (e.getActionCommand() == "connect") {
java.lang.Process pro = null;
Runtime rt = Runtime.getRuntime();
tf.setText("本机打开投影仪");
String s = "\"C:\\NSC.exe\"";
try {
pro = rt.exec(s);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/*public void windowClosed(WindowEvent e) {
try {
in.close();
out.close();
socket.close();
} catch (IOException ex) {
out.println("出错");
}
System.exit(0);
}*/
}
[/code]
相关文章推荐
- CLOSE_WAIT导致服务端无法正常连接(JAVA、SOCKET相关)
- 在eclipse中可以正常运行,但是打包之后就无法通过java运行了
- 我用vs2005写了一个webservice用来查询sql2005数据库中表值,返回一个dataset类型集合,测试时候显示的xml文档没有问题,但是当我添加web引用的时候却无法正常显示而且报错原因之一。
- JAVA NIO导致JAVA服务端程序无法关闭的奇怪问题
- 由关闭通知导致Toast无法正常显示到Java中的反射
- 调用部署在本机IIS服务器的WCF服务并返回数据DataTable,一直报错:远程主机强迫关闭了一个现有的连接,但是服务访问正常
- 同一个javaweb工程,部分页面在一个tomcat中能够正常交互,而在另一个tomcat无法正常使用
- deployJava.js的一个缺憾:无法正确检测客户端JRE
- 在window 2003 下安装 Macromedia Flash 8 无法正常启动,读取字体后自动关闭
- 做个像tocmat那样,写一个startup.bat启动一个java应用,再写一个shutdown.bat来关闭该应用
- 正常调试一个web项目的java代码
- IE下部分activex控件无法正常调用的设置
- IE下部分activex控件无法正常调用的设置
- 用户控件包含一个搜索功能,但是在调用页面中按回车时无法直接提交这个表单,只有鼠标点击才提交,如何解决?
- java.sql.SQLException: 当事务仍处于活动状态时,无法关闭连接。解决办法
- 【Java】编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
- 求助!毕业设计——用java开发一个搜索引擎(小弟在迷茫中)
- 关于在httpHandlers增加了一个,但是URL中包含空格,无法处理
- 某些品牌或型号的便携式计算机,如果在睡眠时处于关闭盖板的状态,无法正常恢复?
- 解决vista系统部分CHM打不开,“无法正常显示”的问题