Java套接字-自定义对象发送
我已经创建了工作套接字,当我尝试发送文本或数字时,没问题,但当我尝试发送自定义类对象时,我得到了NullPointerException。。。 下面是一些代码:Java套接字-自定义对象发送,java,sockets,object,arraylist,Java,Sockets,Object,Arraylist,我已经创建了工作套接字,当我尝试发送文本或数字时,没问题,但当我尝试发送自定义类对象时,我得到了NullPointerException。。。 下面是一些代码: public boolean SendLi(List<Entity> list) { try { out.writeObject(list); } catch (IOException e) { e.printStackTrace(); System.err.
public boolean SendLi(List<Entity> list)
{
try {
out.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Send: Error on OutputStream.write(byte[])");
}
return true;
}
public List<Entity> RecvLi()
{
List<Entity> data;
data = new ArrayList<Entity>();
try{
data = (List<Entity>) in.readObject();
} catch (IOException e) {
System.err.println("Send: Error on OutputStream.read(byte[]) - IOException");
return null;
} catch (ClassNotFoundException e) {
System.err.println("Send: Error on OutputStream.read(byte[]) - ClassNotFound");
e.printStackTrace();
}
return data;
}
public boolean SendLi(列表)
{
试一试{
out.writeObject(列表);
}捕获(IOE异常){
e、 printStackTrace();
System.err.println(“发送:OutputStream.write上的错误(字节[])”;
}
返回true;
}
公共列表RecvLi()
{
列出数据;
数据=新的ArrayList();
试一试{
.readObject()中的数据=(列表);
}捕获(IOE异常){
System.err.println(“发送:OutputStream.read上的错误(字节[])-IOException”);
返回null;
}catch(classnotfounde异常){
System.err.println(“发送:OutputStream.read(byte[])-ClassNotFound上的错误”);
e、 printStackTrace();
}
返回数据;
}
实际上,我为列表编写的代码,但我想使用一些similar函数来发送其他对象。
谢谢你的回复 在通过套接字发送自定义类之前,需要对其进行序列化。要发送的对象
out.writeObject(list);
如果要序列化,您的类应该实现java.io.Serializable接口下面的代码片段对我来说适用于所有类型的可序列化对象。请查收 *IRequest是要发送的对象。 *IResponse是要获取的对象 客户端代码: 服务器代码:
问题是自定义对象是序列化的,但它没有帮助。我无法序列化ArrayList,所以它必须是其他的…请尝试设置ArrayList实体[]。我猜您这边有问题,因为我每天都在使用它。也许首先您应该尝试传输一个简单的整数对象。这不可能有什么问题。
Socket clientSocket = new Socket(hostName, portNo);
clientSocket.setSoTimeout(connectionTimeout);
try {
OutputStream outputStream = clientSocket.getOutputStream();
InputStream inputStream = clientSocket.getInputStream();
ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStream);
ObjectInputStream objInputStream = null;
try {
objOutputStream.writeObject(request);
objOutputStream.flush();
objInputStream = new ObjectInputStream(inputStream);
res = (IResponse) objInputStream.readObject();
} finally {
if (objOutputStream != null) {
objOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (objInputStream != null) {
objInputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
}
} finally {
clientSocket.close();
clientSocket = null;
}
private IMessageProcessor messageProcessor = null;
private Socket clientSocket = null;
private Logger logger = null;
String clientIP = clientSocket.getInetAddress().getHostAddress();
int clientPortNr = clientSocket.getPort();
InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream();
try {
ObjectInputStream objInputStream = null;
ObjectOutputStream objOutputStream = null;
try {
IRequest request = null;
objInputStream = new ObjectInputStream(inputStream);
objOutputStream = new ObjectOutputStream(outputStream);
request = (IRequest) objInputStream.readObject();
IResponse response = null;
try {
response = messageProcessor.processMessage(clientIP,
clientPortNr,
request,
logger);
objOutputStream.writeObject(response);
} catch (Exception ex) {
objOutputStream.writeObject(ex);
}
objOutputStream.flush();
objOutputStream.reset();
} finally {
if (objInputStream != null)
objInputStream.close();
if (objOutputStream != null)
objOutputStream.close();
}
} finally {
inputStream.close();
outputStream.close();
clientSocket.close();
}
} catch (Exception ex) {
logger.error(ex.getMessage());
}