Telnet/SSH to a Java `ServerSocket` - android

I created a really simple Java program that creates a java.net.ServerSocket on an open port, x, and waits for a connection using accept (). I want to use telnet/ssh to connect to the program via port x so that I can communicate with said program. My problem is after I connect to the port, my program recognizes and accepts the connection, but ssh freezes. I'm assuming this is because I don't have the proper response. I just wanna know what I should be doing next. How do I respond?
I could use a library, but I'd like to understand what should come next. However, I would still really appreciate someone telling me if I really should be using a library. Also, I know some basic networking concepts like TCP/IP, OSI, and that data gets wrapped, sent, then unwrapped, but that's the extent of my networking knowledge.

SSH is a very specific protocol, involving encryption. Do you not just want to telnet to the socket, which uses plain-text by default?

ssh is probably much too complicated for what you want to achieve. It freezes because it waits for a response from your server that it probably never gets. ssh has also complicated security requirements that you probably don't want to implement for your simple server.
Why don't you start with something very basic on the client side as in this tutorial. From there on, you can still add features and functionality as needed.
Edit:
Sending commands with telnet:
Client Side:
Just issue telnet host port, there you type say Hello World and hit Enter.
Server side:
You receive a stream of bytes from your client. First you have to parse it as a String. Then you could simply split this String by looking for the first whitespace character. The string before that is your 'command', the part after the whitespace is your 'payload'.
In the example this would give you 'say' as the command and 'Hello World' as the payload.
Then compare the 'command' with a list of known commands, and based on what command you have you can then execute it with the payload as an argument.

Unless you are an genius at cryptography and networks, I doubt you can implement an SSH server. I don't know what your exact requirements are but you may take a look at this: http://www.sshtools.com/en/maverick-sshd/ It's a sshd in JAVA(but I haven't used it personally, so can't tell you much)

Related

Interface between Raspberry Pi and Android

I am trying to do some wacky home automation which will require me to send a signal from my Android phone to my Pi 3 in order to execute a script to control a motor using the GPIO pins.
The only part I'm stumped on is the best way to connect the Android and Pi.
I've read so many different things and it's all overwhelming, the amount of differing answers I've seen.
So far I'm leaning towards using Jsch in my app to ssh into the Pi and execute the command, but I have been told this is silly.
Can anybody explain to me why this is a bad idea and explain to me a better one? Ideally the phone app would be able to connect over both local network and other networks.
Nothing is wrong with SSH, but people typically use web servers on the Pi plus HTTP requests on the Android side. Or you can run your own protocol via a raw socket connection.
SSH commands might be more secure if you use SSH keys.
Otherwise, you'd be exposing your commands to anyone snooping on your internet traffic, and random people will be controlling your devices
Either way, if you want access both internal and external to home, you can do more research to see if you can "port forward" your router
The advantage of using HTTP for this sort of thing is that it potentially creates its own user interface. What I mean by this is that, if the interface on the Pi is a Web server, then you have a way to provide an HTML/JavaScript interface to your Android device, and thus avoid the need to create an Android app at all -- the user just needs a browser. The whole user interface is managed from the Pi.
I've used this approach for motor control on the Pi a fair bit. On the Pi I use a C program that embeds the libmicrohttpd webserver engine. The program can serve out ordinary HTML pages to create the user interface on the browser, or respond to particular HTTP GET requests that result from the user clicking buttons or manipulating sliders or whatever. You can do some really sophisticated stuff by sending JavaScript functions that make their HTTP requests outside the normal HTTP request/response flow, so you can (for example) have a browser display that updates dynamically (e.g., display sensor values from the Pi).
Moreover, it's easy-ish to provide some kind of security using SSL and HTTP authentication. I prefer C, but there are webserver libraries for Python that work on the Pi as well.
To my way of thinking, the only time it's worth considering something more complex than this is when you need a user interface on Android that can't be implemented in HTML/JavaScript.
I'm sure there are many different ways to do what you want. I prefer to do most of the work on the Pi, because I find writing Android apps deeply unrewarding. On the other hand, if you like developing for Android and have plenty of experience doing so, the approach you suggested -- sending commands to the Pi over SSH -- could work perfectly well. It would just mean doing most of the work in Android.
Yes, ssh is silly solution. I suggest to develop rest api webservice, host it on your PI and invoke it from your Android app

send data from phone to PC via WIFI

In my application, I want to send some data(say, "Hello") from my app to my PC over WIFI network. In PC, a service will listen to that data. But i don't have any idea of sending data over WIFI. I didn't get any proper references too. can someone help me.
Thanks in advance.
What you need is a propper protocol which both, your server and your phone can easily deal with.
If you don't send sensitive data, I think easiest would be HTTP
This depends on what you want the service on the PC to do with the received data, really. For all I know, you might find some off-the-shelf software that can help you set up this service. However, as others mention here, http is definetely the easiest way to do this. If you cannot find any existing software for the "listening pc", you'll need to write a program that listens at a predetermined port for http traffic (I wouldn't use the standard http port, 80, since you might run into conflicts with existing http software). You can do this quite easily in java, using Serversocket and/or Socket (as Tobias Rizau mentions above). Personally, I'd write a C/C++ program, since imo. that makes a better service, but that's absolutely not necessary (especially if you only know Java).

