Does anyone know of a simple telnet server?
I want to embed in my application and set my own commands
something simple not complex .
Try this one,
http://code.google.com/p/telnetd-x/
A telnetd alone is useless. You have to connect it to a shell. We use jacl and jyson as shell.
I have written a simple Telnet server in Java: TelnetStdioRedirector
I did as below
public static void main(String[] args) {
String url = "hostname";
int port = 8080;
try (Socket pingSocket = new Socket(url, port)) {
try (PrintWriter out = new PrintWriter(pingSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));) {
out.println("ping");
System.out.println("Telnet Success: " + in.readLine());
}
} catch (IOException e) {
System.out.println("Telnet Fail: " + e.getMessage());
}
}
Related
My application connects to hardware which is basically a plotter machine which prints data based on the command. I am doing socket connection and firing some commands. In the hardware I can see command has been reached but I am not able to get return value of executed command. Below is the sample code. There is no error but at the execution on this line String cmd = in.readLine(); it got stuck.
Main.Java
public class Test
{
public static void main(String[] args)
{
try
{
PrintTest printTest = new PrintTest();
printTest.print();
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
Method
public void print() throws IOException
{
Socket socket = null;
try
{
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 10);
System.out.println("isConnected :- " + socket.isConnected());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("TC1004,1;");
out.flush();
String cmd = in.readLine();
System.out.println("Recieved: " + cmd);
}
catch (Throwable ex)
{
ex.printStackTrace();
}
finally
{
if (socket != null)
{
socket.close();
}
}
}
}
readLine(); will read (or block) until it encounters a line feed, so that's why you're getting stuck. However you didn't use a line feed in the command you sent either, which is suspicious since there must be some agreed form in the protocol to be able to tell commands apart (flush() is not enough).
I suspect the plotter is still waiting for you to finish your command with a \nbefore you can read anything it returns.
I'm using Ganymed to run OS commands from JAVA. In Linux everything works like a charm. The problem is with Windows. I get the error: There was a problem while connecting to [IP]:[port]. I've tried to connect through localhost/router ip/internet ip and port 22/1023 and I've opened the ports on windows firewall and on the router as well.
I'm guessing the problem is that there isnt anything that listen the port like ssh in Linux. Am I right?
what do I need to do to fix that?
BTW I've looked on JSCH lib but Ganymed is much more simpler
Here's my sample code:
public class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String hostname = "192.168.xxx.xxx", username = "xxx", password = "xxx";
int port = 1023;
try {
Connection conn = new Connection(hostname,port);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed.");
}
Session sess = conn.openSession();
sess.execCommand("ver");
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
conn.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
}
You should install open ssh on windows to keep the ssh server running, which would act as a listener interface for inbound connections
Hello I am trying to send an object to a server, and then after the server has received it, taking that same object from the server and reading it as a String in the client output. My initial sent message seems to work while everything after that isnt, here is my code:
import java.io.*;
import java.net.*;
public class GUGLi {
static Socket socket = null;
static ObjectOutputStream out = null;
static ObjectInputStream in = null;
String host = "host";
public static void main(String[] args) throws IOException {
try {
OpenPort();
InfoSent();
ReadInfo();
String line;
while ((line = in.toString()) != null) {
System.out.println(line);
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + "host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + "host");
System.exit(1);
in.close();
socket.close();
out.close();
}
}
public static void OpenPort() throws UnknownHostException, IOException{
socket = new Socket ("host", 7879);
}
public static void InfoSent()throws IOException {
Student info = new Student (22, "Guglielmo", "Male",
"email", "#");
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(info);
System.out.println("Sent: " + info);
out.flush();
}
public static void ReadInfo()throws IOException {
in = new ObjectInputStream(socket.getInputStream());
}
}
line = in.toString() doesn't read lines. It turns an InputStream into its string representation. You need to invoke a read method. If you are hoping to read lines you need BufferedInputStream.readLine(). But if you're reading objects over the same socket you can't mix stream/reader types, so you should read with readUTF() and write with writeUTF() at the other end.
The first problem you have is you're not calling a reading method as sais by #EJP, so see his answer to have more details.
EDIT: I've deleted the information about not being able to receive data in your InputStream. I was wrong about thinking that your socket was not connected to another client, cause i was thinking that String host = "host" would connect to the loopback address, and I was not seeing any code to send data back to your InputStream.
I suggest that you read some tutorials to clear that out. I suggest you Java tutorial about socket to have a strong basic. It will explain everything you need.
So I wrote a simple Socket program that send message from Client to Server program and wanted to know what is the proper procedure to go about testing this? Both my Client and Server machines are running on Ubuntu 12.04 and I'm remote connecting to both of them.
For my Client code when I instantiate the client socket (testSocket) do I use its IP Address and Port number or Servers IP Address and Port number?
Here is the Code for Client:
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket testSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
try
{
testSocket = new Socket("192.168.0.104", 5932);
os = new DataOutputStream(testSocket.getOutputStream());
is = new DataInputStream(testSocket.getInputStream());
}
catch (UnknownHostException e)
{
System.err.println("Couldn't find Host");
}
catch (IOException e)
{
System.err.println("Couldn't get I/O connection");
}
if (testSocket != null && os != null && is != null)
{
try
{
os.writeBytes("Hello Server!\n");
os.close();
is.close();
testSocket.close();
}
catch (UnknownHostException e)
{
System.err.println("Host not found");
}
catch (IOException e)
{
System.err.println("I/O Error");
}
}
}
Here is the code for Server:
public static void main(String[] args)
{
String line = new String() ;
try
{
ServerSocket echoServer = new ServerSocket(5932);
Socket clientSocket = echoServer.accept();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
PrintStream os = new PrintStream(clientSocket.getOutputStream());
while (true)
{
line = is.readLine();
os.println(line);
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
I'm new to Sockets and not sure what I'm supposed be seeing. I compiled both programs in terminal fine but not sure which one should I be running first or do they need to be started simultaneously?
Thanks
Your server is running in a infinite loop. Avoid that.
You have to restart your computer.
while (true)
{
line = is.readLine();
os.println(line);
}
try
while (!line.equals("Hello Server!"))
{
line = is.readLine();
os.println(line);
}
Run the server first. echoServer.accept(); waits for a connection. When it gets the first connection,
http://docs.oracle.com/javase/tutorial/networking/sockets/ this is a short java tutorial on how to work with sockets and also you can learn how to make a server that would accept multiple connections at a time. This tutorial explains you always need to start the server first, which is only logical. You should use threads to manage connections and then close them so that you use resources efficiently
I'm trying to test a scenario where one server accepts connections(one each time) from one client, using always the same ports (on the server and on the client side).
The purpose is to have 1 client application sending little pieces of data at a rate bigger than 100/min. The well obvious solution would be to have an always connected link between the client and the server, but this is production stuff, and that would require bigger changes in the code that is already implemented. With the solution we have implemented today, we always have +-1K of connections in TIME_WAIT, and I want to get rid of them.
I have implemented a simple tester, and the code is:
public class Server {
public static void main(String[] args) {
ServerSocket ssock = null;
try {
ssock = new ServerSocket();
ssock.bind(new InetSocketAddress(Common.SERVER_PORT));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
while(true){
try{
Socket cSock = ssock.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(cSock.getInputStream()));
reader.readLine();
PrintWriter writer = new PrintWriter(cSock.getOutputStream());
writer.println(Common.SERVER_SEND);
writer.flush();
reader.close();
writer.close();
cSock.close();
}catch (Exception e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}
}
public class Client {
public static void main(String[] args) throws Exception {
InetSocketAddress cliAddr = new InetSocketAddress(
InetAddress.getByName(args[0]),
Common.CLIENT_PORT);
InetSocketAddress srvAddr = new InetSocketAddress(
InetAddress.getByName(args[1]),
Common.SERVER_PORT);
for(int j=1;j<=50;j++){
Socket sock = null;
try{
sock = new Socket();
sock.setReuseAddress(true);
sock.bind(cliAddr);
sock.connect(srvAddr);
PrintWriter writer =
new PrintWriter(
sock.getOutputStream());
writer.println(Common.CLIENT_SEND);
writer.flush();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
sock.getInputStream()));
reader.readLine();
}catch (Exception e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(-1);
}finally{
if(sock!=null) sock.close();
System.out.println("Done " + j);
}
}
}
}
public class Common {
public static final int SERVER_PORT = 9009;
public static final int CLIENT_PORT = 9010;
public static final String CLIENT_SEND = "Message";
public static final String SERVER_SEND = "OK";
}
When executing the client and server, on windows hosts, in one client execution I always get
java.net.ConnectException: Connection timed out
When executing the client and the server in linux hosts, on some client executions I get a
java.net.NoRouteToHostException: Cannot assign requested address
I've been killing my head over this behavior. Can someone please tell me if it is possible to do what I want, and what I am doing wrong?
If you want to get rid of the TIME_WAIT state, don't be the peer that receives the close. Be the peer that initiates the close. In this case, close the connection immediately after reading the response, and have the server cycle around looking for another request so that it reads the EOF rather than just closing the connection immediately after sending the response. However this will only make the problem worse, as all the TIME_WAIT states will accumulate at the server rather than at the client. On the other hand, the server is now structured to accept multiple requests per connection, so then all you have to do is adapt the clients to use a connection pool and all your problems are solved.