&引用;java.io.StreamCorruptedException:无效的类型代码:AC";-从服务器客户端java程序运行客户端代码时发现错误
我试图从XMLParser中读取.xml文件,因此基本上我希望读取xml文件并将其从服务器打印到客户端,但在运行客户端代码时,我得到:&引用;java.io.StreamCorruptedException:无效的类型代码:AC";-从服务器客户端java程序运行客户端代码时发现错误,java,sockets,client,Java,Sockets,Client,我试图从XMLParser中读取.xml文件,因此基本上我希望读取xml文件并将其从服务器打印到客户端,但在运行客户端代码时,我得到: java.io.StreamCorruptedException: invalid type code: AC 客户端代码为: import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.Act
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();
}
}
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.net.InetAddress;
导入java.net.Socket;
导入java.net.UnknownHostException;
导入java.util.List;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入服务器。视频;
导入server.VideoFile;
@抑制警告(“串行”)
公共类客户端扩展JFrame{
容器内容窗格;
@抑制警告(“原始类型”)
JComboBox选择框;
列出视频;
列出视频文件;
套接字服务器套接字;
字符串host=“127.0.0.1”;
int端口=1176;
ObjectInputStream inputFromServer;
对象输出流输出;
公共客户端()引发异常{
//下面第41行
setupGUI();
System.out.println(“读取XML文件”);
MyXMLReader=新的MyXMLReader();
//video=reader.getList(“videoList.xml”);
System.out.println(“完成读取”);
}
public void openSocket()引发UnknownHostException,IOException{
/**获取服务器的地址对象*/
serverSocket=新套接字(“本地主机”,端口);
System.out.println(“连接到端口中的本地主机:“+端口”);
out=newObjectOutputStream(serverSocket.getOutputStream());
//out.flush();
inputFromServer=newObjectInputStream(serverSocket.getInputStream());
}
@抑制警告(“未选中”)
public void getListFromSocket()引发IOException,ClassNotFoundException{
试一试{
视频=(列表)inputFromServer.readObject();
}捕获(IOE异常){
e、 printStackTrace();
系统退出(-1);
}
//serverSocket.close();
}
public void runClient()引发异常{
试一试{
openSocket();
getListFromSocket();
serverSocket.close();
setupGUI();
}捕获(未知后异常e){
System.out.println(“不知道主机:+host”);
系统退出(-1);
}捕获(IOE异常){
e、 printStackTrace();
System.out.println(“无法打开I/O连接:“+host+”:“+port”);
系统退出(-1);
}catch(classnotfounde异常){
System.out.println(“未找到传入对象的类定义”);
e、 printStackTrace();
系统退出(-1);
}
}
@SuppressWarnings({“unchecked”,“rawtypes”})
私有void setupGUI()引发异常{
setTitle(“客户”);
设置大小(600400);
setVisible(真);
contentPane=getContentPane();
setLayout(新的BorderLayout());
runClient();
//下面第105行
String[]selectionListData=新字符串[video.size()];
对于(int i=0;i
完全错误是:
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)
已连接到端口1176中的本地主机
java.io.StreamCorruptedException:类型代码无效:AC
位于java.io.ObjectInputStream.readObject0(未知源)
位于java.io.ObjectInputStream.readObject(未知源)
位于client.client.getListFromSocket(client.java:61)
位于client.client.runClient(client.java:72)
在client.client.(client.java:40)
位于client.client.main(client.java:124)
谁能给我一个解决办法,我肯定这是一个小问题,但我不能弄明白
谢谢
编辑:服务器代码为:
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();
}
}
包服务器;
导入java.io.IOException;
导入java.io.ObjectOutputStream;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.List;
导入java.lang.*;
@抑制警告(“未使用”)
公共类服务器扩展线程实现可运行{
列出视频;
静态字符串时间戳;
//套脱
服务器套接字服务器套接字;
int端口=1176;
//String host=“localhost”;
插座客户端插座;
ObjectOutputStream输出客户端;
公共服务器(){
螺纹();
System.out.println(“读取XML文件”);
MyXMLReader=新的MyXMLReader();
//video=reader.getList(“videoList.xml”);
System.out.println(“完成读取”);
}
public void openSockets()引发IOException{
试一试{
serverSocket=新的serverSocket(端口);
System.out.println(“已建立套接字连接…”);
}捕获(IOE异常){
System.out.println(“无法侦听端口:“+端口”);
系统退出(-1);
}
TimeStamp=new java.ut
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)