gpt4 book ai didi

java - 通过 TCP/IP 接收对象

转载 作者:可可西里 更新时间:2023-11-01 02:32:24 27 4
gpt4 key购买 nike

我打算通过 TCP/IP 编写一个程序,我应该通过客户端或服务器发送对象,当我想发送或接收字符串时它是正确的,但当我试图读取一个对象时:

private Socket client;

public ThreadedClient(Socket client) {
this.client = client;
}

@Override
public void run() {
try {
ObjectInputStream objIn = new ObjectInputStream(client.getInputStream());
while(true){
try {
Object fromClient = objIn.readObject();

} catch (ClassNotFoundException e) {e.printStackTrace();}
}
} catch (IOException e) {e.printStackTrace();}
}

我收到一个异常:

java.io.StreamCorruptedException: invalid stream header: 306E6165
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at org.bihe.serverSocket.ThreadedClient.run(Server.java:137)
at java.lang.Thread.run(Unknown Source)

它指的是这一行:

    ObjectInputStream objIn = new ObjectInputStream(client.getInputStream());

这是我的服务器代码:

            ServerSocket ss = new ServerSocket(8800);
while(true){
Socket newClient = ss.accept();

System.out.println(">>>> Client number " + (++counter) + " connected.");
OutputStream outputStream = newClient.getOutputStream();
PrintWriter sender = new PrintWriter(outputStream);
sender.println(true);
sender.flush();
ThreadedClient client = new ThreadedClient(newClient);
clients.add(client);
new Thread(client).start();

客户端代码:

sc = new Socket("127.0.0.1", 8800);
InputStream inputStream = sc.getInputStream();
Scanner scanner = new Scanner(inputStream);
boolean s = scanner.nextBoolean();
if(s){
System.out.println("Client connected successfully.");
return true;
}else{
System.out.println("Ohhh, Some problem happened, try again later!");
}

任何人都可以向我解释发生了什么,这个异常是什么以及为什么我收到这个异常?

最佳答案

如果你想通过网络发送对象,你必须序列化你的对象。

检查这个问题: How To send an object over TCP in Java

Java 序列化: Serialization

你可以这样做:

import java.net.*;  
import java.io.*;

class testobject implements Serializable {
int value;
String id;

public testobject(int v, String s ){
this.value=v;
this.id=s;
}

}

public class SimpleServer {
public static void main(String args[]) {
int port = 2002;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to = (testobject)ois.readObject();
if (to!=null){System.out.println(to.id);}
System.out.println((String)ois.readObject());
is.close();
s.close();
ss.close();
}catch(Exception e){System.out.println(e);}
}
}

import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]){
try{
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to = new testobject(1,"object from client");
oos.writeObject(to);
oos.writeObject(new String("another object from the client"));
oos.close();
os.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}

关于java - 通过 TCP/IP 接收对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21073024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com