Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import java.nio.ByteBuffer;

import java.util.Map;

public class Main {
    private static final int limit = 251;

    private static void receive(Map<Integer, byte[]> map, DatagramSocket socket, int[] length) {
        byte[] b = new byte[limit + 4];
        DatagramPacket p = new DatagramPacket(b, b.length);
        try {
            socket.receive(p);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (p.getLength() != 0) {
            ByteBuffer buf = ByteBuffer.wrap(b);
            int key = buf.getInt();
            byte[] bytes = new byte[p.getLength() - 4];
            length[0] += bytes.length;
            buf.get(bytes);
            map.put(key, bytes);
            receive(map, socket, length);
        }
    }

    public static byte[] getInt(int a) {
        return ByteBuffer.allocate(4).putInt(a).array();
    }
}