1

我正在尝试创建一个简单的 UDP 服务器来处理来自 Log4j2 Socket appender 的包,但是我从收到的第二个包中得到了“java.io.StreamCorruptedException:无效流标头:7372003E”。

附加器:

<Socket name="Socket" host="localhost" port="9500" protocol="UDP" >
    <SerializedLayout /> 
</Socket>

片段 UDP 服务器:

public void startSerialized() throws Exception {
    serverSocket = new DatagramSocket(9500);
    LogEvent logEvent = null;
    ObjectInputStream obj = null;
    ByteArrayInputStream bis = null;
    while (true) {
        try {
            receivePacket = new DatagramPacket(packet, packet.length);
            serverSocket.receive(receivePacket);
            bis = new ByteArrayInputStream(receivePacket.getData());
            obj = new ObjectInputStream(bis);
            logEvent = (LogEvent) obj.readObject();
            System.out.println(logEvent.toString());    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bis.close();
            obj.close();
        }           
    }
}

有什么建议吗?

4

1 回答 1

0

我使用了您帖子中的相同代码,但我成功地将调试日志发送到服务器端。下面是我的 log4j2.xml

    <Socket name="Socket" host="localHost" port="9092" protocol="UDP" >
        <SerializedLayout />
    </Socket>
</appenders>


<loggers>

    <logger name="rezg.gdsws.util" level="info">
        <appender-ref ref="Socket" />
    </logger>

    <logger name="org.apache" level="debug" />  <!-- Otherwise when we enable debug Apache debugs also printed -->

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</loggers>
于 2013-05-03T11:47:30.007 回答