DatagramSocket socket = new DatagramSocket();
InetAddress address = InetAddress.getByName(DATA_DIC.UDP_IP);
// 데이터가 저장될 공간으로 byte배열을 생성한다.
byte[] msg = new byte[1024];
byte[] msg2 = new byte[1024];
if (sendMSG != null) {
msg = sendMSG.getBytes();
} else {
msg = null;
}
while (true) {
if (msg != null) {
DatagramPacket packet = new DatagramPacket(msg, msg.length, address, DATA_DIC.UDP_PORT);
Log.d(tag, "서버로 전송중...");
packet.setLength(msg.length);
socket.send(packet); // 전송
sendMSG = null;
msg = null;
}
DatagramPacket packet1 = new DatagramPacket(msg2, msg2.length);
packet1.setLength(msg2.length);
socket.receive(packet1); // 수신
DATA_DIC.receiveMSG = new String(packet1.getData());
System.out.println("current server time : '" + new String(packet1.getData()) + "'");
msg2 = new byte[1024];
이런식으로 되어있는데요 줄때는 이상이 ㅇ없는데 받을때는 길게 받으면 마지막글자가 깨지고 덜받더라구요
ㅠㅠ new byte[1024] 이렇게 말고 주는만큼 크기 잡아주고 받는만큼 크기 잡아주는 방법이있을까요?