gpt4 book ai didi

java - Java 中具有 TCP 套接字连接的 Jackson 流 API

转载 作者:太空宇宙 更新时间:2023-11-04 06:34:41 25 4
gpt4 key购买 nike

问题在于通过套接字连接发送和获取 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;
}
}
}

最佳答案

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

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

关于java - Java 中具有 TCP 套接字连接的 Jackson 流 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com