Here you can find the source of toByte(InetSocketAddress socketAddress)
public static byte[] toByte(InetSocketAddress socketAddress)
//package com.java2s; //License from project: Apache License import java.net.*; public class Main { public static byte[] toByte(InetSocketAddress socketAddress) { if (socketAddress == null) { throw new IllegalArgumentException("socketAddress is null"); }//w ww. j av a 2 s . c o m InetAddress inetAddress = socketAddress.getAddress(); if (inetAddress == null) { throw new IllegalArgumentException("socketAddress is invalid"); } byte[] address = inetAddress.getAddress(); byte[] result = new byte[address.length + 2]; System.arraycopy(address, 0, result, 2, address.length); int port = socketAddress.getPort(); result[1] = (byte) (port >> 8 & 0xFF); result[0] = (byte) (port & 0xFF); return result; } public static String getAddress(SocketAddress address) { if (address == null) { return null; } if (address instanceof InetSocketAddress) { InetSocketAddress isa = (InetSocketAddress) address; return isa.getAddress().getHostAddress() + ":" + isa.getPort(); } else { return address.toString(); } } }