gpt4 book ai didi

java - Java 中的数据包发送

转载 作者:行者123 更新时间:2023-11-30 04:52:32 25 4
gpt4 key购买 nike

好吧,我正在尝试将数据包发送到本地游戏服务器,但数据包和连接让我有点困惑。这是我的代码:

public class Packet{
DataOutputStream os;
DataInputStream is;

public void test() throws IOException
{
try {
Socket socket = new Socket("127.0.0.1", 25565);
} catch (UnknownHostException e) {
} catch (IOException e) {
}
os.writeByte(1);
os.writeInt(23);
writeString2("TEST", os);
os.writeLong(0);
os.writeShort(0);
os.writeInt(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
Menu.log1.addElement("Testing");
}

但是当我启动它时,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Packet.test(Packet.java:29)
at Menu$2.actionPerformed(Menu.java:309)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6373)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6138)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4735)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)`

有人可以帮我解决他的问题吗,我已经困扰了我一段时间了。

最佳答案

在使用它们之前,您需要实际使 osis 引用指向某些对象:

Socket socket = new Socket("127.0.0.1", 25565);
is = new DataInputStream(socket.getInputStream());
os = new DataOutputStream(socket.getOutputStream());

关于java - Java 中的数据包发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9520556/

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