socket net exception-我想用mer server同时接收和object以及arraylist

fslejnso  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(258)

我想与mer server同时接收和object以及arraylist。下面是来自发送方的代码。
我试图通过信用卡号码-我希望它是有效的在接收服务器,并发回卡的有效信息发送给发件人再次。我想用这个程序同时传递arraylist的对象,我想确保这不会妨碍它。
发件人:

try {

    int portNo=3990;
    InetAddress serverAddress=InetAddress.getLocalHost();

    Socket socket=new Socket(serverAddress,portNo);

    ObjectOutputStream ObjectOS=new ObjectOutputStream(socket.getOutputStream());
    ObjectOS.writeObject(OrderedItem);
    ObjectOS.flush();

    DataOutputStream dout=new DataOutputStream(socket.getOutputStream());  
    dout.writeUTF(textFieldCreditCard.getText());  
    dout.flush();  
    dout.close();  

    DataInputStream dis=new DataInputStream(socket.getInputStream());  
    String  str=(String)dis.readUTF();  
    System.out.println("message= "+str);

    ObjectInputStream ObjectIS=new ObjectInputStream(socket.getInputStream());
    OrderedItem=(List<OrderedItem>)ObjectIS.readObject();

    ObjectOS.close();
    ObjectIS.close();
    socket.close();

}

catch(IOException|ClassNotFoundException ex) {

    ex.printStackTrace();
}

收件人:
编译器说错误在这一行:检查我粘贴在下面的错误: ObjectOutputStream objectOS = new ObjectOutputStream(socket.getOutputStream()); 不过,我想保持这条线,因为我可能需要通过考试 arraylist 或者以后的对象。

public class Server {

    public  static void main(String[] args)  {

        try {

            int portNo=3990;                
            ServerSocket serverSocket=new ServerSocket(portNo);             
            while(true) {

                Socket socket= serverSocket.accept();

                ObjectInputStream ObjectIS= new ObjectInputStream(socket.getInputStream());

                List<OrderedItem> S=(List<OrderedItem>)ObjectIS.readObject();

                DataInputStream dis=new DataInputStream(socket.getInputStream());  
                String  str=(String)dis.readUTF();  
                System.out.println("Credit Card Number= "+str);  

                if(str.length()==14) {

                    DataOutputStream send=new DataOutputStream(socket.getOutputStream());  
                    send.writeUTF("Card Valid");  

                }
                else {

                    DataOutputStream send=new DataOutputStream(socket.getOutputStream());  
                    send.writeUTF("Card Invalid. Card Number must be 14 Digits");  

                                    }

            for(OrderedItem s:S)
            System.out.println(s.getSubTotalAmount());

    ObjectOutputStream objectOS = new ObjectOutputStream(socket.getOutputStream());

            }

        }
        catch(IOException|ClassNotFoundException ex){

            ex.printStackTrace();

        }

    }
}

错误:

java.net.SocketException: Software caused connection abort: socket write error
    at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
    at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
    at java.base/java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1883)
    at java.base/java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1792)
    at java.base/java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:248)
    at Server.main(Server.java:68)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题