Socket is closed 可能原因

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 serverSocket;
		Socket clientSocket = null;
		String str;
		DataInputStream  In = null;
		
		public Server() {
			this.setTitle("服务器");
			this.setBounds(200,200,400,400);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setLayout(new GridLayout(3,1));
			lbl = new JLabel("等待启动");
			text = new JTextField();
			btn1 = new JButton("连接");
			btn2 = new JButton("发送");
			btn3 = new JButton("停止");
			
			
			btn2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						send();
					}catch (Exception e1){
						e1.printStackTrace();
					}
				}
			});
			
			btn3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						stop();
					}catch (Exception e1){
						e1.printStackTrace();
					}
				}
			});
			
			add(lbl);
			add(text);
			add(btn1);
			add(btn2);
			add(btn3);
		}
		
		public void server1()throws Exception{
			serverSocket = new ServerSocket(5555);
			lbl.setText("等待连接");
			clientSocket = serverSocket.accept();
			lbl.setText("连接成功");
			In = new DataInputStream(clientSocket.getInputStream());
			str = In.readUTF();
			text.setText(str);
		}
		
		public void server()throws Exception{
//			serverSocket = new ServerSocket(5555);			
//			clientSocket = serverSocket.accept();
			In = new DataInputStream(clientSocket.getInputStream());
			str = In.readUTF();
			text.setText(str);
		}

		
		
		public void send()throws Exception{
			String s = text.getText();
			//BufferedReader reader = new BufferedReader(new FileReader(inFile));
			DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
			out.writeUTF(s);
			System.out.println("服务器发送");
			//reader.close();
//			out.close();
//			clientSocket.close();
		}
		

		
		//
		public void stop()throws Exception{
			clientSocket.close();
			serverSocket.close();
			System.exit(0);
		}
	}
	
	class User extends JFrame{
		JLabel lbl;
		JTextField text;
		JButton btn1,btn2,btn3;
		ServerSocket serverSocket;
		Socket clientSocket2 = null;
		String str;
		DataInputStream  In = null;
		
		public  User() throws Exception{
			this.setTitle("客户端");
			this.setBounds(800,200,400,400);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setLayout(new GridLayout(3,1));
			lbl = new JLabel("连接中。。。");
			text = new JTextField("请输入信息:");
			btn1 = new JButton("连接");
			btn2 = new JButton("发送");
			btn3 = new JButton("停止");
			
			btn1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						connect();
					}catch (UnknownHostException e1){
						e1.printStackTrace();
					}catch(Exception e1) {
						e1.printStackTrace();
					}
				}
			});
			
			
			btn2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						send();
					}catch (Exception e1){
						e1.printStackTrace();
					}
				}
			});
			
			btn3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						stop();
					}catch (Exception e1){
						e1.printStackTrace();
					}
				}
			});
			
			add(lbl);
			add(text);
			add(btn1);
			add(btn2);
			add(btn3);
		}
		//
		public void stop()throws Exception{
			clientSocket2.close();
			serverSocket.close();
			System.exit(0);
		}
		
		public void server2()throws Exception{		
			
			In = new DataInputStream(clientSocket2.getInputStream());
			str = In.readUTF();
			text.setText(str);
		}
	
		public void connect() throws UnknownHostException,Exception{
			clientSocket2 = new Socket("127.0.0.1",5555);
			lbl.setText("成功连接到服务器");
		}
		
		public void send()throws Exception{
			String s = text.getText();
			//BufferedReader reader = new BufferedReader(new FileReader(inFile));
			DataOutputStream out = new DataOutputStream(clientSocket2.getOutputStream());
			out.writeUTF(s);
			System.out.println("客户端发送");
			//reader.close();
//			out.close();
//			clientSocket2.close();
		}
		

	}
	public static void main(String []args)throws Exception{
		TCPTalk tcp = new TCPTalk();
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章