基于 DatagramPacket 的应用程序中的 ArrayIndexOutOfBoundsException

发布于 2024-12-29 12:41:55 字数 3896 浏览 2 评论 0原文

首先,我需要为我在程序中使用的变量/函数的波兰名称道歉。

所以,字典很少:

Klient - Client
Serwer - Server
wejscie - input
wyjscie - output
klienci - clients
teraz - now
teraz - text
nawiazPolaczenie - establishConnection
czyscBufor - clearBuffer
odbierzDane - receiveData
pakiet - packet
wyslijDane - sendData

问题出在客户端从服务器接收数据时 - 出现 ArrayIndexOutOfBoundsException。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at kontrolerklient.Klient.czyscBuforWejscia(Klient.java:43)
    at kontrolerklient.Klient.odbierzDane(Klient.java:48)
    at kontrolerklient.Klient.nawiazPolaczenie(Klient.java:33)
    at kontrolerklient.Klient.<init>(Klient.java:25)
    at kontrolerklient.KontrolerKlient.main    (KontrolerKlient.java:11)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

整个服务器的代码:

package kontrolerserwer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;

public class Serwer
{
    private DatagramSocket dgSocket;
    private byte[] bufferIn;
    private byte[] bufferOut;
    private InetAddress[] klienci;

    public Serwer() throws IOException
    {
        dgSocket = new DatagramSocket(9998, InetAddress.getByName("192.168.1.100"));

        bufferIn = new byte[1024];
        bufferOut = new byte[1024];
        klienci = new InetAddress[256];

        dgSocket.setSoTimeout(1000);
        wyslijDane("ready?", InetAddress.getByName("192.168.1.100"));

        Date teraz = new Date();
        teraz.setTime(teraz.getTime()+10000);

        while (teraz.after(new Date()))
        {

        }
    }

    public void wyslijDane(String tekst, InetAddress ip) throws IOException
    {
        bufferOut = tekst.getBytes("ASCII");
        dgSocket.send(new DatagramPacket(bufferOut, bufferOut.length, ip, 9999));
    }
}

..和客户端的代码:

package kontrolerklient;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

class Klient
{
    private DatagramSocket dgSocket;
    private InetAddress host;
    private byte[] wejscie;
    private byte[] wyjscie;

    public Klient() throws UnknownHostException, SocketException, IOException
    {
        wejscie = new byte[1024];
        wyjscie = new byte[1024];
        host = null;

        dgSocket = new DatagramSocket(9999, InetAddress.getByName("192.168.1.100"));

        nawiazPolaczenie();

    }

    private void nawiazPolaczenie() throws IOException
    {
        while (true)
        {
            if (odbierzDane().equals("ready?"))
            {
                wyslijDane("ready!", 9998);
            }
        }
    }

    private void czyscBuforWejscia()
    {
        for (int i = 0; i < 1024; i++)
            wejscie[i] = 0;   
    }

    public String odbierzDane() throws IOException
    {
        czyscBuforWejscia();

        DatagramPacket pakiet = new DatagramPacket(wejscie, wejscie.length);
        System.out.println(pakiet.getLength());
        try
        {
            dgSocket.receive(pakiet);
            host = pakiet.getAddress();

            // getting packet's data
            String s = new String(pakiet.getData(), 0, wejscie.length);

            // getting packet's data length
            int i;
            for (i = 0; (i < 1024) && (wejscie[i] != 0); i++);

            // returning packet's data
            return s.substring(0, i); 
        }
        catch (Exception e) { }

        return "";
    }

    public void wyslijDane(String dane, int port) throws IOException
    {
        wejscie = dane.getBytes("ASCII");
        dgSocket.send(new DatagramPacket(wyjscie, wyjscie.length, host, port));
    }
}

At the beginning, I need to apologise for polish names of variables/functions that I've used in my program.

So, there's little dictionary:

Klient - Client
Serwer - Server
wejscie - input
wyjscie - output
klienci - clients
teraz - now
teraz - text
nawiazPolaczenie - establishConnection
czyscBufor - clearBuffer
odbierzDane - receiveData
pakiet - packet
wyslijDane - sendData

