001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }