use of java.net.SocketException in project Smack by igniterealtime.
the class STUNResolverTest method testICEPriority.
/**
* Test priority generated by STUN lib
*
* @throws Exception
*/
public void testICEPriority() throws Exception {
String first = "";
for (int i = 0; i < 100; i++) {
ICENegociator cc = new ICENegociator((short) 1);
// gather candidates
cc.gatherCandidateAddresses();
// priorize candidates
cc.prioritizeCandidates();
for (Candidate candidate : cc.getSortedCandidates()) {
short nicNum = 0;
try {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
short tempNic = 0;
NetworkInterface nic = NetworkInterface.getByInetAddress(candidate.getAddress().getInetAddress());
while (nics.hasMoreElements()) {
NetworkInterface checkNIC = nics.nextElement();
if (checkNIC.equals(nic)) {
nicNum = tempNic;
break;
}
i++;
}
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
TransportCandidate transportCandidate = new ICECandidate(candidate.getAddress().getInetAddress().getHostAddress(), 1, nicNum, "1", candidate.getPort(), "1", candidate.getPriority(), ICECandidate.Type.prflx);
transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress());
System.out.println("C: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " p:" + candidate.getPriority());
} catch (UtilityException e) {
LOGGER.log(Level.WARNING, "exception", e);
} catch (UnknownHostException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
}
Candidate candidate = cc.getSortedCandidates().get(0);
String temp = "C: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " p:" + candidate.getPriority();
if (first.equals(""))
first = temp;
assertEquals(first, temp);
first = temp;
}
}
use of java.net.SocketException in project openkit-android by OpenKit.
the class AsyncHttpRequest method makeRequestWithRetries.
private void makeRequestWithRetries() throws ConnectException {
// This is an additional layer of retry logic lifted from droid-fu
// See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
boolean retry = true;
IOException cause = null;
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
while (retry) {
try {
makeRequest();
return;
} catch (UnknownHostException e) {
if (responseHandler != null) {
responseHandler.sendFailureMessage(e, "can't resolve host");
}
return;
} catch (SocketException e) {
// Added to detect host unreachable
if (responseHandler != null) {
responseHandler.sendFailureMessage(e, "can't resolve host");
}
return;
} catch (SocketTimeoutException e) {
if (responseHandler != null) {
responseHandler.sendFailureMessage(e, "socket time out");
}
return;
} catch (IOException e) {
cause = e;
retry = retryHandler.retryRequest(cause, ++executionCount, context);
} catch (NullPointerException e) {
// there's a bug in HttpClient 4.0.x that on some occasions causes
// DefaultRequestExecutor to throw an NPE, see
// http://code.google.com/p/android/issues/detail?id=5255
cause = new IOException("NPE in HttpClient" + e.getMessage());
retry = retryHandler.retryRequest(cause, ++executionCount, context);
}
}
// no retries left, crap out with exception
ConnectException ex = new ConnectException();
ex.initCause(cause);
throw ex;
}
use of java.net.SocketException in project FastDev4Android by jiangqqlmj.
the class IoUtils method getInputStreamFromUrl.
public static InputStream getInputStreamFromUrl(String url, RequestCallBack requestCallBack) {
if (url == null || !url.contains("http://")) {
Log.e("listlogic", "列表下载地址异常");
return null;
}
if (requestCallBack != null) {
requestCallBack.onRequestStart();
}
if (requestCallBack != null) {
requestCallBack.onRequestLoading();
}
URI encodedUri = null;
HttpGet httpGet = null;
try {
encodedUri = new URI(url);
httpGet = new HttpGet(encodedUri);
} catch (URISyntaxException e) {
// 清理一些空格
String encodedUrl = url.replace(' ', '+');
httpGet = new HttpGet(encodedUrl);
e.printStackTrace();
}
// 创建httpclient对象
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEF_CONN_TIMEOUT);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DEF_SOCKET_TIMEOUT);
HttpResponse httpResponse = null;
InputStream inputStream = null;
try {
try {
// 执行请求
httpResponse = httpClient.execute(httpGet);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
if (httpResponse != null) {
int httpCode = httpResponse.getStatusLine().getStatusCode();
if (httpCode == HttpStatus.SC_OK) {
// 请求数据
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
inputStream = httpEntity.getContent();
byte[] bytes = getByteArrayFromInputstream(inputStream);
if (bytes != null) {
InputStream inputStream2 = new ByteArrayInputStream(bytes);
if (requestCallBack != null) {
requestCallBack.onRequestSuccess(inputStream2);
}
return inputStream2;
}
}
} else {
httpGet.abort();
if (requestCallBack != null) {
requestCallBack.onRequestError(RequestCallBack.HTTPSTATUSERROR, "HTTP链接错误");
}
}
} else {
httpGet.abort();
if (requestCallBack != null) {
requestCallBack.onRequestError(RequestCallBack.HTTPRESPONSEERROR, "数据获取异常");
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
if (requestCallBack != null) {
requestCallBack.onRequestError(RequestCallBack.HTTPRESPONSEERROR, "数据获取异常");
}
} catch (IOException e) {
e.printStackTrace();
if (requestCallBack != null) {
requestCallBack.onRequestError(RequestCallBack.HTTPRESPONSEERROR, "数据获取异常");
}
} finally {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (requestCallBack != null) {
requestCallBack.onCancel();
}
}
return null;
}
use of java.net.SocketException in project jmxtrans by jmxtrans.
the class TCollectorUDPWriterTests method testSocketException.
/**
* Test a socket exception when creating the DatagramSocket.
*/
@Test
public void testSocketException() throws Exception {
// Prepare
SocketException sockExc = new SocketException("X-SOCK-EXC-X");
PowerMockito.whenNew(DatagramSocket.class).withNoArguments().thenThrow(sockExc);
try {
// Execute
this.writer.start();
Assert.fail("LifecycleException missing");
} catch (LifecycleException lcExc) {
// Verify
Assert.assertSame(sockExc, lcExc.getCause());
Mockito.verify(this.mockLog).error(contains("create a datagram socket"), eq(sockExc));
}
}
use of java.net.SocketException in project Smack by igniterealtime.
the class BasicResolver method resolve.
/**
* Resolve the IP address.
* <p/>
* The BasicResolver takes the IP addresses of the interfaces and uses the
* first non-loopback, non-linklocal and non-sitelocal address.
* @throws NotConnectedException
* @throws InterruptedException
*/
@Override
public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException, InterruptedException {
setResolveInit();
clearCandidates();
Enumeration<NetworkInterface> ifaces = null;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
addCandidate(tr);
setResolveEnd();
return;
}
}
}
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
addCandidate(tr);
setResolveEnd();
return;
}
}
}
try {
TransportCandidate tr = new TransportCandidate.Fixed(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName(), getFreePort());
tr.setLocalIp(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName());
addCandidate(tr);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "exception", e);
}
setResolveEnd();
}
Aggregations