Socket Connection with Sub context of URL - java

I used below code to read sub context news from bbc site,but it is throwing UnknownHostException.Any hints please.
try {
InetAddress addr = InetAddress.getByName("bbc.co.uk/news/");
int port = 80;
// This constructor will block until the connection succeeds
Socket socket = new Socket(addr, port);
} catch (UnknownHostException e) {
System.out.println("exception is"+e);
} catch (IOException e) {
}

From the JavaDoc on InetAddress.getByName(...):
Determines the IP address of a host, given the host's name.
In bbc.co.uk/news/ the host is bbc.co.uk with news being a subcontext as you already stated.
So change that to InetAddress.getByName("bbc.co.uk") or alternatively use the URL class, if you don't have to use a Socket : new URL("http://bbc.co.uk/news/").openStream().

Your InetAddress.getByName() method is incorrect. It should specify only the host name.
InetAddress addr = InetAddress.getByName("bbc.co.uk");

Related

Multicast packet not received by MulticastSocket on any localhost client

Intro
There are a number of tutorials on creating a multicast publisher and receiver.
I used the one found here with a few modifications.
A few others:
here
here
here
note that these tutorials are all quite similar.
Details:
The server runs on port 7777 and sends datagrams to 224.0.0.0 (I tested a couple of other ip's in the multicast range: 224.0.0.0 to 239.255.255.255, but these didn't work)
Client then joins the multicast group 224.0.0.0 and waits for a packet response (run as a thread)
extra: I send a message like: 123.23.13.12[host-name]:1234 as the datagram data.
Problem:
Multicast packets from server (on localhost) not reaching client (on localhost).
Clients include a java console application (code found below) and Android application on Android Emulator. Both clients do not receive multicast packets.
I know that the multicast packets are being sent as this is shown in Wireshark
Below you will find a basic example of that which I have.
TL;DR: Server sends multicast packets (confirmed via Wireshark) but client doesn't receive them.
Suggestions are very welcome!
UPDATE
Based on Just another Java programmer's comment, I check my firewall. Lo and behold, my firewall was dropping on the input and forward chains. I set this to acceptall incoming (temporarily)
Based on Ron Maupin's comments.
I have changed the message sent to exclude the hostname, thus the message sent is 123.12.13.23:1234
I have changed the multicast send address to 239.254.0.0 which is within the specified range (see Ron's comment)
the multicast port is set to 7777
the outgoing interface is set with s.setInterface(InetAddress.getLocalHost()) in the broadcastServer() try catch block
With these changes applied, the client(s) still do not receive any packets.
Code:
Server Side (Console App):
String multicastAddress = "239.254.0.0", multicastPort = 7777;
private void broadcastServer() {
String message = null;
MulticastSocket s = null;
InetAddress local = null, group = null;
InetAddress[] allByName;
try {
local = InetAddress.getLocalHost();
s = new MulticastSocket(multicastPort);
s.setReuseAddress(true);
s.setInterface(local)
s.joinGroup(InetAddress.getByName(multicastAddress));
group = InetAddress.getByName(multicastAddress);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MulticastSocket socket = s;
// getNetworkIP() gets lan network ip
// serverport = 1025
message = local.getHostAddress() + ":" + String.valueOf(serverPort);
byte[] buf = message.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, multicastPort);
thdBroadcaster = new Thread(() -> {
while (bRunServer) {
try {
Thread.sleep(1000);
printf("[Broadcast] Broadcasting...");
socket.send(packet);
printf("OK\n");
printf("");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
socket.close();
});
thdBroadcaster.start();
}
Client Side (Console app):
String multicastAddress = "239.254.0.0", multicastPort = 7777;
private void startServerListenerThread() {
Thread thdServerListener = new Thread(new Runnable() {
#Override
public void run() {
MulticastSocket socket = null;
InetAddress group = null;
try {
socket = new MulticastSocket(multicastPort);
socket.setReuseAddress(true);
group = InetAddress.getByName(multicastAddress);
socket.joinGroup(group);
handleServerBroadcasts(socket);
socket.leaveGroup(group);
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleServerBroadcasts(final MulticastSocket socket) {
while (true){
try {
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData());
String address = received.substring(0, received.indexOf(":"));
String port = received.substring(received.indexOf(":") + 1);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception x){
x.printStackTrace();
}
}
}
});
thdServerListener.start();
}
You are calling setReuseAddress() after binding the socket. It has no effect. You need to create an unbound MulticastSocket, call setReuseAddress(), and then bind it.
The solution ended up being a trivial change.
Resolving the issue was as simple as changing:
s = new MulticastSocket(multicastPort);
to
s = new MulticastSocket();
on the server side ONLY
Note: The firewall is a requirement, check if multicast packets are allowed through. The interface I used is localhost, not a requirement though. However this can be set by getting the specified interface you need.
UPDATE
EJP's comment:
I changed the port used to 8888. This port was used to send server datagrams and for the client to connect on with their MulticastSocket

Obtain ISP IP Address Android

Started working on a network monitoring app and I was wondering if there was a way to find out the ISP assigned IP address?
I've looked into the Inet and WifiManager API's and all they seem to give you is your local network ip address. The code I tried below, just gives me my local address.
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
TextView ipaddr = (TextView)findViewById(R.id.address);
ipaddr.setText("Your IP Address is: " + ip);
You make an HTTP call to a service that tells you what your external IP.
An example of such a service is ipify.
https://api.ipify.org/
Documentation:
https://www.ipify.org
Excerpt:
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("https://api.ipify.org").openStream(), "UTF-8").useDelimiter("\\A")) {
System.out.println("My current IP address is " + s.next());
} catch (java.io.IOException e) {
e.printStackTrace();
}
Try this:
try {
URL ip = new URL("http://checkip.amazonaws.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(ip.openStream()));
System.out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}

Java Socket on Android, connection through 3G internet global ip

I have problems using Sockets on Android. I'm new to this topic so I try to achieve a simple chat, Android phone to another Android phone as a training.
I can use my code to create a ServerSocket and connect to it with another Socket from the same device ( An 'echo' example with only one client on one device works fine) I've done that also using two IP adresses on the same wifi (192.168...) but any tentative to connect a distant client using internet ip address using 3G connection fails with a
java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
What I do is, creating the ServerSocket (ServerConnect.java) :
private ServerSocket _mainServer = null;
private void initServer() {
try {
//port I use here is arbitrary: 8081
_mainServer = new ServerSocket(CONNECT_SOCKET_PORT);
} catch (IOException e) {
Log.w("ServerSocket", e.toString());
e.printStackTrace();
}
}
in the same class, in a separate thread I do this to wait for client connections :
while (running) {
GuestConnect ssc = new GuestConnect(_mainServer.accept());
ssc.setListener(this);
ssc.startConnection();
_clientSockets.add(ssc);
performGuestAdded("bra!");
}
it makes the server waiting for multiple client connection, so it can host more than two poeple in the 'chat room'.
The comunication server side is made from the local client callback and dispatch messages to all my guests (I'm still working on this part, its not really done yet but I don't think its relevant to my problem here) :
public void onMessageReceived(TBTGuestConnect sender, String message) {
for(TBTGuestConnect guestConnect : _clientSockets)
if(guestConnect != sender)
guestConnect.sendMessage(message);
}
Clients are store as 'GuestConnect' objects here is how I set them (GuestConnect.java):
public class GuestConnect {
private StringBuilder _currentMessage;
private BufferedReader _is;
private PrintStream _os;
private Socket _clientSocket = null;
private String _hostname;
private boolean _running = false;
public GuestConnect(String hostname) {
_hostname = hostname;
_currentMessage = new StringBuilder();
}
clientSocket initialisation (still GuestConnect.java):
private void initSocket() {
if(_clientSocket==null)
{
// Try to open a server socket on given port
// Here is the fail when I called it from another device
try {
_clientSocket = new Socket(_hostname, ServerConnect.CONNECT_SOCKET_PORT);
} catch (IOException e) {
Log.w("GuestSocket", e.toString());
}
}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams
try {
_is = new BufferedReader(new InputStreamReader(_clientSocket.getInputStream()));
_os = new PrintStream(_clientSocket.getOutputStream());
} catch (IOException e) {
Log.w("GuestSocket", e.toString());
}
}
again, in the same class there is the comunication part :
initSocket();
_running = true;
performConnectionStarted();
try {
while (_running) {
String received = _is.readLine();
_currentMessage.append(received);
if (received.contains(ServerConnect.CONNECT_SOCKET_MESSAGE_END)) {
String finalMsg = _currentMessage.toString().substring(0, _currentMessage.lastIndexOf(ServerConnect.CONNECT_SOCKET_MESSAGE_END));
performMessageReceived(finalMsg);
_currentMessage.setLength(0);
}
}
} catch (IOException e) {
Log.w("GuestSocket", e.toString());
performError(e);
} finally {
try {
_os.close();
_is.close();
_clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
performError(e);
}
_clientSocket = null;
}
I can then send a message from this method :
public void sendMessage(String toSend) {
if (_running)
_os.println(toSend+ServerConnect.CONNECT_SOCKET_MESSAGE_END);
else
throw new IllegalStateException("Send message : Connection isn't started !");
}
So the question is, what should I do to make this works using global internet ip addresses ?
And more generally, what it the technical difference between same wifi local ip address and internet ip adress ?
i'll start from the end because it's easier - local ip address is private, it's not valid (and not visible) out of your local network, someone out of your local network can't access it directly but you can access hosts with public ip addresses. because they are not universally visible private addresses are not unique, public ip addresses are unique and (normally) visible from any point on the internet. there's more on the subject http://en.wikipedia.org/wiki/Network_address_translation
when it comes to 3g networks take a look here Could I connect to my iphone using 3g ip? and here ServerSocket accept() not accepting connections on Android . so most probably it can't be done
Don't know if this would be of any importance to you but in our country most 3G accounts are blocked by the ISP from incoming connections. You have to apply to unblock the ports. Some ISP's won't unblock them and some will.
Found that out when I wanted to connect my DVR with a 3G modem.

can't connect Socket when not in same IP range. (subnet)

When running a ServerSocket on a machine A with ip 145.74.217.109
And then trying to connect to machine A using machine B with ip 145.74.219.103 I am unable to connect.
But when using machine C with ip 145.74.217.180 it works.
Iam not sure if this is solvable in code or its just network settings. If It is network issues is there another way to go around this problem or would I need to go away from sockets to ...?
Machine A:
public void run() {
try {
sock = new ServerSocket(Constants.PORT);
for (;;) {
Socket newsock = sock.accept();
System.out.println("Accepting new player: ");
new PaintballPlayer(newsock);
}
} catch (Exception e) {
System.out.println("IO error " + e);
}
System.out.println("End!");
try {
sock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Machine B, C :
socket = new Socket(ip, Constants.PORT);
if you look at the IP us server has IP 145.74.217.109 IP with 145.74.217.180 are able to connect while 145.74.219.103 is not being able to connect it means subnet 145.74.219.X has no access to 145.74.217.X that is why it is not being able to connect. It is network setting ask you network administrator for that. Make your both machine have same subnet mask

Establishing a connection between Java server and client within Android appEDIT

I have an app in android in which I created an android client and a Java sever.
But I'm confronting the following issue: my client (the android part) connects to the local machine on port 6000 using the android loopback address.
My server (in Java) listens on local machine at the port 6000 - but what is the IP I have to use to get the socket that accepts the clients?
InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
So what is the SERVERIP I have to use?
UPDATE:My client runns on an emulator!!!!!
EDIT:
public class ClientThread implements Runnable {
Object syncToken;
public ClientThread(Object syncToken) {
this.syncToken = syncToken;
}
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 50458);
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err
.println("Couldn't get I/O for the connection to host");
}
try {
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println(e);
}
while (true) {
synchronized (syncToken) {
try {
syncToken.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
And here is: private String serverIpAddress = "10.0.2.2";!!!!!
From http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking: if you want to communicate from within the emulator to the local host, use IP 127.0.0.1 on the local host and use IP 10.0.2.2 in Android. This should let you communicate between the Android client and the local host server.
You want to run the server part on the Android? I guess not, and in such case using loopback address is not really going to work, as loopback interface on the Android system loops back to the Android machine itself, it is not routed to the outside.
For the serverAddr, use the #InetSocketAddress(int port) constructor, it specifies the wildcard address and a specific port, meaning it listens on all the interfaces of the machine.
Edit: For best results, on the android device use the DNS name of the server to connect to it.

Categories