Exception in thread “main” java.net.SocketException: Socket is closed
io流关闭的同时socket也会关闭。
程序可以在最后关闭io流与socket流或者单独设置一个函数隔离开来。
以下程序是通过TCP实现客户端和服务器间的通信对话
package shiyan5;
import java.io.*;
import java.net.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TCPTalk {
public TCPTalk() throws Exception{
Server s = new Server();
User u = new User();
s.setVisible(true);
u.setVisible(true);
s.server1();
while(true) {
u.server2();
s.server();
}
}
class Server extends JFrame{
JLabel lbl;
JTextField text;
JButton btn1,btn2,btn3;
ServerSocket