java.net.SocketException:Socket被关闭TCP连接

时间:2013-05-05 03:37:55

标签: java sockets tcp

进行一些调试,我认为下面这个方法会发生错误。该方法基本上向服务器发送ID(字符串),并且在接收到该ID之后服务器查找它发送回客户端的记录。 ID显示在用户选择的GUI JList中,因此每次用户单击JList中的不同ID时,ListSelectionListener事件都会激活此方法。客户端从服务器收到记录后,会将其显示在JTextField中。当用户首次单击JList中的ID时,一切都按预期工作。但是当你点击列表中的另一条记录时,我得到了例外:

  

java.net.SocketException:Socket已关闭

编辑:现在我得到了:

  

java.net.SocketException:软件导致连接中止:recv失败

客户计划:

    import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class ClientGUI extends JFrame implements ListSelectionListener{

    //gui components
    JList jlist = null;
    String requestID = null; //Assigned to selected ID in JList. 
    JScrollPane scpane = null;
    JTextField field = null;
    JPanel pane = null;
    DefaultListModel<String> listModel = null;
    ArrayList<String> idList = null;

    //client stuff:
    Socket sock1 = null;
    Socket sock2 = null;
    ObjectInputStream in = null;
    ObjectOutputStream out = null;

    public ClientGUI() throws ClassNotFoundException{
        //get List of IDs from Server1
        try{
            sock1 = new Socket("FahadAhmed-PC", 8889); 
            in = new ObjectInputStream(sock1.getInputStream());
            idList = new ArrayList<String>(29);
            ArrayList<Customer> custList = null;
            custList = (ArrayList<Customer>) in.readObject();
            for(Customer c : custList){
                idList.add(c.getID());
            }

            in.close();
            sock1.close();

            sock2 = new Socket("FahadAhmed-PC", 8888); 
            ArrayList streams = new ArrayList(3); 
            streams.add(out = new ObjectOutputStream(sock2.getOutputStream()));
            streams.add(in = new ObjectInputStream(sock2.getInputStream()));

        }catch(UnknownHostException e) {
            System.err.println("Don't know about host: FahadAhmed-PC");
            System.exit(1);
        }catch(IOException e){
            System.err.println(e);
            System.exit(1);
        }

        //Setup GUI
        jlist = new JList(idList.toArray());
        jlist.setVisibleRowCount(10);
        scpane = new JScrollPane(jlist);
        jlist.addListSelectionListener(this);
        pane = new JPanel(new BorderLayout());
        pane.setPreferredSize(new Dimension(300, 300));
        field = new JTextField(29);
        field.setEditable(false);
        pane.add(scpane, BorderLayout.PAGE_START);
        pane.add(field, BorderLayout.PAGE_END);

        this.setContentPane(pane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String args[]) throws ClassNotFoundException{
        ClientGUI gui = new ClientGUI();
    }


    @Override
    public void valueChanged(ListSelectionEvent arg0) {
        if(!arg0.getValueIsAdjusting())
            try {
                System.out.println(jlist.getSelectedValue().toString());
                getRecord(jlist.getSelectedValue().toString());
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }

    private void getRecord(String getID) throws ClassNotFoundException {
        try{

            //System.out.println(getID + "sent to getRecord method");
            out.writeObject(getID);

            String rec = (String) in.readObject();

            field.setText(rec);
            //in.close();
            //sock2.shutdownInput();
            out.flush();
            //sock2.shutdownOutput();
            //out.close();

        }catch(UnknownHostException e) {
            System.err.println("Don't know about host: FahadAhmed-PC");
            System.exit(1);
        }catch(IOException e){
            System.err.println(e);
            System.exit(1);
        }
    }


}

2 个答案:

答案 0 :(得分:1)

关闭输入/输出流会关闭套接字。您需要在socket上使用shutdownInput方法,才能关闭输入流:

//do sth with fromSocket ... and close it 
socket.shutdownInput();
socket.shutdownOutput();

答案 1 :(得分:1)

关闭输入流或输出流会关闭套接字,我不知道你为什么在这里关闭任何东西,但这里还有另一个问题。您必须在套接字的两端使用相同的ObjectInputStreamObjectOutputStream。否则,两个对等体将不同步,您将开始看到StreamCorruptedException: invalid type code

之类的内容