Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.List;
- public class Server implements Runnable {
- ArrayList<Integer> portslist = new ArrayList<>();
- private byte[] buf = new byte[256];
- DatagramSocket socket;
- public Server(ArrayList<Integer> portslist) {
- this.portslist = portslist;
- }
- public static void main(String args[]) throws IOException {
- for(int i=0; i<args.length; i++) {
- if(!args[i].matches("\\d*")) {
- System.out.println("Program can be started by using list of ports");
- System.exit(0);
- }
- }
- System.out.println("START");
- }
- @Override
- public void run() {
- while (true) {
- try {
- socket = new DatagramSocket(2);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- DatagramPacket packet = new DatagramPacket(buf, buf.length);
- try {
- socket.receive(packet);
- } catch (IOException e) {
- e.printStackTrace();
- }
- InetAddress address = packet.getAddress();
- int port = packet.getPort();
- packet = new DatagramPacket(buf, buf.length, address, port);
- String received = new String(packet.getData(), 0, packet.getLength());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement