socket中为什么ObjectOutputStream写了两次 而ObjectInputStream读了两次都是第一次写的内容啊 求大神啊

LBH283681056 2013-02-20 01:02:11
//读的
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();
}

}

}
...全文
96 1 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Java_Super_Man 2013-02-20
为什么要使用ObjectIn、Out?
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
社区管理员
  • Java SE
申请成为版主
帖子事件
创建了帖子
2013-02-20 01:02
社区公告
暂无公告