Telnet/SSH to a Java `ServerSocket`
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 networ开发者_运维问答king 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)
精彩评论