Check if internet is working or not in j2me - java

I am making application in j2me, i am sending data to server
but before that i want to first check that if internet is working or not (it may be from WIFI or GPRS)

Try to use getResponseCode() and getResponseMessage() methods of HttpConnection API .

There's no reliable way to determine if 'the internet is working' other than sending a request to your server. Just because you have a connection to an ISP, it doesn't mean that the ISP has a connection to the internet, or that a packet sent via that connection can reach your server.
The phone might tell you it's not connected, but if you want to know whether you can reach your server when it is connected, send a request and see what happens.

Related

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

THE CASE:
I have a TCP client what send a data and I want to hear the data from my server. So, here, everything okay.
I decided make an socket server on port 9876 to listen this device (using Java 8). The data which I receive is weird, when I print it is like: "慳慳慳", but in theory, the data coming should be a hex. This is an example of the package what the device sends (login):
40407F000431303031313132353239393837000000000000001001C1F06952FDF069529C91110000000000698300000
C0000000000036401014C00030001190A0D04121A1480D60488C5721800000000AF4944445F3231364730325F53205
6312E322E31004944445F3231364730325F482056312E322E31000000DF640D0A
I want to replicate in a easy way (one class if its possible) the connection by castel to the device (port 5086 for Traccar server app) in almost 6 methods:
Connect
Disconnect
Encode
Decode
Send package
Receive package
My goal is replicate this behavior with PHP. I tried with Websocket/HTTP protocols and the result is that I can't connect with it. I cant hear any package from the device.
So, other question could be: how I can code my own custom protocol (Castel) to connect server with these devices?
I reviewed the Traccar Server code, but its very extensive. I proved the device with it and it works, but I can't understand how (at the code level).
Documentation
Castel communication flow
Protocol package format for download upload
Login package example
I did a simple socket with PHP and this is what I listened
Traccar server includes a lot of different things, but you can just take the protocol parsing part if you want. Here's the decoder for the Castel protocol:
github.com/traccar/.../CastelProtocolDecoder.java
The decoder is based on Netty, so if you want to use it, you need to implement the rest of the server to listen to incoming connections. I would recommend checking Netty guide for more details on how to do it:
https://netty.io/wiki/user-guide-for-4.x.html

Getting IP Address of another device (Sever) in Android

I am trying to build an android application that connects two or more devices as a client/server(using socket).
But problem is in client device user need to manually put IP address of Server device to connect with server. But from the client i don't know the server IP Address. and i don't want to enter it manually.
is there any way to get IP address(programmatically) of server device that using same application and on the same network ?
Any help would be greatly appreciated.
Thank in advanced.
After trying many ways finally, I have got a solution which is
Network discovery using UDP broadcast (credit goes to this documentation)
(Thanks #Fildor for your suggestion to implement this service).
Solution
Using UDP packets and broadcasting them! This technique however is not optimal, but as long as we stay in one network this shouldn’t be a problem.
UDP packets however are fairly easy to work with.
Server implementation
Open a socket on the server that listens to the UDP requests.
Make a loop that handles the UDP requests and responses
Inside the loop, check the received UPD packet to see if it’s valid
Still inside the loop, send a response to the IP and Port of the
received packet
Client implementation
Open a socket on a random port.
Try to broadcast to the default broadcast address (255.255.255.255)
Loop over all the computer’s network interfaces and get their
broadcast addresses
Send the UDP packet inside the loop to the interface’s broadcast
address
Wait for a reply
When we have a reply, check to see if the package is valid
When it’s valid, get the package’s sender IP address; this is the
server’s IP address
CLOSE the socket! We don’t want to leave open random ports on someone
else’s computer

P2P communication , chat App (android)

i am using this tutorial as guide. i am creating an android app which is basically chat application. this uses socket programming to communicate .
i want to connect directly to device and deliver the message. the message does not go to server. server only tell me the address of the device thats it.
i followed the tutorial i mentioned above and this only works for the case where client and server are within same wifi/network. i want to connect to server from client irrespective of their network connection place. how do i do it from any network to any network.
i think i need to use port forwarding , but this is not practical . so we better use a server for keeping track of all the ip changes. and deliver the messages directly from the source to client. so how do i do it. please suggest any resources.
update
as of now what i studied is i have to use innetaddress to communicate if i am inside an wifi router.
thank you

Ping to check for connectivity to a domain

In my android application I am querying a database. I am only able to query the database if the phone is connected to the internal network, i.e: by being connected to company WiFi, or by using VPN connection.
I would like to check this connection first - maybe by trying to 'ping' the server first? Is this possible?
Thanks.
Just try to connect to the network/database/ip and handle any exception thrown b/c the network is unreachable or b/c of a time out.
You can also try InetAddress which has an isReachable() method.
You could consider using android.net.ConectivityManager if you really want to check the network status.
ConectivityManager.requestRouteToHost() can be used to ensure a server is reachable (although doubts have been raised about its reliability - mentioned in CommonsWare answer here : how to mange wifi internet connectivity in android? )
Alternatively, if you just want to check WIFI is connected, try
ConectivityManager.getNetworkInfo(ConectivityManager.TYPE_WIFI).isConnected()
Couple of caveats :
1) I'm typing this in a browser not an IDE, so it might have syntax errors,
2) You'll have to make that code much more defensive for production, and
3) All connectivity tests in ConectivityManager can be unreliable if networks are currently becoming available or unavailable.
Hope this covers what you were asking.

Using Clients IP as the Servers IP

This might be one of those "huh, why?" questions, but I figured it would be worth the try.
How would one, from a server-side application, use the clients IP address as the applications IP address to another website? The basic idea is that any work the server side application does, is seen as the client itself doing the work, and not the servers static IP.
I am not sure if changing HTTP headers would work, but I could be wrong. Is there any documentation out there on this?
Thanks,
Kyle
Utterly, utterly impossible. You won't even be able to open a TCP connection because the other website's server will try to handshake with the client, and fail.
An IP address isn't just any old ID, it's the actually address that servers will send any response to. Spoofing it basically only makes sense if you can fit your request into a single IP packet (which rules out TCP and thus HTTP) and are not interested in the response. Even then it can fail because your ISP's routers may have anti-spoofing rules that drop packets with "outside" IP addresses originating from "inside" networks.
Why on earth would a legitimate application want to spoof its IP address?
Changing HTTP headers might cut it, but most likely it won't. Depends on how naive the other server is.
It sounds like you're trying to do something the wrong way, can you give a bit more information as to what exactly the use-case is?
If there's no processing to be done in between, you can do port forwarding on your server's IP firewall, so the client connects to your server but ends up talking to the other server.
If there's more involvement of your server, then the correct thing to do would be to pass the client's IP to the other server as part of the URL (if it's a web app) or elsewhere in the data (if not) so the receiving server can know and correctly log the process without any need for fakery. Of course this would also call for a change in the other app.
Again assuming we're talking about HTTP, another idea that came to my mind would be to redirect your client to the other server. As long as all necessary data is in the URI, you could advise the client's browser to connect to the other server with a URI of your own creation that could carry whatever extra value your server's processing adds to the request.
Decades ago, the designer of internet asked, "how can we prevent Kyle Rozendo from doing such a devious thing?"
If the client is cooperating, you can install some software on client machine, and do the work from there. For example, a signed java applet on your page. [kidding]If the client is not cooperating, install some trojan virus[/kidding]

Categories

Resources