English 中文(简体)
“java.io.Stream CorruptedException: 无效型代码:AC”-Error在从服务器-客户 j方案[复制]管理客户代码时发现
原标题:"java.io.StreamCorruptedException: invalid type code: AC"- Error found while running the client code from the server-client java program [duplicate]

Well, I am trying to read a .xml file from the XMLParser so basically I want to read the XML files and print it out from the server to the client, but while running the client code i am getting:

java.io.StreamCorruptedException: invalid type code: AC

客户代码是:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;

import server.Video;
import server.VideoFile;

@SuppressWarnings("serial")
public class Client extends JFrame{
    Container contentPane;
    @SuppressWarnings("rawtypes")
    JComboBox selectionBox;

    List<Video> video;
    List<VideoFile> videoFile;

    Socket serverSocket;
    String host = "127.0.0.1";
    int port = 1176;
    ObjectInputStream inputFromServer;
    ObjectOutputStream out;

    public Client() throws Exception {
        // line 41 below
            setupGUI();

        System.out.println("Reading the XML File");
        MyXMLReader reader = new MyXMLReader();
        //video = reader.getList("videoList.xml");
        System.out.println("Finished reading");
    }

    public void openSocket() throws UnknownHostException, IOException {
         /** Obtain an address object of the server */
        serverSocket = new Socket("localhost", port);
        System.out.println("Connected to localhost in port: " + port);
        out = new ObjectOutputStream(serverSocket.getOutputStream());
        //out.flush();
        inputFromServer = new ObjectInputStream(serverSocket.getInputStream());

    }

    @SuppressWarnings("unchecked")
    public void getListFromSocket() throws IOException, ClassNotFoundException {
        try{
        video = (List<Video>)inputFromServer.readObject();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        //serverSocket.close();
    }
    public void runClient() throws Exception {

            try {
                openSocket();
                getListFromSocket();
                serverSocket.close();
                setupGUI();
                } catch (UnknownHostException e) {
                System.out.println("Don t know about host : " + host);
                System.exit(-1);
                } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Couldn t open I/O connection : " + host + ":" + port);
                System.exit(-1);
                } catch (ClassNotFoundException e) {
                System.out.println("Class definition not found for incoming object.");
                e.printStackTrace();
                System.exit(-1);
            }

    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void setupGUI() throws Exception{
        setTitle("A Client");
        setSize(600, 400);
        setVisible(true);
        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

            runClient();

        // line 105 below
        String [] selectionListData = new String[video.size()];
        for (int i = 0; i < video.size(); i++) {
        selectionListData[i] = video.get(i).getFilename();
        selectionListData[i] = video.get(i).getID();
        selectionListData[i] = video.get(i).getTitle();
        System.out.println(selectionListData);
        }
        selectionBox = new JComboBox(selectionListData);
        selectionBox.setSelectedIndex(0);
        add(selectionBox, BorderLayout.NORTH);
        selectionBox.addActionListener((ActionListener) this);

        validate();
    }

    public void actionPerformed(ActionEvent e) {
        @SuppressWarnings("rawtypes")
        JComboBox comboBox = (JComboBox)e.getSource();
        String selectedTitle = (String)comboBox.getSelectedItem();
        System.out.println("Selected title : " + selectedTitle);
        }


    public static void main(String[] args) throws IOException, Exception {
            // line 127 below
        new Client();

    }

}

全部错误是:

    Connected to localhost in port: 1176
    java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at client.Client.getListFromSocket(Client.java:61)
    at client.Client.runClient(Client.java:72)
    at client.Client.<init>(Client.java:40)
    at client.Client.main(Client.java:124)

任何人都能够这样做,我确信,这是小事,但我可以这样说。

Thanks

EDIT:服务器代码:

package server;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.lang.*;

@SuppressWarnings("unused")
public class Server extends Thread implements Runnable{
    List<Video> video;
    static String TimeStamp;
    //Socket Declerations
    ServerSocket serverSocket;
    int port = 1176;
    //String host = "localhost";
    Socket clientSocket;
    ObjectOutputStream outputToClient;



    public Server() {
        Thread();
        System.out.println("Reading the XML File");
        MyXMLReader reader = new MyXMLReader();
        //video = reader.getList("videoList.xml");
        System.out.println("Finished reading");
    }

    public void openSockets() throws IOException {
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("Socket Connection Established....");
        } catch (IOException e) {
            System.out.println("Could not listen on port : " + port);
            System.exit(-1);
        }
        TimeStamp = new java.util.Date().toString();
        System.out.println("Opened socket on : " + port + ", waiting for client. " + TimeStamp);

        try {
            clientSocket = serverSocket.accept();
            //System.out.println("Sending to Client.....");
            } catch (IOException e) {
            System.out.println("Could not accept client.");
            System.exit(-1);
            }
            outputToClient = new ObjectOutputStream(clientSocket.getOutputStream());
    }
    public void writeToSocket() throws IOException {
        outputToClient = new ObjectOutputStream(clientSocket.getOutputStream());
        outputToClient.writeObject(video);
        System.out.println(outputToClient);
    }

    public void Thread() {
        Thread socketThread;
        socketThread = new Thread("Socket") {
        public void run() {
            try {
                openSockets();
                writeToSocket();
                clientSocket.close();
                serverSocket.close();
            } catch (IOException e) {
                System.out.println("Error on sockets connection.");
                e.printStackTrace();
            }
        }
};
socketThread.start();
}

    public static void main(String[] args) throws IOException {
        new Server();
        //new Thread();


    }

}

EDIT: 删除重复条目后的新错误:

Exception in thread "main" java.lang.NullPointerException
    at client.Client.setupGUI(Client.java:105)
    at client.Client.<init>(Client.java:41)
    at client.Client.main(Client.java:127)
最佳回答

您将在同一张服务器舱面上翻开标语,使特别头盔被送两倍。

最后一行<代码>outputToClient = 新的目标OutputStream(客户Socket.getOutputStream());

www.un.org/spanish/ecosoc 第1行:<代码>output ToClient = 新的目标OutputStream(客户Socket.getOutputStream());

消除你们想要的东西,应该做的是罚款。

至于谁是谁,你现在接受我感觉到,录像在客户中是无效的。 在法典中,你将这一点说得了,因为在服务器中,你永远不会制造一个视频阵列,因此客户只是拿不到。

问题回答

这一错误是由于使用了不止一个<代码>造成的。 ObjectOutputStream on the same socket. 在上述两端使用相同的<代码>ObjectOutputstream和。 大部分其他流体和读物/作者类型,但如果不这样做的后果通常更加神秘。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...