Java Code Examples for java.net.SocketException#getMessage()

The following examples show how to use java.net.SocketException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UdpDataSender.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private DatagramSocket createSocket(int timeout, int sendBufferSize) {
    try {
        final DatagramSocket datagramSocket = new DatagramSocket();

        datagramSocket.setSoTimeout(timeout);
        datagramSocket.setSendBufferSize(sendBufferSize);
        if (logger.isInfoEnabled()) {
            final int checkSendBufferSize = datagramSocket.getSendBufferSize();
            if (sendBufferSize != checkSendBufferSize) {
                logger.info("DatagramSocket.setSendBufferSize() error. {}!={}", sendBufferSize, checkSendBufferSize);
            }
        }

        return datagramSocket;
    } catch (SocketException e) {
        throw new IllegalStateException("DatagramSocket create fail. Cause" + e.getMessage(), e);
    }
}
 
Example 2
Source File: IPUtil.java    From dubai with MIT License 6 votes vote down vote up
public static Collection<InetAddress> getAllHostAddress() {
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        Collection<InetAddress> addresses = new ArrayList<InetAddress>();

        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                addresses.add(inetAddress);
            }
        }

        return addresses;
    } catch (SocketException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 3
Source File: NioServer.java    From TakinRPC with Apache License 2.0 6 votes vote down vote up
private void init() {

        ServerSocketChannel socketChannel = processor.javaChannel();

        ServerSocket javaSocket = socketChannel.socket();

        try {
            if (serverConfig.getReceiveBufferSize() != null) {
                javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize());
            }
            if (serverConfig.getReuseAddress() != null) {
                javaSocket.setReuseAddress(serverConfig.getReuseAddress());
            }
        } catch (SocketException e) {
            throw new NioException("config channel error:" + e.getMessage(), e);
        }
    }
 
Example 4
Source File: UDPReceiver.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private DatagramSocket createSocket0(int receiveBufferSize) {
    try {
        DatagramSocket socket = new DatagramSocket(null);
        socket.setReceiveBufferSize(receiveBufferSize);
        if (logger.isWarnEnabled()) {
            final int checkReceiveBufferSize = socket.getReceiveBufferSize();
            if (receiveBufferSize != checkReceiveBufferSize) {
                logger.warn("DatagramSocket.setReceiveBufferSize() error. {}!={}", receiveBufferSize, checkReceiveBufferSize);
            }
        }
        socket.setSoTimeout(1000 * 5);
        return socket;
    } catch (SocketException ex) {
        throw new RuntimeException("Socket create Fail. Caused:" + ex.getMessage(), ex);
    }
}
 
Example 5
Source File: NioServer.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
private void init() {

        ServerSocketChannel socketChannel = processor.javaChannel();

        ServerSocket javaSocket = socketChannel.socket();

        try {
            if (serverConfig.getReceiveBufferSize() != null) {
                javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize());
            }
            if (serverConfig.getReuseAddress() != null) {
                javaSocket.setReuseAddress(serverConfig.getReuseAddress());
            }
        } catch (SocketException e) {
            throw new NioException("config channel error:" + e.getMessage(), e);
        }
    }
 