The problem is in moment, when the client is receiving data from server - there's ArrayIndexOutOfBoundsException.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at kontrolerklient.Klient.czyscBuforWejscia(Klient.java:43)
    at kontrolerklient.Klient.odbierzDane(Klient.java:48)
    at kontrolerklient.Klient.nawiazPolaczenie(Klient.java:33)
    at kontrolerklient.Klient.<init>(Klient.java:25)
    at kontrolerklient.KontrolerKlient.main    (KontrolerKlient.java:11)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

Whole server's code:

package kontrolerserwer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;

public class Serwer
{
    private DatagramSocket dgSocket;
    private byte[] bufferIn;
    private byte[] bufferOut;
    private InetAddress[] klienci;

    public Serwer() throws IOException
    {
        dgSocket = new DatagramSocket(9998, InetAddress.getByName("192.168.1.100"));

        bufferIn = new byte[1024];
        bufferOut = new byte[1024];
        klienci = new InetAddress[256];

        dgSocket.setSoTimeout(1000);
        wyslijDane("ready?", InetAddress.getByName("192.168.1.100"));

        Date teraz = new Date();
        teraz.setTime(teraz.getTime()+10000);

        while (teraz.after(new Date()))
        {

        }
    }

    public void wyslijDane(String tekst, InetAddress ip) throws IOException
    {
        bufferOut = tekst.getBytes("ASCII");
        dgSocket.send(new DatagramPacket(bufferOut, bufferOut.length, ip, 9999));
    }
}

..and client's code:

package kontrolerklient;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

class Klient
{
    private DatagramSocket dgSocket;
    private InetAddress host;
    private byte[] wejscie;
    private byte[] wyjscie;

    public Klient() throws UnknownHostException, SocketException, IOException
    {
        wejscie = new byte[1024];
        wyjscie = new byte[1024];
        host = null;

        dgSocket = new DatagramSocket(9999, InetAddress.getByName("192.168.1.100"));

        nawiazPolaczenie();

    }

    private void nawiazPolaczenie() throws IOException
    {
        while (true)
        {
            if (odbierzDane().equals("ready?"))
            {
                wyslijDane("ready!", 9998);
            }
        }
    }

    private void czyscBuforWejscia()
    {
        for (int i = 0; i < 1024; i++)
            wejscie[i] = 0;   
    }

    public String odbierzDane() throws IOException
    {
        czyscBuforWejscia();

        DatagramPacket pakiet = new DatagramPacket(wejscie, wejscie.length);
        System.out.println(pakiet.getLength());
        try
        {
            dgSocket.receive(pakiet);
            host = pakiet.getAddress();

            // getting packet's data
            String s = new String(pakiet.getData(), 0, wejscie.length);

            // getting packet's data length
            int i;
            for (i = 0; (i < 1024) && (wejscie[i] != 0); i++);

            // returning packet's data
            return s.substring(0, i); 
        }
        catch (Exception e) { }

        return "";
    }

    public void wyslijDane(String dane, int port) throws IOException
    {
        wejscie = dane.getBytes("ASCII");
        dgSocket.send(new DatagramPacket(wyjscie, wyjscie.length, host, port));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聊慰 2025-01-05 12:41:55

我想在这里:

wejscie = dane.getBytes("ASCII")

您正在覆盖原始声明:

wejscie = new byte[1024];

使用一些未知大小的字节数组。但随后您清除了缓冲区:

for (int i = 0; i < 1024; i++)

具有固定大小。本来应该是:

for (int i = 0; i < wejscie.length; i++)

I guess that here:

wejscie = dane.getBytes("ASCII")

you are overwriting the original declaration:

wejscie = new byte[1024];

with some byte array of unknown size. But then you clear the buffer:

for (int i = 0; i < 1024; i++)

with fixed size. It should have been:

for (int i = 0; i < wejscie.length; i++)
凤舞天涯 2025-01-05 12:41:55

读取数据包时,您需要使用接收到的数据的长度(DatagramPacket.getLength()),而不是您创建的字节数组的长度。

when reading the packet, you need to use the length of the data received (DatagramPacket.getLength()), not the length of the byte array you created.

枯寂 2025-01-05 12:41:55

wyslijDanewejscie 设置为 "ready!".getBytes(),但 czyscBuforWejscia 预计其大小为 1024

wyslijDane sets wejscie to "ready!".getBytes(), but czyscBuforWejscia expects its size to be 1024

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文