Showing posts with label network. Show all posts
Showing posts with label network. Show all posts

Learning Network Programming with Java

| 0 comments |
Learning Network Programming with Java

Key Features
  • Learn to deliver superior server-to-server communication through the networking channels
  • Gain expertise of the networking features of your own applications to support various network architectures such as client/server and peer-to-peer
  • Explore the issues that impact scalability, affect security, and allow applications to work in a heterogeneous environment
Book Description
Network-aware applications are becoming more prevalent and play an ever-increasing role in the world today. Connecting and using an Internet-based service is a frequent requirement for many applications. Java provides numerous classes that have evolved over the years to meet evolving network needs. These range from low-level socket and IP-based approaches to those encapsulated in software services.

This book explores how Java supports networks, starting with the basics and then advancing to more complex topics. An overview of each relevant network technology is presented followed by detailed examples of how to use Java to support these technologies.

We start with the basics of networking and then explore how Java supports the development of client/server and peer-to-peer applications. The NIO packages are examined as well as multitasking and how network applications can address practical issues such as security.

A discussion on networking concepts will put many network issues into perspective and let you focus on the appropriate technology for the problem at hand. The examples used will provide a good starting point to develop similar capabilities for many of your network needs.

What you will learn
  • Connect to other applications using sockets
  • Use channels and buffers to enhance communication between applications
  • Access network services and develop client/server applications
  • Explore the critical elements of peer-to-peer applications and current technologies available
  • Use UDP to perform multicasting
  • Address scalability through the use of core and advanced threading techniques
  • Incorporate techniques into an application to make it more secure
  • Configure and address interoperability issues to enable your applications to work in a heterogeneous environment
About the Author
Richard M Reese has worked in both industry and academia. For 17 years, he worked in the telephone and aerospace industries, serving in several capacities, including research and development, software development, supervision, and training. He currently teaches at Tarleton State University, where he has the opportunity to apply his years of industry experience to enhance his teaching.

Richard has written several Java books and a C Pointer book. He uses a concise and easy-to-follow approach to topics at hand. His Java books have addressed EJB 3.1, updates to Java 7 and 8, certification, functional programming, jMonkeyEngine, and natural language processing.

Table of Contents
  1. Getting Started with Network Programming
  2. Network Addressing
  3. NIO Support for Networking
  4. Client/Server Development
  5. Peer-to-Peer Networks
  6. UDP and Multicasting
  7. Network Scalability
  8. Network Security
  9. Network Interoperability


Read More..

Scan Reachable IP to discover devices in network

| 0 comments |

This example, scan a range of IPs, to check if it is reachable, in turn to discover connected devices in the same network.


MainActivity.java
package com.blogspot.android_er.androidipscanner;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private Button btnScan;
private ListView listViewIp;

ArrayList<String> ipList;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button)findViewById(R.id.scan);
listViewIp = (ListView)findViewById(R.id.listviewip);


ipList = new ArrayList();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
listViewIp.setAdapter(adapter);

btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScanIpTask().execute();
}
});

}

private class ScanIpTask extends AsyncTask<Void, String, Void>{

/*
Scan IP 192.168.1.100~192.168.1.110
you should try different timeout for your network/devices
*/
static final String subnet = "192.168.1.";
static final int lower = 100;
static final int upper = 110;
static final int timeout = 5000;

@Override
protected void onPreExecute() {
ipList.clear();
adapter.notifyDataSetInvalidated();
Toast.makeText(MainActivity.this, "Scan IP...", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... params) {

for (int i = lower; i <= upper; i++) {
String host = subnet + i;

try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(timeout)){
publishProgress(inetAddress.toString());
}

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

return null;
}

@Override
protected void onProgressUpdate(String... values) {
ipList.add(values[0]);
adapter.notifyDataSetInvalidated();
Toast.makeText(MainActivity.this, values[0], Toast.LENGTH_LONG).show();
}

@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_LONG).show();
}
}

}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context=".MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<Button
android_id="@+id/scan"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Scan"/>
<ListView
android_id="@+id/listviewip"
android_layout_width="match_parent"
android_layout_height="wrap_content"/>
</LinearLayout>


Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml
 <uses-permission android_name="android.permission.INTERNET"/>



Related:
- Java version run on Raspberry Pi - scan connected IP in the same network

Read More..

Java code to listing Network Interface Addresses run on Windows 10

| 0 comments |
One of the most useful pieces of information you can get from a network interface is the list of IP addresses that are assigned to it. You can obtain this information from a NetworkInterface instance by using one of two methods. The first method, getInetAddresses(), returns an Enumeration of InetAddress. The other method, getInterfaceAddresses(), returns a list of java.net.InterfaceAddress instances. This method is used when you need more information about an interface address beyond its IP address. For example, you might need additional information about the subnet mask and broadcast address when the address is an IPv4 address, and a network prefix length in the case of an IPv6 address. ~ https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html

The following example program lists all the network interfaces and their addresses on a machine (tested on PC running Windows 10):
package javalistinetaddress;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

public class JavaListInetAddress {

public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}

static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
System.out.printf("Display name: %s ", netint.getDisplayName());
System.out.printf("Name: %s ", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
System.out.printf("InetAddress: %s ", inetAddress);
}
System.out.printf(" ");
}

}



Related:
- Java code to listing Network Interface Addresses, run on Raspberry Pi/Raspbian Jessie

Read More..