各位老兄好,通过ObjectoutputStream ,ObjectINputStream 如何读写数据 CLSendStruct 是否实现串型化接口:java.io.Serializable还有你的输入,输出流是怎么重定向的? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的输入流为一个结构体,如何将CLSendStruct 实现串型化接口 只需要 public class CLSendStruct implements java.io.Serializable { //你原有的定义 } 对没错,我就是如此的,有类没发现错误java.lang.ClassNotFoundException: airtserver.CLSendStruct receiveSTRK 为CLSendStruct的对象receiveSTRK = (CLSendStruct)clientReceive.readObject()这一个有错误 public class AirTest{ public static void main(String[] args)throws IOException{ CLSendStruct sendSTRK = new CLSendStruct(); Socket airSocket; //BufferedReader socketInput; //DataOutputStream socketOutput; ObjectInputStream InputStream = null; ObjectOutputStream OutputStream = null; ObjectOutputStream OOS = null;// = new ObjectOutputStream(; //sendSTRK.setCommand(args[0].getBytes()); try{ System.out.println("Connect..."); airSocket = new Socket("127.0.0.1",1024); OutputStream = new ObjectOutputStream(airSocket.getOutputStream ()); InputStream = new ObjectInputStream(airSocket.getInputStream ()); //DisplayReply(ObjectInputStream); System.out.println("Command + TEST"); sendSTRK.setCommand("TEST".toCharArray()); sendSTRK.setResult("qqqqqqqqqq".toCharArray()); sendSTRK.equals(OOS); //OutputStream.write(sendSTRK.getPool().toString().getBytes()); OutputStream.writeObject(sendSTRK); OutputStream.flush (); DisplayReply(InputStream);/* System.out.println("Command1 + QUIT"); sendSTRK.setCommand("QUIT".toCharArray()); sendSTRK.setResult("PPPPPPPPPPP".toCharArray()); sendSTRK.equals(OOS); OutputStream.writeObject (sendSTRK); OutputStream.flush (); DisplayReply(InputStream); //this.m_msg = (Message) p_ObjectInputStream.readObject();*/ OutputStream.close(); InputStream.close(); airSocket.close(); }catch(Exception e){ System.out.println("exception: "+e); } System.exit(0); } private static void DisplayReply(ObjectInputStream inputStream) throws IOException,Exception{ ObjectInputStream OIS = null; String s = new String(); CLSendStruct receiveSTRK = new CLSendStruct(); try{ //inputStream.equals(receiveSTRK); System.out.println("qq "+receiveSTRK.toString()); //receiveSTRK.equals(inputStream); receiveSTRK= (CLSendStruct)inputStream.readObject(); System.out.println("qq "+receiveSTRK.toString()); s = receiveSTRK.getSuccID().toString(); if (s == "1"){ s = receiveSTRK.getResult().toString(); System.out.println("SUCCESS "+receiveSTRK.getGWTime().toString()); System.out.println(s); }else System.out.println("NO SUCCESS "+receiveSTRK.getGWTime().toString()); }catch(Exception e){ System.out.println("111111 "+e); } }}public class CLSendStruct implements Serializable{ private char[] Command = new char[100]; //发送查询命令 private char[] Result = new char[4000]; public CLSendStruct(){ } 下面为getXX(),setxx()}服务器 public AirHandler(Socket newSocket){ mySocket = newSocket; } public void run(){ //java.sql.Connection con; //java.sql.Statement stmt = null; String nextLine; StringTokenizer tokens; String command; stockInfoTime = new Date(); try{ clientReceive = new ObjectInputStream(mySocket.getInputStream ()); clientSend = new ObjectOutputStream(mySocket.getOutputStream ()); /* sendSTRK.setSuccID("1".toCharArray()); sendSTRK.setResult("HELLO WORLD !!!".toCharArray()); sendSTRK.equals(OOS); clientSend.writeObject(OOS);*/ try{ //clientReceive.readObject().equals(receiveSTRK); System.out.println("command: 1"); while((receiveSTRK = (CLSendStruct)clientReceive.readObject())!= null){ //break the line into tokens //tokens = new StringTokenizer(nextLine); System.out.println("command: 2"); try{ command = receiveSTRK.getCommand().toString(); System.out.println("command: "+command); //quit command if(command.equalsIgnoreCase("QUIT")){ sendSTRK.setxx(); clientSend.writeObject(sendSTRK);; } //stock command }catch(NoSuchElementException excpt){ sendSTRK.setSuccID("0".toCharArray()); sendSTRK.setResul("NoSuchElementException".toCharArray()); sendSTRK.equals(OOS); clientSend.writeObject(sendSTRK); } }谢谢帮忙 receiveSTRK = (CLSendStruct)clientReceive.readObject()这一句 上为服务器,下为客户端receiveSTRK= (CLSendStruct)inputStream.readObject(); OutputStream.writeObject (sendSTRK);这是写的 你读写一个简单的数据可以吗?OutputStream.writeInt(100); //ClientinputStream.readInt() ; //Server你试试可以读到这个100吗? sendSTRK.setCommand("QUIT".toCharArray());你为什么这么用:"QUIT".toCharArray();你定义的结构里面有字符数组?如果有数组,你的数组是否实现序列化接口呢? public class CLSendStruct implements Serializable{ private char[] Command = new char[100]; //发送查询命令 private char[] Result = new char[4000]; public CLSendStruct(){ } 下面为getXX(),setxx()}难道这样序列化不行吗?如何对字符数组进行序列化 泛型问题求助高手 使用NetBeans开发桌面应用程序时怎么控制用户权限 JDK帮忙文档中:Direct Known Subclasses: 怎么翻译? 如何改变用setOneTouchExpandable(true);设置后的图标的位置 在用西门子TC35I发送中文短信时,能在超级终端中发送成功,在程序中确发不成功,为什么? Class类的使用问题? 怎么不能改变文本框的大小了?? 又是多线程!唉!高手请指教! java的代码量少,依赖包少,程序运行速度上会有多少提高? Java初学者提出几个关于JAVA的问题. 门外汉,提问-有关JAVA入门的问题 请问这段程序为什么错了?
public class CLSendStruct implements java.io.Serializable
{
//你原有的定义
}
java.lang.ClassNotFoundException: airtserver.CLSendStruct
receiveSTRK = (CLSendStruct)clientReceive.readObject()
这一个有错误
public static void main(String[] args)throws IOException{
CLSendStruct sendSTRK = new CLSendStruct(); Socket airSocket;
//BufferedReader socketInput;
//DataOutputStream socketOutput;
ObjectInputStream InputStream = null;
ObjectOutputStream OutputStream = null;
ObjectOutputStream OOS = null;// = new ObjectOutputStream(;
//sendSTRK.setCommand(args[0].getBytes());
try{
System.out.println("Connect...");
airSocket = new Socket("127.0.0.1",1024);
OutputStream = new ObjectOutputStream(airSocket.getOutputStream ());
InputStream = new ObjectInputStream(airSocket.getInputStream ()); //DisplayReply(ObjectInputStream); System.out.println("Command + TEST");
sendSTRK.setCommand("TEST".toCharArray());
sendSTRK.setResult("qqqqqqqqqq".toCharArray());
sendSTRK.equals(OOS); //OutputStream.write(sendSTRK.getPool().toString().getBytes());
OutputStream.writeObject(sendSTRK);
OutputStream.flush ();
DisplayReply(InputStream);
/*
System.out.println("Command1 + QUIT");
sendSTRK.setCommand("QUIT".toCharArray());
sendSTRK.setResult("PPPPPPPPPPP".toCharArray());
sendSTRK.equals(OOS); OutputStream.writeObject (sendSTRK);
OutputStream.flush ();
DisplayReply(InputStream); //this.m_msg = (Message) p_ObjectInputStream.readObject();*/ OutputStream.close();
InputStream.close();
airSocket.close();
}catch(Exception e){
System.out.println("exception: "+e);
}
System.exit(0);
} private static void DisplayReply(ObjectInputStream inputStream) throws IOException,Exception{
ObjectInputStream OIS = null;
String s = new String();
CLSendStruct receiveSTRK = new CLSendStruct();
try{
//inputStream.equals(receiveSTRK);
System.out.println("qq "+receiveSTRK.toString());
//receiveSTRK.equals(inputStream);
receiveSTRK= (CLSendStruct)inputStream.readObject();
System.out.println("qq "+receiveSTRK.toString());
s = receiveSTRK.getSuccID().toString();
if (s == "1"){
s = receiveSTRK.getResult().toString();
System.out.println("SUCCESS "+receiveSTRK.getGWTime().toString());
System.out.println(s);
}else
System.out.println("NO SUCCESS "+receiveSTRK.getGWTime().toString());
}catch(Exception e){
System.out.println("111111 "+e);
}
}
}
public class CLSendStruct implements Serializable{ private char[] Command = new char[100]; //发送查询命令
private char[] Result = new char[4000];
public CLSendStruct(){
}
下面为getXX(),setxx()
}
服务器 public AirHandler(Socket newSocket){ mySocket = newSocket;
} public void run(){
//java.sql.Connection con;
//java.sql.Statement stmt = null;
String nextLine;
StringTokenizer tokens;
String command;
stockInfoTime = new Date();
try{
clientReceive = new ObjectInputStream(mySocket.getInputStream ());
clientSend = new ObjectOutputStream(mySocket.getOutputStream ());
/* sendSTRK.setSuccID("1".toCharArray());
sendSTRK.setResult("HELLO WORLD !!!".toCharArray());
sendSTRK.equals(OOS);
clientSend.writeObject(OOS);*/
try{
//clientReceive.readObject().equals(receiveSTRK);
System.out.println("command: 1");
while((receiveSTRK = (CLSendStruct)clientReceive.readObject())!= null){
//break the line into tokens
//tokens = new StringTokenizer(nextLine);
System.out.println("command: 2");
try{
command = receiveSTRK.getCommand().toString();
System.out.println("command: "+command);
//quit command
if(command.equalsIgnoreCase("QUIT")){
sendSTRK.setxx();
clientSend.writeObject(sendSTRK);;
}
//stock command
}catch(NoSuchElementException excpt){
sendSTRK.setSuccID("0".toCharArray());
sendSTRK.setResul("NoSuchElementException".toCharArray());
sendSTRK.equals(OOS);
clientSend.writeObject(sendSTRK);
}
}
谢谢帮忙
这一句
receiveSTRK= (CLSendStruct)inputStream.readObject();
这是写的
OutputStream.writeInt(100); //ClientinputStream.readInt() ; //Server你试试可以读到这个100吗?
你定义的结构里面有字符数组?
如果有数组,你的数组是否实现序列化接口呢?
private char[] Result = new char[4000];
public CLSendStruct(){
}
下面为getXX(),setxx()
}难道这样序列化不行吗?
如何对字符数组进行序列化