我想从服务器端发送一个对象到客户端,但我找不到问题。这是我收到的客户端上的错误:使用TCP通过套接字从服务器端发送ArrayList到客户端
java.io.StreamCorruptedException: invalid type code: 43
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at connection.MainClient.doAll(MainClient.java:56)
at connection.TestScreen$2.actionPerformed(TestScreen.java:82)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这里下面是服务器端方法: 在无限循环的单独方法只是从文件中读取和解析的用户名和密码ArrayList的它们是Server类中的实例变量。这里下面
public void doStuff() throws Exception
{
ServerSocket serverSocket = new ServerSocket(5001);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
ObjectOutputStream objectOutput = new ObjectOutputStream(clientSocket.getOutputStream());
String inputLine;
out.println("Connected");
while(true)
{
seperate();
while ((inputLine = in.readLine()) != null)
{
if(inputLine.equalsIgnoreCase("users"))
objectOutput.writeObject(getUserNames());
else
if(inputLine.equalsIgnoreCase("pass"))
objectOutput.writeObject(getPassWords());
else
if(inputLine.equalsIgnoreCase("stop"))
objectOutput.reset();
}
}
}
是客户端从服务器请求的信息:在学习服务器插座和插座,但我想在这里做到的是这个
public boolean doAll(String name, String pass) throws Exception
{
try
{
kkSocket = new Socket("PC", 5001);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: PC.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: PC.");
System.exit(1);
}
ObjectInputStream objectInput = new ObjectInputStream(kkSocket.getInputStream());
out.println("user");
Object obj = objectInput.readObject();
users = (ArrayList<String>)obj;
out.println("pass");
obj = objectInput.readObject();
this.pass = (ArrayList<String>)obj;
out.println("stop");
objectInput.close();
out.close();
in.close();
kkSocket.close();
if(userIsRegistered(name,pass))
return true;
return false;
}
我新。当我在另一个类中按下一个按钮,我这样做:
MainClient b = new MainClient();
login = b.doAll(userField.getText(),passField.getText());
Login is obviously a boolean.
差不多一个登录系统在服务器cleint在当按下登录按钮将调用一个客户端连接到该服务器,并得到用户的列表并传递存储在指定目录中的文本文件中的服务器上。然后,当客户端收到这些信息时,它会检查它是否可能是来自我从两个文本字段获得的字符串中的用户,并返回true或false。然后关闭套接字到服务器,它应该保持重新连接每一次?
我的主要问题是你如何解决它一直抛出的错误?
类似于http://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac – Shellum 2013-03-13 17:53:00
@Chuck Norris第一次写**完美**代码。虫子害怕他的回旋踢,因此他的代码为什么没有错误。 – 2013-03-13 17:58:36
@Chuck Norris错误被认为是一个功能。所有开发人员都需要的功能。 – 2013-03-13 17:59:16