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();
}

}

}
...全文
151 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Java_Super_Man 2013-02-20
  • 打赏
  • 举报
回复
为什么要使用ObjectIn、Out?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