Example 6
Source File: IPUtils.java    From opencron with Apache License 2.0 6 votes vote down vote up
public static Collection<InetAddress> getAllHostAddress() {
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        Collection<InetAddress> addresses = new ArrayList<InetAddress>();

        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                addresses.add(inetAddress);
            }
        }

        return addresses;
    } catch (SocketException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 7
Source File: GitPull.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
private String[] getHostAddresses() {
    try {
        final List<String> hostAddrs = Lists.newArrayList();
        final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        while (nis.hasMoreElements()) {
            final NetworkInterface ni = nis.nextElement();
            final Enumeration<InetAddress> addrs = ni.getInetAddresses();
            while (addrs.hasMoreElements()) {
                final InetAddress addr = addrs.nextElement();
                if (addr instanceof Inet4Address) {
                    final String hostAddr = addr.getHostAddress();
                    if (!ObjectCompare.isInList(hostAddr, HOST_FILTERS)) {
                        hostAddrs.add(hostAddr);
                    }
                }
            }
        }

        return hostAddrs.toArray(new String[hostAddrs.size()]);
    } catch (final SocketException e) {
        throw new org.nanoframework.server.exception.UnknownHostException(e.getMessage());
    }
}
 
Example 8
Source File: Util.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static boolean treatAsBindException(SocketException se) {
  if(se instanceof BindException) {
    return true;
  }
  final String msg = se.getMessage();
  return (msg != null && msg.contains("Invalid argument: listen failed"));
}
 
Example 9
Source File: RMIHydraSocketFactory.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public Socket createSocket(String host, int port) throws IOException
{
  int retryCount = INITIAL_RETRY_COUNT;
  do {
    retryCount--;
    try {
      return new Socket(host, port);
    } catch (SocketException se) {
      //IBM J9 sometimes reports "listen failed" instead of BindException
      //see bug #40589
      final String msg = se.getMessage();
      boolean reattempt = se instanceof BindException || 
                          se instanceof ConnectException || 
                          (msg != null && 
                           msg.contains("Invalid argument: listen failed"));
 
      if (!reattempt || retryCount <= 0) {
        throw se;
      } else {
        try {Thread.sleep(THROTTLE_PERIOD);} catch (InterruptedException ie) {
          retryCount = 0;
          Thread.currentThread().interrupt();
        }
      }
    }
  } while (true);
}
 
Example 10
Source File: AcceptorImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static boolean treatAsBindException(SocketException se) {
  if(se instanceof BindException) { 
    return true;
  }
  final String msg = se.getMessage();
  return (msg != null && msg.contains("Invalid argument: listen failed"));
}
 
Example 11
Source File: SmartIPUtil.java    From smart-admin with MIT License 5 votes vote down vote up
public static String getLocalHostIP() {
    // 本地IP,如果没有配置外网IP则返回它
    String localIp = null;
    // 外网IP
    String netIp = null;
    try {
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        // 是否找到外网IP
        boolean finded = false;
        while (netInterfaces.hasMoreElements() && ! finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();
                // 外网IP
                if (! ip.isSiteLocalAddress() && ! ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == - 1) {
                    netIp = ip.getHostAddress();
                    finded = true;
                    break;
                } else if (ip.isSiteLocalAddress() && ! ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == - 1) {
                    // 内网IP
                    localIp = ip.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.getMessage();
    }
    if (netIp != null && ! "".equals(netIp)) {
        return netIp;
    } else {
        return localIp;
    }
}
 
Example 12
Source File: RMIHydraSocketFactory.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public Socket createSocket(String host, int port) throws IOException
{
  int retryCount = INITIAL_RETRY_COUNT;
  do {
    retryCount--;
    try {
      return new Socket(host, port);
    } catch (SocketException se) {
      //IBM J9 sometimes reports "listen failed" instead of BindException
      //see bug #40589
      final String msg = se.getMessage();
      boolean reattempt = se instanceof BindException || 
                          se instanceof ConnectException || 
                          (msg != null && 
                           msg.contains("Invalid argument: listen failed"));
 
      if (!reattempt || retryCount <= 0) {
        throw se;
      } else {
        try {Thread.sleep(THROTTLE_PERIOD);} catch (InterruptedException ie) {
          retryCount = 0;
          Thread.currentThread().interrupt();
        }
      }
    }
  } while (true);
}
 
Example 13
Source File: AcceptorImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static boolean treatAsBindException(SocketException se) {
  if(se instanceof BindException) { 
    return true;
  }
  final String msg = se.getMessage();
  return (msg != null && msg.contains("Invalid argument: listen failed"));
}
 
Example 14
Source File: Util.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static boolean treatAsBindException(SocketException se) {
  if(se instanceof BindException) {
    return true;
  }
  final String msg = se.getMessage();
  return (msg != null && msg.contains("Invalid argument: listen failed"));
}
 
Example 15
Source File: RTPSessionMgr.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
public void addTarget(SessionAddress sessionaddress)
throws IOException
{
    remoteAddresses.addElement(sessionaddress);
    if(remoteAddresses.size() > 1)
    {
        setRemoteAddresses();
        return;
    }
    remoteAddress = sessionaddress;
    try
    {
        rtcpRawReceiver = new RTCPRawReceiver(localAddress, sessionaddress, defaultstats, streamSynch, controlSocket);
        rtpRawReceiver = new RTPRawReceiver(channel, localAddress, sessionaddress, defaultstats, dataSocket);
    }
    catch(SocketException socketexception)
    {
        throw new IOException(socketexception.getMessage());
    }
    catch(UnknownHostException unknownhostexception)
    {
        throw new IOException(unknownhostexception.getMessage());
    }
    rtpDemultiplexer = new RTPDemultiplexer(cache, rtpRawReceiver, streamSynch);
    rtcpForwarder = new PacketForwarder(rtcpRawReceiver, new RTCPReceiver(cache));
    if(rtpRawReceiver != null)
    {
        rtpForwarder = new PacketForwarder(rtpRawReceiver, new RTPReceiver(cache, rtpDemultiplexer));
    }
    rtcpForwarder.startPF("RTCP Forwarder for address" + sessionaddress.getControlHostAddress() + " port " + sessionaddress.getControlPort());
    if(rtpForwarder != null)
    {
        rtpForwarder.startPF("RTP Forwarder for address " + sessionaddress.getDataHostAddress() + " port " + sessionaddress.getDataPort());
    }
    cleaner = new SSRCCacheCleaner(cache, streamSynch);
    if(cache.ourssrc != null && participating)
    {
        cache.ourssrc.reporter = startParticipating(rtpRawReceiver.socket);
    }
}
 
Example 16
Source File: ExceptionHandling.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
@ExceptionHandler
public String test4(SocketException e){
    ModelAndView modelAndView = new ModelAndView("/index");
    Print.Normal("存储源配置不正确,初始化失败"+ e.getMessage());
    e.printStackTrace();
    modelAndView.addObject("error",e.getMessage());
    //返回错误信息,并显示给用户
    return e.getMessage();
}
 
Example 17
Source File: HttpProxyTransportWrapper.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Connect the socket to a HTTP proxy and request forwarding to our remote
 * host.
 * 
 * @param host
 * @param port
 * @param proxyHost
 * @param proxyPort
 * @param username
 * @param password
 * @param userAgent
 * @return HttpProxyTransportWrapper
 * @throws IOException
 * @throws UnknownHostException
 */
public static HttpProxyTransportWrapper connectViaProxy(String host,
		int port, String proxyHost, int proxyPort, String username,
		String password, String userAgent) throws IOException,
		UnknownHostException {
	HttpProxyTransportWrapper socket = new HttpProxyTransportWrapper(host,
			port, proxyHost, proxyPort);
	int status;
	socket.username = username;
	socket.password = password;
	socket.userAgent = userAgent;

	try {
		InputStream in = socket.getInputStream();
		OutputStream out = socket.getOutputStream();
		HttpRequest request = new HttpRequest();

		request.setHeaderBegin("CONNECT " + host + ":" + port + " HTTP/1.0");
		request.setHeaderField("User-Agent", userAgent);
		request.setHeaderField("Pragma", "No-Cache");
		request.setHeaderField("Host", host);
		request.setHeaderField("Proxy-Connection", "Keep-Alive");
		out.write(request.toString().getBytes());
		out.flush();
		socket.responseHeader = new HttpResponse(in);

		if (socket.responseHeader.getStatus() == 407) {
			String realm = socket.responseHeader.getAuthenticationRealm();
			String method = socket.responseHeader.getAuthenticationMethod();

			if (realm == null) {
				realm = "";
			}

			if (method.equalsIgnoreCase("basic")) {
				socket.close();
				socket = new HttpProxyTransportWrapper(host, port,
						proxyHost, proxyPort);
				in = socket.getInputStream();
				out = socket.getOutputStream();
				request.setBasicAuthentication(username, password);
				out.write(request.toString().getBytes());
				out.flush();
				socket.responseHeader = new HttpResponse(in);
			} else if (method.equalsIgnoreCase("digest")) {
				throw new IOException(
						"Digest authentication is not supported");
			} else {
				throw new IOException("'" + method + "' is not supported");
			}
		}

		status = socket.responseHeader.getStatus();

	} catch (SocketException e) {
		throw new SocketException("Error communicating with proxy server "
				+ proxyHost + ":" + proxyPort + " (" + e.getMessage() + ")");
	} finally {

	}

	if ((status < 200) || (status > 299)) {
		throw new IOException("Proxy tunnel setup failed: "
				+ socket.responseHeader.getStartLine());
	}

	socket.setSoTimeout(0);
	return socket;
}
 
Example 18
Source File: SocksProxyTransport.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Connect the socket to a SOCKS 4 proxy and request forwarding to our
 * remote host.
 * 
 * @param remoteHost
 * @param remotePort
 * @param proxyHost
 * @param proxyPort
 * @param userId
 * @return SocksProxyTransport
 * @throws IOException
 * @throws UnknownHostException
 */
public static SocksProxyTransport connectViaSocks4Proxy(String remoteHost,
		int remotePort, String proxyHost, int proxyPort, String userId)
		throws IOException, UnknownHostException {
	SocksProxyTransport proxySocket = new SocksProxyTransport(remoteHost,
			remotePort, proxyHost, proxyPort, SOCKS4);
	proxySocket.username = userId;
	try {
		InputStream proxyIn = proxySocket.getInputStream();
		OutputStream proxyOut = proxySocket.getOutputStream();
		InetAddress hostAddr = InetAddress.getByName(remoteHost);
		proxyOut.write(SOCKS4);
		proxyOut.write(CONNECT);
		proxyOut.write((remotePort >>> 8) & 0xff);
		proxyOut.write(remotePort & 0xff);
		proxyOut.write(hostAddr.getAddress());
		proxyOut.write(userId.getBytes());
		proxyOut.write(NULL_TERMINATION);
		proxyOut.flush();

		int res = proxyIn.read();

		if (res == -1) {
			throw new IOException("SOCKS4 server " + proxyHost + ":"
					+ proxyPort + " disconnected");
		}

		if (res != 0x00) {
			throw new IOException("Invalid response from SOCKS4 server ("
					+ res + ") " + proxyHost + ":" + proxyPort);
		}

		int code = proxyIn.read();

		if (code != 90) {
			if ((code > 90) && (code < 93)) {
				throw new IOException(
						"SOCKS4 server unable to connect, reason: "
								+ SOCKSV4_ERROR[code - 91]);
			}
			throw new IOException(
					"SOCKS4 server unable to connect, reason: " + code);
		}

		byte[] data = new byte[6];

		if (proxyIn.read(data, 0, 6) != 6) {
			throw new IOException(
					"SOCKS4 error reading destination address/port");
		}

		proxySocket.setProviderDetail(data[2] + "." + data[3] + "."
				+ data[4] + "." + data[5] + ":"
				+ ((data[0] << 8) | data[1]));
	} catch (SocketException e) {
		throw new SocketException("Error communicating with SOCKS4 server "
				+ proxyHost + ":" + proxyPort + ", " + e.getMessage());
	}

	return proxySocket;
}
 
Example 19
Source File: RTPSessionMgr.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
public int startSession(SessionAddress sessionaddress, SessionAddress sessionaddress1, SessionAddress sessionaddress2, EncryptionInfo encryptioninfo)
throws IOException, InvalidSessionAddressException
{
    if(started)
    {
        return -1;
    }
    localSenderAddress = sessionaddress1;
    cache.sessionbandwidth = 0x5dc00;
    controlport = sessionaddress.getControlPort();
    dataport = sessionaddress.getDataPort();
    CheckRTPPorts(dataport, controlport);
    dataaddress = sessionaddress.getDataAddress();
    controladdress = sessionaddress.getControlAddress();
    if(dataaddress.isMulticastAddress() || controladdress.isMulticastAddress() || isBroadcast(dataaddress) || isBroadcast(controladdress))
    {
        throw new InvalidSessionAddressException("Local Address must be UNICAST IP addresses");
    }
    CheckRTPAddress(dataaddress, controladdress);
    RTCPRawReceiver rtcprawreceiver = null;
    RTPRawReceiver rtprawreceiver = null;
    InetAddress inetaddress = null;
    try
    {
        inetaddress = InetAddress.getLocalHost();
    }
    catch(Throwable throwable1)
    {
        System.err.println("InitSession : UnknownHostExcpetion " + throwable1.getMessage());
        throwable1.printStackTrace();
        return -1;
    }
    try
    {
        rtcprawreceiver = new RTCPRawReceiver(controlport, controladdress.getHostAddress(), defaultstats, streamSynch);
        if(dataaddress != null)
        {
            rtprawreceiver = new RTPRawReceiver(channel, dataport, dataaddress.getHostAddress(), defaultstats);
        }
    }
    catch(SocketException socketexception)
    {
        throw new IOException(socketexception.getMessage());
    }
    finally
    {
        if(dataaddress != null && rtprawreceiver == null && rtcprawreceiver != null)
        {
            System.err.println("could not create RTCP/RTP raw receivers");
            rtcprawreceiver.closeSource();
        }
    }
    rtpDemultiplexer = new RTPDemultiplexer(cache, rtprawreceiver, streamSynch);
    rtcpForwarder = new PacketForwarder(rtcprawreceiver, new RTCPReceiver(cache));
    if(rtprawreceiver != null)
    {
        rtpForwarder = new PacketForwarder(rtprawreceiver, new RTPReceiver(cache, rtpDemultiplexer));
    }
    rtcpForwarder.startPF("RTCP Forwarder for address" + controladdress.toString() + "port " + controlport);
    if(rtpForwarder != null)
    {
        rtpForwarder.startPF("RTP Forwarder for address " + dataaddress.toString() + "port " + dataport);
    }
    controlport = sessionaddress2.getControlPort();
    dataport = sessionaddress2.getDataPort();
    CheckRTPPorts(dataport, controlport);
    dataaddress = sessionaddress2.getDataAddress();
    controladdress = sessionaddress2.getControlAddress();
    if(dataaddress.isMulticastAddress() || controladdress.isMulticastAddress() || isBroadcast(dataaddress) || isBroadcast(controladdress))
    {
        throw new InvalidSessionAddressException("Remote Address must be UNICAST IP addresses");
    }
    CheckRTPAddress(dataaddress, controladdress);
    cleaner = new SSRCCacheCleaner(cache, streamSynch);
    if(!nonparticipating && !unicast && cache.ourssrc != null)
    {
        cache.ourssrc.reporter = startParticipating(sessionaddress, sessionaddress1, cache.ourssrc, rtcprawreceiver.socket);
    }
    started = true;
    return 0;
}
 
Example 20
Source File: AbstractChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
AnnotatedSocketException(SocketException exception, SocketAddress remoteAddress) {
    super(exception.getMessage() + ": " + remoteAddress);
    initCause(exception);
    setStackTrace(exception.getStackTrace());
}