ubuntu on android unable to open display chroot

hello
I am using ubuntu on android(with chroot)
I am trying to execute xev
(to change the volume button to CTRL)
but every time I trying to open up xev,
it says
unable to open the display ''
or
unable to open the display '127.0.0.1:0.0'
(when I change DISPLAY variable with export)
could somebody tell me how to solve this problem?
I can't find official error page on google
your little advice would be very helpful for me
Regards.
Taeyun.
DISPLAY=127.0.0.1:0.0 says to use TCP/IP to connect to the display 0.0. Distributions haven't configured X servers to listen on TCP for five years or more because it is a needless security risk; you need to remove -nolisten from however you execute your X, if you really wish to use TCP rather than Unix domain sockets.
But do you really need it to listen on TCP for connections? Seems unlikely -- Unix domain sockets (e.g., /tmp/.X11-unix/X0) are much more common, just use DISPLAY=:0.0 to ask for the Unix domain socket instead. You may need to link the socket into your chroot environment, depends on where client and server start from.
Once you can contact the X server from your X client, you also need to authenticate it, using xauth(1). This is annoying :) and is the single most popular reason why everyone just uses SSH X11 forwarding (ssh -X) rather than deal with TCP listening and xauth manually. (Incidentally, can you just run an sshd "inside" the chroot on a different port, so you can easily get all this magic for free? :)

How to create an Android RFCOMM socket without any input from the user?

Here's the scenario:
I need to write an application for Android to create a RFCOMM socket to a PC with a Bluetooth dongle (I'm going to write the server too).
My requirement is that the user doesn't have to pair device manually.
Actually, with a big hack, I'm using the createInsecureRfcommSocket.
A little scenario: I've an Android application which exchanges information with a Linux box with a RFCOMM socket opened. I can manually set the PIN on the devices (hard coding it IS an option)
I'm looking for different roads:
Write a JNI wrapper
As has been done here by Max Kellermann, I can write a JNI layer to make all the pairing phase. This should be a good option, but there's a problem:
The NDK 4b does not provide libbluetooth libraries, so -lbluetooth fails, as well as the NDK v.3.
Options:
Find the NDK 1.5 (which includes lib bluetooth). After days of web search I've been not able to find out. Anyone has or knows where I can find it?
Compile libbluetooth for Android by myself and use them for -lbluetooth. No lucky there, I'm not able to build them. Any hint?
Use something exposed by the APIs
Any one know how can I use createRfcommSocketToServiceRecord and have the user not to manually pair the device? Is it possible? How should I write the server?
Something I do not know
Maybe (sure!) there is something I do not know. Maybe I can use something else? Not RFCOMM? SDP?
Maybe I can manually pair with the Android API?
I hope to have been clear enough, if not just ask. And again, as it's not the first time, I'm in your hands :)
Thanks for all the support guys!
At the end, I can say that you can't.
Even if you find a way, using wrappers, writing C modules and so on, android evolution will probably change the thing you're using.
And so, there's no option. Sadly.
Let's see how Android Bluetooth API will change in the future.
Yes we can create it Rfcomsocket and listen the socket without user concerns and also without pairing devices.
https://code.tutsplus.com/tutorials/create-a-bluetooth-scanner-with-androids-bluetooth-api--cms-24084
Follow this !!
The Android Bluetooth API is not finalized, and will change. Use at your own risk. This class implements an API to the Bluetooth RFCOMM layer. An RFCOMM socket is similar to a normal socket in that it takes an address and a port number. The difference is of course that the address is a Bluetooth-device address, and the port number is an RFCOMM channel. The API allows for the establishment of listening sockets via methods bind, listen, and accept, as well as for the making of outgoing connections with connect, connectAsync, and waitForAsyncConnect. After constructing a socket, you need to create it and then destroy it when you are done using it. Both create and accept return a FileDescriptor for the actual data. Alternatively, you may call getInputStream and getOutputStream to retrieve the respective streams without going through the FileDescriptor.

Sending a Java object from my Android phone to my Computer

I was wondering what the simplest program for sending an object from my Android phone to my computer wirelessly (via LAN) would be. I have created Java RMI programs with a server and multiple clients, so I have a grasp of the concept. However with android I'm just not sure where to start.
What I am aiming to do is send some sort of information (could simply be text) to my computer and my computer will do an action. I have the GUI interface's and the actions to be carried out all worked out, just the sending of some sort of information is getting me.
Could anyone help me out?
I would say it's not much different from sending data between regular computers. Basically you have the same options. Unless you have some special requirements, the most straight forward solution would be to just set up a ordinary server / socket.
A tutorial on the basics (including example code): http://www.ryerson.ca/~dgrimsha/courses/cps841/serverSockets.html

Categories

Resources