/* * $Id: UDPChannelAddress.java,v 1.5 1997/10/27 03:58:42 djw Exp $ * * Copyright 1996 Massachusetts Institute of Technology * * Permission to use, copy, modify, distribute, and sell this software and * its documentation for any purpose is hereby granted without fee, provided * that the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of M.I.T. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. M.I.T. makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * */ package ants; import java.net.InetAddress; import java.net.UnknownHostException; /** * UDP singlecast address of a channel * * @author David Wetherall */ public class UDPChannelAddress extends ChannelAddress { public InetAddress address; public int port; public String toString() { return address.getHostName() + ":" + port; } public UDPChannelAddress(InetAddress address, int port) { this.address = address; this.port = port; } public UDPChannelAddress(String hp) throws Exception { int idx = hp.indexOf(':'); int len = hp.length(); if (idx <= 0 || idx >= len-1) throw new Exception("want host:port, got " + hp); String h = null; String p = null; try { h = hp.substring(0, idx); p = hp.substring(idx+1); } catch (StringIndexOutOfBoundsException e) { // should not be able to reach here throw new Exception("trouble in UDPChannelAddress!"); } address = InetAddress.getByName(h); try { port = Integer.parseInt(p); } catch (NumberFormatException e) { throw new Exception("non-numeric port " + p); } } public static void main(String[] args) throws Exception { String[] h = { "localhost:8000", "18.31.0.126:4096" }; for (int i = 0; i < h.length; i++) { UDPChannelAddress n = new UDPChannelAddress(h[i]); String s = n.toString(); System.out.println(h[i] + " -> " + s); } } }