在Java中使用TCP套接字连接的Jackson流API

时间:2014-08-30 08:39:30

标签: java json sockets jackson

问题在于通过套接字连接发送和获取JSON对象......它无法在服务器端正确解析。这是我的第一个java项目

这是我的套接字类:

    static class CheckerSocket implements Runnable{
    static Socket socket;
    static InputStream sin ;
    static OutputStream sout;

    static boolean makeServer(){
        int port = 6969;
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            socket = serverSocket.accept();
            System.out.println("Someone connected!");
            sin = socket.getInputStream();
            sout = socket.getOutputStream();
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;}
    }

    static boolean makeClient(String address, Integer port){
        try{
            InetAddress ipAddress = InetAddress.getByName(address);
            socket = new Socket(ipAddress,port);
            System.out.println("Connected to server!");
            sin =socket.getInputStream();
            sout =socket.getOutputStream();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;}
    }

    @Override
    public void run() {
        if(Board.playerServer){
        makeServer(); Main.setScene();
        }
        else makeClient(tf_ip.getText(),Integer.parseInt(tf_port.getText())); ;
    }
}
}

我正在使用jackson.core库提供的JsonGeneratore和JsonParser 发电机:

public static void sendJSON(MoveToSend moveToSend) throws IOException {
    JsonGenerator jsonGenerator = new    JsonFactory().createJsonGenerator(Controller.CheckerSocket.sout);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeBooleanField("isAttack", moveToSend.isAttack());
    jsonGenerator.writeStringField("From", moveToSend.getFrom());
    jsonGenerator.writeStringField("To", moveToSend.getTo());
    if (moveToSend.isAttack()) {
        jsonGenerator.writeArrayFieldStart("Killed");
        for (String s : moveToSend.getKilledCheckers()) jsonGenerator.writeString(s);
        jsonGenerator.writeEndArray();
    }
    jsonGenerator.writeEndObject();
    jsonGenerator.flush();
}

分析器:

public static MoveToSend receiveJSON() {
    //TODO PARSING JSON
    MoveToSend receivedMove = new MoveToSend();
    try {
    JsonParser jsonParser = new   JsonFactory().createJsonParser(Controller.CheckerSocket.sin);
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String name = jsonParser.getCurrentName();
            if ("isAttack".equals(name)) {
                jsonParser.nextToken();
                receivedMove.setIsAttack(jsonParser.getBooleanValue());
            } else if ("From".equals(name)) {
                jsonParser.nextToken();
                receivedMove.setFrom(jsonParser.getText());
            } else if ("To".equals(name)) {
                jsonParser.nextToken();
                receivedMove.setTo(jsonParser.getText());
            } else if ("Killed".equals(name)) {
                if (receivedMove.isAttack()) {
                    jsonParser.nextToken();
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        receivedMove.setKilledCheckers(jsonParser.getText());
                    }
                }
            }
        }

    }catch (JsonGenerationException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    return receivedMove;
    }

测试服务器类:

public class TestServer {
public static void main(String[] args) throws IOException {
    Controller.CheckerSocket.makeServer();
    MoveToSend receivedMove;
    receivedMove = MoveSender.receiveJSON();
}

}

测试客户端类:

public class TestClient {
public static void main(String[] args) throws IOException {
    Controller.CheckerSocket.makeClient("127.0.0.1",6969);
    ArrayList<String> killed = new ArrayList<String>();
    killed.add("32");
    killed.add("54");
    killed.add("76");
    MoveToSend m1 = new MoveToSend("21","87",killed);
    MoveSender.sendJSON(m1);
    while(true){
        if(m1==null) return;
    }
}
}

1 个答案:

答案 0 :(得分:1)

问题解决了:更改了流类型。

sendJSON(){
DataOutputStream out = new DataOutputStream(Controller.CheckerSocket.sout);
JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(out);
-//-
}
receiveJSON(){
DataInputStream in = new DataInputStream(Controller.CheckerSocket.sin);
JsonParser jsonParser = new JsonFactory().createJsonParser(in);
-//-
}