Example usage for java.net UnknownHostException printStackTrace

List of usage examples for java.net UnknownHostException printStackTrace

Introduction

In this page you can find the example usage for java.net UnknownHostException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:org.codelabor.system.remoting.tcp.services.SimpleSocketAdapterServiceImpl.java

public String send(String inputMessage) throws Exception {
    Socket socket = null;//from  w  w  w.j a v a2s  . c o m
    InputStream inputStream = null;
    OutputStream outputStream = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    String receivedMessage = null;

    try {
        // connect to server
        socket = new Socket(host, port);

        // setup stream
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();

        // send message
        outputStream.write(inputMessage.getBytes(charsetName));
        outputStream.flush();
        if (log.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("sent message: [").append(inputMessage).append("]");
            log.debug(sb.toString());
        }

        int readSize = 0;
        int totalReadSize = 0;

        byte[] messageLengthBytes = new byte[MESSAGE_LENGTH_FIELD_LENGTH];
        inputStream.read(messageLengthBytes, 0, MESSAGE_LENGTH_FIELD_LENGTH);
        String messageLengthString = new String(messageLengthBytes, charsetName);
        totalReadSize += MESSAGE_LENGTH_FIELD_LENGTH;
        int messageLength = Integer.parseInt(messageLengthString);

        // read input stream
        byte[] buffer = new byte[8 * 1024];
        byteArrayOutputStream = new ByteArrayOutputStream();
        bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
        while (messageLength > totalReadSize && (readSize = inputStream.read(buffer)) != -1) {
            bufferedOutputStream.write(buffer, 0, readSize);
            bufferedOutputStream.flush();
            totalReadSize += readSize;
            if (log.isDebugEnabled()) {
                StringBuilder sb = new StringBuilder();
                sb.append("read size: ").append(readSize);
                sb.append(", ");
                sb.append("total read size: ").append(totalReadSize);
                sb.append(", ");
                sb.append("message length: ").append(messageLength);
                log.debug(sb.toString());
            }
        }
        byte[] remainsMessageBytes = byteArrayOutputStream.toByteArray();
        byte[] receivedMessageBytes = new byte[messageLengthBytes.length + remainsMessageBytes.length];
        System.arraycopy(messageLengthBytes, 0, receivedMessageBytes, 0, messageLengthBytes.length);
        System.arraycopy(remainsMessageBytes, 0, receivedMessageBytes, messageLengthBytes.length,
                remainsMessageBytes.length);
        receivedMessage = new String(receivedMessageBytes, charsetName);

        if (log.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("received message: [").append(receivedMessage).append("]");
            log.debug(sb.toString());
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (byteArrayOutputStream != null)
            byteArrayOutputStream.close();
        if (bufferedOutputStream != null)
            bufferedOutputStream.close();
        if (inputStream != null)
            inputStream.close();
        if (outputStream != null)
            outputStream.close();
        if (socket != null)
            socket.close();
    }
    return receivedMessage;
}

From source file:com.ayu.filter.RegularService.java

@Async
public void registerUser(String ip, String date, String type, String document) {

    //System.out.println(" Attack from  "+ip +" captured at"+ date+" "+"type of attack is"+" "+type);
    //System.out.println(" Database Insertion ");

    try {/*  ww  w.  j av a 2s.c  o  m*/
        Thread.sleep(5000);

    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    MongoClient mongoClient = null;
    try {
        mongoClient = new MongoClient();
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // or, to connect to a replica set, supply a seed list of members
    //MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
    //                                    new ServerAddress("localhost", 27018),
    //                                  new ServerAddress("localhost", 27019)));

    DB db = mongoClient.getDB(document);
    DBCollection coll;
    coll = db.getCollection(document);
    BasicDBObject doc = new BasicDBObject("Ip-Address", ip).append("Date", date).append("Attack-Type", type);

    //System.out.println("Data Display");
    coll.insert(doc);
    mongoClient.close();

    //System.out.println(" Asynchronous method call of database  Complete ");

}

From source file:org.privatenotes.sync.web.WebConnection.java

protected HttpResponse execute(HttpUriRequest request) throws UnknownHostException {

    DefaultHttpClient httpclient = new DefaultHttpClient();

    try {/*  www .ja v  a 2s .com*/
        // Execute the request
        HttpResponse response = httpclient.execute(request);
        return response;

    } catch (UnknownHostException e) {
        throw e;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.qpark.eip.core.spring.statistics.impl.AppUserStatisticsChannelAdapter.java

/**
 * Get the host name of the server.//from ww w  . j a  v a 2 s.  co m
 *
 * @return the host name or <code>null</code>.
 */
String getHostName() {
    if (this.hostName == null) {
        try {
            this.hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return this.hostName;
}

From source file:org.changhong.sync.web.WebConnection.java

protected HttpResponse execute(HttpUriRequest request) throws UnknownHostException {

    DefaultHttpClient httpclient = MySSLSocketFactory.getNewHttpClient();

    try {/*from   w w  w . j  a  va 2  s  . c  o m*/
        // Execute the request
        TLog.i(TAG, "Sending http-header: {0}: {1}", "X-Tomboy-Client", Tomdroid.HTTP_HEADER);
        request.addHeader("X-Tomboy-Client", Tomdroid.HTTP_HEADER);
        HttpResponse response = httpclient.execute(request);
        return response;

    } catch (UnknownHostException e) {
        throw e;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        TLog.i(TAG, "Somethings wrong with your HTTP request. Maybe errors in SSL, certificate?");
        e.printStackTrace();
    }

    return null;
}

From source file:org.apache.camel.processor.idempotent.mongodb.test.TestIdempotentRepositoryInRoute.java

private void prepareDB() {

    Mongo mongo;/*from   w w  w. ja  va  2s .  c  o  m*/
    try {

        mongo = new Mongo(host1, port1);
        db = mongo.getDB(dbName1);
        coll = db.getCollection(collectionName1);

        this.cleanDb();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:org.openconcerto.sql.PropsConfiguration.java

public static String getHostname() {
    final InetAddress addr;
    try {/* w ww  .  j ava 2 s.  c  o  m*/
        addr = InetAddress.getLocalHost();
    } catch (final UnknownHostException e) {
        e.printStackTrace();
        return "local";
    }
    return addr.getHostName();
}

From source file:com.jagornet.dhcp.client.GenerateTestConfig.java

public void generate() {
    try {/*  w  w  w  .j  a v  a  2 s .  c  om*/
        DhcpServerConfig config = DhcpServerConfig.Factory.newInstance();

        V6ServerIdOption serverId = V6ServerIdOption.Factory.newInstance();
        serverId.setOpaqueData(OpaqueDataUtil.generateDUID_LLT());
        config.setV6ServerIdOption(serverId);

        V4ServerIdOption v4ServerId = V4ServerIdOption.Factory.newInstance();
        String myIp = ipv4Address.getHostAddress();
        v4ServerId.setIpAddress(myIp);
        config.setV4ServerIdOption(v4ServerId);

        PoliciesType policies = PoliciesType.Factory.newInstance();
        Policy policy = policies.addNewPolicy();
        policy.setName("dhcp.ignoreSelfPackets");
        policy.setValue("false");
        config.setPolicies(policies);

        LinksType links = LinksType.Factory.newInstance();

        Link v4Link = links.addNewLink();
        v4Link.setName("Test IPv4 Client Link");
        // assume the client is on a /24 size IPv4 subnet
        int p = myIp.lastIndexOf('.');
        String myNet = myIp.substring(0, p + 1);
        v4Link.setAddress(myNet.concat("0/24"));
        V4ConfigOptionsType v4Options = v4Link.addNewV4ConfigOptions();
        v4Options.addNewV4SubnetMaskOption().setIpAddress("255.255.255.0");
        // assume the router is at .1
        v4Options.addNewV4RoutersOption().addIpAddress(myNet.concat("1"));
        v4Options.addNewV4DomainServersOption().addIpAddress("10.0.0.1");
        v4Options.addNewV4DomainNameOption().setString("jagornet.test.com");
        V4AddressPoolsType v4Pools = v4Link.addNewV4AddrPools();
        // create a pool of 50 addresses at the end of subnet
        v4Pools.addNewPool().setRange(myNet + "200-" + myNet + "250");

        Link v6Link = links.addNewLink();
        v6Link.setName("Test IPv6 Client Link");
        String myIf = networkInterface.getName();
        v6Link.setInterface(myIf);
        V6ConfigOptionsType v6Options = v6Link.addNewV6MsgConfigOptions();
        v6Options.addNewV6DnsServersOption().addIpAddress("2001:db8:1::1");
        v6Options.addNewV6DomainSearchListOption().addDomainName("jagornet.test.com");
        V6AddressPoolsType v6Pools = v6Link.addNewV6NaAddrPools();
        v6Pools.addNewPool().setRange("2001:db8:1::/64");

        config.setLinks(links);
        DhcpServerConfiguration.saveConfig(config, filename);

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.drinviewer.droiddrinviewer.DrinViewerBroadcastReceiver.java

private String getWiFiBroadcastAddress(Context context) {
    String bcastaddr = null;/*  w  ww .  j  a va 2 s .co  m*/
    WifiManager mWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = mWifi.getDhcpInfo();

    if (mWifi.isWifiEnabled() && dhcp != null) {
        int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
            quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);

        try {
            bcastaddr = InetAddress.getByAddress(quads).getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return bcastaddr;
}

From source file:org.magnum.dataup.VideoController.java

private String getUrlBaseForLocalServer() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();//from ww w . jav  a  2 s.  co m
    InetAddress IP = null;
    try {
        IP = getLocalHostLANAddress();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    System.out.println("IP of my system is := " + IP.getHostAddress());
    String base = "http://" + IP.getHostAddress()
            + ((request.getServerPort() != 80) ? ":" + request.getServerPort() : "");
    System.out.println("dataUrl is :" + base);
    return base;
}