I want access serial com port in windows 8 but I'm unable to get com port using java but com port is showing in "Device Manager". Please do the needfull. Is any additional setting is required ?
I'm using the following code in windows8.
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
public class SerialPortReading implements SerialPortEventListener {
public static void main(String[] args) {
//
// Get an enumeration of all ports known to JavaComm
//
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null; // will be set if port found
while (portIdentifiers.hasMoreElements())
{
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(pid.getName());
}
}
#Override
public void serialEvent(SerialPortEvent se) {
System.out.println("ok");
}
}
Java Communications API does not support Windows any more: Oracle Download Page. Use another library for example:
JSerialComm
Related
I created a web service using Eclipse (luna), Tomcat8 and Axis2.To do, I wrote in Java a class 'AddOperator' defining the service as folow:
public class AddOperator {
public int add(int x,int y) {
return x+y;
}
}
I created also a client who consomms this service using the class'AddClientOpp' as folow:
public class AddClientOpp {
public static void main(String[] args) throws RemoteException {
AddOperatorStub stub = new AddOperatorStub();
Add add = new Add();
add.setX(25);
add.setY(30);
System.out.println(stub.add(add).get_return());
}
}
Now, I need to obtain the client's IP adress consomming my web service. I searched and found a few methods and portions of codes that would allow to do this, such as:
import javax.servlet.http.HttpServletRequest;
//...
private static String getClientIp(HttpServletRequest request) {
String remoteAddr = "";
if (request != null) {
remoteAddr = request.getHeader("X-FORWARDED-FOR");
if (remoteAddr == null || "".equals(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
}
}
return remoteAddr;
}
or:
import java.net.*;
import java.io.*;
import java.applet.*;
public class GetClientIP extends Applet {
public void init() {
try {
InetAddress Ip =InetAddress.getLocalHost();
System.out.println("IP:"+Ip.getHostAddress());
} catch(Exception e) {
e.printStackTrace();
}
}
}
But I do not know where to integrate these code portions or methods into my web service creation process described above!!
if someone has an idea about thi please tell me what i have to do. it's verry important for my project!!
thanks.
You can use the following code to retrieve the client address:
MessageContext.getCurrentMessageContext().getProperty(MessageContext.REMOTE_ADDR)
I'm trying to get the network interfaces from any site or ip. For example I want to get googl's opening networks interfaces.
From example from my little code that I get the my network interfaces:
public class InterfaceLister {
public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
System.out.println(ni);
}
}
}
How can I do as like me (code of my network interfaces' scanning), but for another IP's . If it possible ?
THANKS
code is changed:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class InterfaceLister {
public static void main(String[] args) throws SocketException, UnknownHostException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
InetAddress google = InetAddress.getByName("www.google.com");
NetworkInterface anotherInterface = NetworkInterface.getByInetAddress(google);
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
System.out.println(ni);
//if (anotherInterface != null)
System.out.println("google: " + anotherInterface);
}
}
}
some outputs....
name:lo (Software Loopback Interface 1)
google: null
name:eth0 (Hyper-V Virtual Ethernet Adapter)
google: null
name:eth1 (Hyper-V Virtual Switch Extension Adapter)
google: null
name:net0 (Bluetooth Device (RFCOMM Protocol TDI))
google: null
name:wlan0 (Microsoft Hosted Network Virtual Adapter)
google: null
For the Java project i need to scan the list of ip connected to the same local network via wlan or eth0 or anything. I need to get the list of ip address that are up in the local network.
I tried
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
for (InterfaceAddress address : networkInterface.getInterfaceAddresses())
{
System.out.println(address.getNetworkPrefixLength());
}
But it gives
Exception in thread "main" java.lang.NullPointerException
at com.Server.Subnet.main(Subnet.java:17)
I think i need to follow these steps.
Get the subnet address of the network that i connected
Scan all the ip address in the subnet mask
List the ip address that are up
Can you give me the right implementation way
Follow these directions
-- get your system IP
-- get your subnet mask.
-- As per your subnet mask, get the list of possible IP addresses in your subnet.&
-- Now, one by one ping them. (you can use system ping command with java)
-- check ping response, then you can decide whether the host is up or not.
I tried this program to find all the up ip in the subnet of the system connected.
package com.Server;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class Subnet
{
public void Subnet() throws UnknownHostException, SocketException
{
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = (InetAddress) ee.nextElement();
String ip = i.getHostAddress();
String sip = ip.substring(0, ip.indexOf('.',ip.indexOf('.',ip.indexOf('.')+1) + 1) + 1);
try {
for(int it=1;it<=255;it++)
{
String ipToTest = sip+it;
boolean online = InetAddress.getByName(itToTest).isReachable(100);
if (online) {
System.out.println(ipToTest+" is online");
}
}
} catch (IOException e1) {
System.out.println(sip);
}
}
}
}
}
I am working on a java server-client app that transfers file btw each oda within a wired or wireless LAN, my problem now is how to detect the IP address of the client computer and the server computer in a wireless or wired LAN. Bottom-line: how to i use java code to detect the ip address of a computer in a wired or wireless LAN connection btw two computers.
Maybe jgroups can help you: http://www.jgroups.org
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets {
public static void main(String args[]) throws SocketException, UnknownHostException {
System.out.println(System.getProperty("os.name"));
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
if (netint.getName().equals("wlan0") || netint.getName().equals("en0")) {
displayInterfaceInformation(netint);
}
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
I need to automatically detect if a user requires a proxy to access the internet. Is there a way for a Java application to read the systems proxy setting?
Thanks,
Jimmy
Java SE 1.5 provides ProxySelector class to detect the proxy settings. If there is a Direct connection to Internet the Proxy type will be DIRECT else it will return the host and port.
Example below illustrates this functionality:
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
public class TestProxy {
public static void main(String[] args) {
try {
System.setProperty("java.net.useSystemProxies","true");
List<Proxy> l = ProxySelector.getDefault().select(
new URI("http://www.yahoo.com/"));
for (Iterator<Proxy> iter = l.iterator(); iter.hasNext(); ) {
Proxy proxy = iter.next();
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress)proxy.address();
if(addr == null) {
System.out.println("No Proxy");
} else {
System.out.println("proxy hostname : " + addr.getHostName());
System.out.println("proxy port : " + addr.getPort());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The other, accepted, answer is undoubtedly excellent and correct but I thought I would add something here...
If you are on a machine that is configured with "auto detect proxy settings", which I believe is called PAC, the code to detect the proxy in the answer using the Java gubbins will not work (it will think it is a "direct" connection).
There is a library called proxy vole (new BSD license I think), however, that you can use instead so here's the other answer's code slightly modified to use that:
public class testProxy {
public static void main(String[] args) {
try {
System.setProperty("java.net.useSystemProxies","true");
// Use proxy vole to find the default proxy
ProxySearch ps = ProxySearch.getDefaultProxySearch();
ps.setPacCacheSettings(32, 1000*60*5);
List l = ps.getProxySelector().select(
new URI("http://www.yahoo.com/"));
//... Now just do what the original did ...
for (Iterator iter = l.iterator(); iter.hasNext(); ) {
Proxy proxy = (Proxy) iter.next();
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress)
proxy.address();
if(addr == null) {
System.out.println("No Proxy");
} else {
System.out.println("proxy hostname : " +
addr.getHostName());
System.out.println("proxy port : " +
addr.getPort());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
It needs these imports:
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import com.btr.proxy.search.ProxySearch;
Oh, and there're usage examples for proxy vole here.