Socket和ObjectOutputStream问题
Posted zhanchaohan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket和ObjectOutputStream问题相关的知识,希望对你有一定的参考价值。
用到Socket序列化对象网络传输时ObjectOutputStream一直刷新连接
用户代码
package com.jachs.ladflower.ladflower; import java.net.Socket; import org.apache.log4j.Logger; import com.jachs.ladflower.ladflower.thread.UserReaderThread; import com.jachs.ladflower.ladflower.thread.UserWriterThread; public class UserSocket { private static Logger logger = Logger.getLogger(UserSocket.class); static{ try { Socket socket = new Socket(Constant.serverConfigure.getServerIp(), Constant.serverConfigure.getServerPort()); new Thread(new UserReaderThread(socket.getInputStream())).start(); new Thread(new UserWriterThread(socket.getOutputStream())).start(); // MainFrame MainFrame=new MainFrame(socket.getInetAddress().getHostName()); } catch (Exception e) { logger.error("客户端Socket异常: " + e.getMessage()); e.printStackTrace(); } } }
用户写入线程代码
package com.jachs.ladflower.ladflower.thread; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.Scanner; import org.apache.log4j.Logger; import com.google.gson.Gson; import com.jachs.ladflower.ladflower.Constant; import com.jachs.ladflower.ladflower.entity.SendInfo; /**** * 用户Socket写 * @author zhanchaohan * */ public class UserWriterThread implements Runnable{ private Logger logger= Logger.getLogger(UserWriterThread.class); // private ObjectOutputStream objectOutputStream; private DataOutputStream dataOutputStream; private Gson gson=new Gson(); public UserWriterThread(OutputStream outputStream) { super(); try { // this.objectOutputStream =new ObjectOutputStream(outputStream); this.dataOutputStream=new DataOutputStream(outputStream); } catch (Exception e) { logger.error("用户写线程构造器异常: "+e.getMessage()); } } public void run() { try { Scanner scanner=new Scanner(System.in); String info; SendInfo sendInfo; // while((info=scanner.nextLine())!=null) { // sendInfo=new SendInfo(Constant.STATECODE_2,info); // objectOutputStream.writeObject(sendInfo); // } while((info=scanner.nextLine())!=null) { System.out.println("写: "+info); sendInfo=new SendInfo(Constant.STATECODE_2,info); System.out.println("a"); dataOutputStream.writeUTF(gson.toJson(sendInfo)); System.out.println("b"); } } catch (Exception e) { logger.error("用户端写入线程出现异常: "+e.getMessage()); } } }
服务器代码
package com.jachs.ladflower.ladflower; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import org.apache.log4j.Logger; import com.jachs.ladflower.ladflower.entity.SendInfo; import com.jachs.ladflower.ladflower.quartz.QuartzInit; import com.jachs.ladflower.ladflower.thread.ServerReaderThread; import com.jachs.ladflower.ladflower.thread.ServerWriterThread; import com.jachs.ladflower.ladflower.utill.SocketUtill; /**** * 服务器Socket * @author zhanchaohan * */ public class ServerSockets { private static Logger logger= Logger.getLogger(ServerSockets.class); static { try { //初始化定时器 // QuartzInit quartzInit=new QuartzInit(); //启动在线用户刷新定时器 // quartzInit.MonitoringOnLine_Heartbeat(); ServerSocket serverSocket=new ServerSocket(Constant.SERVERPORT); Socket socket; while((socket=serverSocket.accept())!=null) { // Constant.list.add(socket); new Thread(new ServerReaderThread(socket.getInputStream())).start(); new Thread(new ServerWriterThread(socket.getOutputStream())).start(); // SocketUtill.sendMsgToAllSocket(new SendInfo(Constant.STATECODE_0, socket.getInetAddress().getHostName())); System.out.println("getIn"); } } catch (IOException e) { logger.error("服务器端服务开启失败: "+e.getMessage()); e.printStackTrace(); } } }
服务器
写入代码
package com.jachs.ladflower.ladflower.thread; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.Scanner; import org.apache.log4j.Logger; import com.google.gson.Gson; import com.jachs.ladflower.ladflower.Constant; import com.jachs.ladflower.ladflower.entity.SendInfo; /**** * 服务器Socket写 * @author zhanchaohan * */ public class ServerWriterThread implements Runnable{ private Logger logger= Logger.getLogger(ServerWriterThread.class); // private ObjectOutputStream objectOutputStream; private DataOutputStream dataOutputStream; private Gson gson=new Gson(); public ServerWriterThread(OutputStream outputStream) { super(); try { this.dataOutputStream=new DataOutputStream(outputStream); // this.objectOutputStream =new ObjectOutputStream(outputStream); } catch (Exception e) { logger.error("服务器写线程构造器异常: "+e.getMessage()); } } public void run() { try { Scanner scanner=new Scanner(System.in); String info; SendInfo sendInfo; // while((info=scanner.nextLine())!=null) { // sendInfo=new SendInfo(Constant.STATECODE_2,info); // objectOutputStream.writeObject(sendInfo); // } while((info=scanner.nextLine())!=null) { sendInfo=new SendInfo(Constant.STATECODE_2,info); dataOutputStream.writeUTF(gson.toJson(sendInfo)); } } catch (Exception e) { logger.error("服务器端写入线程出现异常: "+e.getMessage()); } } }
服务器读取代码
package com.jachs.ladflower.ladflower.thread; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import org.apache.log4j.Logger; import com.jachs.ladflower.ladflower.entity.SendInfo; import com.jachs.ladflower.ladflower.utill.SocketUtill; /**** * 服务器Socket读 * * @author zhanchaohan * */ public class ServerReaderThread implements Runnable { private Logger logger = Logger.getLogger(ServerReaderThread.class); // private ObjectInputStream inputStream; private DataInputStream dataInputStream; public ServerReaderThread(InputStream inputStream) { super(); this.dataInputStream=new DataInputStream(inputStream); /*try { this.inputStream = new ObjectInputStream(inputStream); } catch (IOException e) { logger.error("服务器读线程构造器异常: "+e.getMessage()); }*/ } public void run() { try { /*SendInfo send; while ((send = (SendInfo) inputStream.readObject()) != null) { SocketUtill.SwithInfo(send); }*/ while(true) { System.out.println(dataInputStream.readUTF()); } } catch (Exception e) { logger.error("服务器端读取线程异常: " + e.getMessage()); } } }
再新起一个项目也使用ObjectInputStream序列化对象传输时正常未找到原因,改用传输json使用Gson
以上是关于Socket和ObjectOutputStream问题的主要内容,如果未能解决你的问题,请参考以下文章
Android Socket + ObjectOutputStream 无法正常工作