uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

UDPServer.java (960B)


      1 import java.net.*;
      2 
      3 public class UDPServer {
      4 	public static void main(String[] args) throws Exception {
      5 		DatagramSocket srvsock;
      6 		DatagramPacket sendpack, recvpack;
      7 		InetAddress addr;
      8 		String msg;
      9 		byte[] senddata = new byte[1024];
     10 		byte[] recvdata = new byte[1024];
     11 		int port = 8888, recvport;
     12 
     13 		if (args.length == 0)
     14 			System.out.println("usage: java UDPServer [port]");
     15 		else
     16 			port = Integer.parseInt(args[0]);
     17 		System.out.println("[UDPServer] starting");
     18 		srvsock = new DatagramSocket(port);
     19 
     20 		while (true) {
     21 			recvpack = new DatagramPacket(recvdata, recvdata.length);
     22 			srvsock.receive(recvpack);
     23 			msg = new String(recvpack.getData(), 0,
     24 			    recvpack.getLength());
     25 			
     26 			System.out.println("your message: " + msg);
     27 			addr = recvpack.getAddress();
     28 			recvport = recvpack.getPort();
     29 			senddata = msg.getBytes();
     30 			sendpack = new DatagramPacket(senddata,
     31 			    senddata.length, addr, recvport);
     32 			srvsock.send(sendpack);
     33 		}
     34 	}
     35 }