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