001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.IOException;
018    
019    import java.net.InetAddress;
020    import java.net.NetworkInterface;
021    import java.net.Socket;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class SocketUtil {
027    
028            public static BindInfo getBindInfo(String host, int port)
029                    throws IOException {
030    
031                    Socket socket = null;
032    
033                    try {
034                            socket = new Socket(host, port);
035    
036                            InetAddress inetAddress = socket.getLocalAddress();
037                            NetworkInterface networkInterface =
038                                    NetworkInterface.getByInetAddress(inetAddress);
039    
040                            BindInfo bindInfo = new BindInfo();
041    
042                            bindInfo.setInetAddress(inetAddress);
043                            bindInfo.setNetworkInterface(networkInterface);
044    
045                            return bindInfo;
046                    }
047                    finally {
048                            if (socket != null) {
049                                    try {
050                                            socket.close();
051                                    }
052                                    catch (IOException ioe) {
053                                    }
054                            }
055                    }
056            }
057    
058            public static class BindInfo {
059    
060                    public InetAddress getInetAddress() {
061                            return _inetAddress;
062                    }
063    
064                    public NetworkInterface getNetworkInterface() {
065                            return _networkInterface;
066                    }
067    
068                    public void setInetAddress(InetAddress inetAddress) {
069                            _inetAddress = inetAddress;
070                    }
071    
072                    public void setNetworkInterface(NetworkInterface networkInterface) {
073                            _networkInterface = networkInterface;
074                    }
075    
076                    private InetAddress _inetAddress;
077                    private NetworkInterface _networkInterface;
078    
079            }
080    
081    }