码迷,mamicode.com
首页 > 其他好文 > 详细

Socket和ObjectOutputStream问题

时间:2018-08-16 20:01:11      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:art   oal   原因   str   host   package   figure   constant   new   

用到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异常:\t" + 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("用户写线程构造器异常:\t"+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("写:\t\t"+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("用户端写入线程出现异常:\t"+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("服务器端服务开启失败:\t"+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("服务器写线程构造器异常:\t"+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("服务器端写入线程出现异常:\t"+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("服务器读线程构造器异常:\t"+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("服务器端读取线程异常:\t" + e.getMessage());
		}

	}
}

  再新起一个项目也使用ObjectInputStream序列化对象传输时正常未找到原因,改用传输json使用Gson

Socket和ObjectOutputStream问题

标签:art   oal   原因   str   host   package   figure   constant   new   

原文地址:https://www.cnblogs.com/zhanchaohan/p/9488654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!