我创建了一个serverSocket并接受客户端连接。但是,当我尝试从客户端读取时,它抛出以下异常。如果我将HashMap
更改为ArrayList
,则它也无效。
Exception in thread "Thread-3" java.lang.ClassCastException: java.awt.Point cannot be cast to java.util.HashMap
at ServerSide.Server.getPoints(Server.java:112)
at ServerSide.Server.run(Server.java:69)
public void getPoints() throws IOException, ClassNotFoundException {
points = (HashMap<Point, Boolean>) objectInputStream.readObject();
Iterator iterator = points.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Point, Boolean> currentPoint = (Map.Entry<Point, Boolean>) iterator.next();
currentPoint.setValue(firgure.isHit(currentPoint.getKey().x, currentPoint.getKey().y));
}
objectOutputStream.writeObject(points);
}
发送方式:
@Override
public HashMap<Point, Boolean> update(HashMap<Point, Boolean> points) throws IOException {
output.println("hit");
output.flush();
toServer.writeObject(points);
try {
return (HashMap<Point, Boolean>) fromServer.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Some shit with classCast!");
return null;
}
}
UDP新类型例外:
java.io.StreamCorruptedException: invalid type code: 3F
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1377)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at ServerSide.Server.getPoints(Server.java:102)
at ServerSide.Server.run(Server.java:69)
此视频展示了它(不)的工作方式: http://www.youtube.com/watch?v=8924rrSyWfY&feature=youtu.be
也许我的服务器或客户端不好?谁能看到一些错误? (我很抱歉我的英语)
服务器
public class Server extends Thread {
public static final int PORT = 1234;
private static ServerSocket serverSocket;
private Socket client;
private ObjectInputStream objectInputStream;
private ObjectOutputStream objectOutputStream;
private BufferedReader bufferedReader;
private PrintWriter printWriter;
private Firgure firgure;
private HashMap<Point, Boolean> points;
private Server(Socket client) {
System.out.println("Client connected");
this.client = client;
firgure = new Firgure();
try {
this.client.setSoTimeout(0);
} catch (SocketException e) {
e.printStackTrace();
}
setDaemon(true);
setPriority(NORM_PRIORITY);
start();
}
static public void serverStart() throws IOException {
System.out.println("Server startes");
serverSocket = new ServerSocket(PORT);
serverSocket.setSoTimeout(0);
System.out.println("Wait client");
while (true) new Server(serverSocket.accept());
}
static public void main(String[] args) throws IOException {
serverStart();
}
@Override
public void run() {
try {
objectInputStream = new ObjectInputStream(client.getInputStream());
objectOutputStream = new ObjectOutputStream(client.getOutputStream());
bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
printWriter = new PrintWriter(client.getOutputStream(), true);
points = new HashMap<Point, Boolean>();
String inputLine;
do {
inputLine = bufferedReader.readLine();
if (inputLine.equalsIgnoreCase("hit")) getPoints();
else if (inputLine.equalsIgnoreCase("echo")) echo();
else if (inputLine.equalsIgnoreCase("close")) close();
else if (inputLine.equalsIgnoreCase("set")) set();
else if (inputLine.equalsIgnoreCase("get")) get();
else continue;
} while (true);
} catch (NullPointerException e) {
System.out.println("Client disconnect");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public int getR() {
return firgure.getR();
}
public void setR(int R) {
firgure.setR(R);
}
public void set() throws IOException, ClassNotFoundException {
setR((Integer) objectInputStream.readObject());
}
public void get() throws IOException {
objectOutputStream.writeObject(new Integer(getR()));
}
public void getPoints() throws IOException, ClassNotFoundException {
try {
points = (HashMap<Point, Boolean>) objectInputStream.readObject();
} catch (StreamCorruptedException e) {
e.printStackTrace();
points = new HashMap<Point, Boolean>();
} catch (ClassCastException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Iterator iterator = points.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Point, Boolean> currentPoint = (Map.Entry<Point, Boolean>) iterator.next();
currentPoint.setValue(firgure.isHit(currentPoint.getKey().x, currentPoint.getKey().y));
}
objectOutputStream.writeObject(points);
}
public void echo() throws IOException {
printWriter.println(bufferedReader.readLine());
}
private void close() {
try {
if (objectInputStream != null) objectInputStream.close();
if (objectOutputStream != null) objectOutputStream.close();
if (bufferedReader != null) bufferedReader.close();
if (client != null) client.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
客户端
public class Client implements IModel {
private static final int PORT = 1234;
private static final String HOST = "localhost";
private Socket server;
private ObjectOutputStream toServer;
private ObjectInputStream fromServer;
private BufferedReader input;
private PrintWriter output;
@Override
public HashMap<Point, Boolean> update(HashMap<Point, Boolean> points) throws IOException {
output.println("hit");
output.flush();
toServer.writeObject(points);
try {
return (HashMap<Point, Boolean>) fromServer.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Some shit with classCast!");
return null;
}
}
@Override
public void connect() throws IOException {
server = new Socket(HOST, PORT);
toServer = new ObjectOutputStream(server.getOutputStream());
fromServer = new ObjectInputStream(server.getInputStream());
input = new BufferedReader(new InputStreamReader(server.getInputStream()));
output = new PrintWriter(server.getOutputStream(), true);
}
@Override
public void disconnect() throws IOException {
if (output != null) output.close();
if (input != null) input.close();
if (fromServer != null) fromServer.close();
if (toServer != null) toServer.close();
if (server != null) server.close();
}
@Override
public boolean isConnected() throws IOException {
if (server == null) return false;
return server.isConnected();
}
@Override
public int getFigureRadius() throws IOException {
output.println("get");
output.flush();
try {
return (Integer) fromServer.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Some shit with cast");
return 0;
}
}
@Override
public void setFigureRadius(int newRadius) throws IOException {
output.println("set");
output.flush();
toServer.writeObject(newRadius);
}
}
答案 0 :(得分:1)
从您的异常消息中可以清楚地看到这一点。您正在发送一个Point并尝试接收HashMap。
答案 1 :(得分:0)
从日志中获得投射错误:
points = (HashMap<Point, Boolean>) objectInputStream.readObject();
调试试试:
Object obj = objectInputStream.readObject();
if(obj instanceof Map<Point, Boolean>){
...
}
else if(obj instanceof String){
...
}