ArrayIndexOutOfBoundsException in DatagramPacket-based application
-
03-12-2019 - |
Вопрос
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));
}
}
Решение
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++)
Другие советы
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.
wyslijDane
sets wejscie
to "ready!".getBytes()
, but czyscBuforWejscia
expects its size to be 1024
Не связан с StackOverflow