socket中为什么ObjectOutputStream写了两次 而ObjectInputStream读了两次都是第一次写的内容啊 求大神啊
//读的
public class GameServerSocketInputThread extends Thread {
//关闭线程标记
public boolean flage = true;
private Socket socket;
private ObjectInputStream is;
//获取数据存放
private Map<String,Object> receiveMap = new HashMap<String, Object>();
//读取后的服务
private ServerInputservice inputservice = new ServerInputservice();
public GameServerSocketInputThread(Socket socket) {
super();
this.socket = socket;
try {
is = new ObjectInputStream(this.socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void run() {
while (flage) {
try {
receiveMap = (Map<String,Object>) is.readObject();
inputservice.ClientInput(receiveMap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//写的
package com.Server;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class GameServerSocketOutputThread extends Thread {
private Socket socket;
private ObjectOutputStream os;
public boolean flage = true;
public GameServerSocketOutputThread(Socket socket) {
super();
this.socket = socket;
try {
os = new ObjectOutputStream(this.socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void waitLock() {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void notifyLock() {
synchronized (this) {
this.notify();
}
}
public void run() {
while (flage) {
waitLock();
if (ServerOutputService.sendMap != null) {
try {
os.writeObject(ServerOutputService.sendMap);
os.flush();
ServerOutputService.sendMap.clear();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class GameServerSocketInputThread extends Thread {
//关闭线程标记
public boolean flage = true;
private Socket socket;
private ObjectInputStream is;
//获取数据存放
private Map<String,Object> receiveMap = new HashMap<String, Object>();
//读取后的服务
private ServerInputservice inputservice = new ServerInputservice();
public GameServerSocketInputThread(Socket socket) {
super();
this.socket = socket;
try {
is = new ObjectInputStream(this.socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void run() {
while (flage) {
try {
receiveMap = (Map<String,Object>) is.readObject();
inputservice.ClientInput(receiveMap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//写的
package com.Server;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class GameServerSocketOutputThread extends Thread {
private Socket socket;
private ObjectOutputStream os;
public boolean flage = true;
public GameServerSocketOutputThread(Socket socket) {
super();
this.socket = socket;
try {
os = new ObjectOutputStream(this.socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void waitLock() {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void notifyLock() {
synchronized (this) {
this.notify();
}
}
public void run() {
while (flage) {
waitLock();
if (ServerOutputService.sendMap != null) {
try {
os.writeObject(ServerOutputService.sendMap);
os.flush();
ServerOutputService.sendMap.clear();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
...全文
相关推荐
socket objectoutputstream 传对象 例子
socket objectoutputstream 传对象 例子 网上找的很好用,在网上留一份
ObjectInputStream 和 ObjectOutputStream 介绍_动力节点Java学院整理
ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持。本文给大家详细介绍了ObjectInputStream 和 ObjectOutputStream的相关知识,感兴趣的朋友一起学习吧
java socket工具类示例
java socket工具类示例,使用控制台实现简单收发,简单聊天系统.
Java Socket实现的传输对象功能示例
主要介绍了Java Socket实现的传输对象功能,结合具体实例形式分析了java socket传输对象的原理及接口、客户端、服务器端相关实现技巧,需要的朋友可以参考下
Javas_socket传递JavaBean(案例)
NULL 博文链接:https://yaodaqing.iteye.com/blog/838382


Java SE

