日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Socket is closed 可能原因

發(fā)布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Socket is closed 可能原因 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Exception in thread “main” java.net.SocketException: Socket is closed

io流關(guān)閉的同時socket也會關(guān)閉。
程序可以在最后關(guān)閉io流與socket流或者單獨設(shè)置一個函數(shù)隔離開來。
以下程序是通過TCP實現(xiàn)客戶端和服務(wù)器間的通信對話

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("服務(wù)器");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("發(fā)送");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("服務(wù)器發(fā)送");//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("發(fā)送");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("成功連接到服務(wù)器");}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("客戶端發(fā)送");//reader.close(); // out.close(); // clientSocket2.close();}}public static void main(String []args)throws Exception{TCPTalk tcp = new TCPTalk();}}

總結(jié)

以上是生活随笔為你收集整理的Socket is closed 可能原因的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。