生活随笔
收集整理的這篇文章主要介紹了
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();}}
總結
以上是生活随笔為你收集整理的Socket is closed 可能原因的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。