我需要制作一个 tcp/ip 通信程序,当向我的按钮添加操作时,它说有未处理的异常,但我无法将 throws IOExceptions 添加到按钮操作功能。如何让它发挥作用?
public void actionPerformed(ActionEvent e) {
try{
int port = Integer.parseInt(txtport.getText());
ServerSocket listener = new ServerSocket(port);
String message = txtsend.getText();
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(message); }
finally {
socket.close();
}}}
finally {
listener.close();
}}
catch(Exception e1){}
}
});