473,486 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

telnet session simulation - troubles

2 New Member
Hello, thanks for reading my topic...I'm just pre-beginner in java, i have been studying it for 3 weeks...So..Thats enough for prologue ;)
I have troubles with this script:
Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class Aster {
  5.  
  6.     private static String host = "172.20.20.247";
  7.     private static int port = 5038;
  8.     private static String login = "Action: Login";
  9.     private static String usname = "UserName: admin";
  10.     private static String password = "Secret: 12345";    
  11.  
  12.     private static final String CRLF = "\r\n";
  13.  
  14.     private static boolean DEBUG = true;
  15.  
  16.  
  17.     public static void main(String args[]) {
  18.          host = System.getProperty("host", host);
  19.         Integer Port = Integer.getInteger("port");
  20.         if(Port != null) port = Port.intValue();
  21.         login = System.getProperty("login", login);
  22.         usname = System.getProperty("usname", usname);
  23.         password = System.getProperty("password", password);
  24.         String debug = System.getProperty("debug", "false"); 
  25.         if("true".equalsIgnoreCase(debug)) DEBUG = true;
  26.  
  27.         Socket socket = null;
  28.         boolean error = false;
  29.         try {
  30.             socket = new Socket(host, port);
  31.             BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  32.             BufferedWriter w = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  33.  
  34.             readLines(r, 3);
  35.             writeLine(w, "login");
  36.             readLines(r, 3);
  37.             writeLine(w, usname);
  38.             readLines(r, 1);
  39.             writeLine(w, password);
  40.             readLines(r, 1);
  41.             writeLine(w, SHUTDOWN);
  42.             readLines(r, 1);
  43.         } catch(IOException ioe) {
  44.             System.out.println("I/O Error: " + ioe);
  45.             error = true;
  46.         } finally {        
  47.             if(socket != null) {
  48.                 try {
  49.                     socket.close();
  50.                 } catch(IOException ignore) {}
  51.             }
  52.         }
  53.         if(error) System.exit(1);
  54.     }
  55.  
  56.  
  57.     public static void readLines(BufferedReader r, int count) throws IOException {
  58.         for(int i = 0;i < count;i++) {
  59.             String t = r.readLine();
  60.             if(DEBUG) System.out.println(t);
  61.         }
  62.     }
  63.  
  64.  
  65.     public static void writeLine(BufferedWriter w, String out) throws IOException {
  66.         w.write(out + CRLF, 0, (out + CRLF).length());
  67.         w.flush();
  68.     }
  69. }
So..this script connects to needed host..and I see the first line from server ("Welcome")...and after this line happens nothing...Commands are not sended to OutputStreamWriter..where is my mistake?
Thanks for any suggestions and answers....
Sep 2 '08 #1
4 1972
chaarmann
785 Recognized Expert Contributor
I am nearly sure that you have not done this program by yourself (the programming style is too good for a beginner and the errors are too simple). I think this program is a homework for you and your teacher wants you to figure out the errors yourself, because if you do, he knows you have clearly understood how it works.

So i will not simply tell you the errors here (which would be contraproductive to your teachers intention and bad for yourself), but help you in understanding it.

First, use a debugger and execute the program step by step. Watch every variable value change. if you use eclipse, you already have a debugger inside.
If you don't have a debugger, then change the program so that it writes out the values for every variable after each line. For example if there is a line
Expand|Select|Wrap|Line Numbers
  1.  w.write(out + CRLF, 0, (out + CRLF).length());
in your program,
then insert 2 lines right below
Expand|Select|Wrap|Line Numbers
  1. System.out.println("out + CRLF =" + out + CRLF);
  2. System.out.println("(out + CRLF).length()" + (out + CRLF).length());
Then you will automatically detect why it does not "send".

Also ask yourself:
there is written:
readLines(r, 3);
and when it executes it, you get back "Welcome". that means getting the data from server works. But what is the meaning of number 3 here? What if you use another number? Does it always come back from this method and executes the next line?:
writeLine(w, "login");
What is this line doing?

Just figure out the answers of these simple questions by debugging as described above.
If you do, you get the solution.

Happy programming,
Chris

If you give someone water when he is thirsty and asks for it, he always comes back to you. If you are not there, he surely will die. You have set up a relationship that neither he nor you like. But if you teach him how to dig a well instead of giving him water, he will be first angry that you don't give him water right away and he has to do some work, but the thirst will let him work and when he is done he will be glad to be independent and not need to bother you ever again and will surely survive.
Sep 3 '08 #2
Nepomuk
3,112 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1.             ...
  2.             readLines(r, 3);
  3.             writeLine(w, "login");
  4.             readLines(r, 3);
  5.             writeLine(w, usname);
  6.             readLines(r, 1);
  7.             writeLine(w, password);
  8.             readLines(r, 1);
  9.             writeLine(w, SHUTDOWN);
  10.             readLines(r, 1);
  11.             ...
  12.     }
  13.  
  14.  
  15.     public static void readLines(BufferedReader r, int count) throws IOException {
  16.         for(int i = 0;i < count;i++) {
  17.             String t = r.readLine();
  18.             if(DEBUG) System.out.println(t);
  19.         }
  20.     }
  21.     ...
  22. }
So..this script connects to needed host..and I see the first line from server ("Welcome")...and after this line happens nothing...Commands are not sended to OutputStreamWriter..where is my mistake?
Thanks for any suggestions and answers....
Hi XiNoID! Welcome to bytes.com!

It's great to have you here!

When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags (as, I see, you have).

About your question:
First of all, this is not a script, it's an application. Have a look at this wikipedia article to find out, what the difference is.

Now, see that call readLines(r, 3)? What is it supposed to do? And what should the output be?

See, the BufferedReader extends the Reader class. The Reader class has a method Reader.read() which is used by the BufferedReader. And in the description of Reader.read() it says:
This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.
So, it's waiting for too further lines and won't continue, until it's received them or the Stream is closed. Can you figure it out from here?


Apart from that, I'll just wish you the best and hope you enjoy being part of bytes.com!

Greetings,
Nepomuk
Sep 3 '08 #3
XiNoID
2 New Member
Thanks for your replies! Chaarmann, you are right, that is not my code, it is modified example that I've found somewhere ..But it is not my homework :) I always wanted to begin serious programming (I had some little expirence before.,) And my choice stopped on Java :) I'm reading the book, that was printed in 2001 year i think i should buy newer one :)...Thanks for your advice, it will be very useful for me...(Sadly I didnt know
Expand|Select|Wrap|Line Numbers
  1. readLines(r, 3); 
what digit "3" means here..I will figure it out today.) Nepomuk, thank you too! i figured that
Expand|Select|Wrap|Line Numbers
  1. readLines(r, 3); 
"3" here means quantity of lines...and each line is an array that is placed in buffer... Everything works now :) I think i will go and buy new book it will make my study much easier i hope :) Anyway thanks everybody!
Sep 3 '08 #4
Nepomuk
3,112 Recognized Expert Specialist
Everything works now :) I think i will go and buy new book it will make my study much easier i hope :) Anyway thanks everybody!
I'm glad it works now. But if you're so new to programming, you may want to start with something simpler.

Check the Java Articles Index in our Java Articles Section - the Java tutorial by sun is really great and you may not even have to buy a new book. Also, there are some good books available for free as PDF files (e.g. Thinking in Java, 3rd Edition by Bruce Eckel) which you can use as further references.

Greetings,
Nepomuk
Sep 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3816
by: kumar | last post by:
Hi all, I want to write a telnet server for an embedded linux box using python.The problem is that it doesn't give a shell ( just a limited CLI commands for configuring it . )My requirement is...
3
8637
by: Yannick Turgeon | last post by:
Hello all, I'm currently trying to pass commands to a telnet session and get the texte generated (stdin + stdout) by the session. The problem I get is that the Telnet.read_until() function...
4
11124
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib,...
2
8258
by: john brown | last post by:
I'm telnetting into a router. Apart from the fact I can't seem to view the output when iniciating the session, I can't match one of the expressions using Net::Telnet. I can telnet into the router...
5
5836
by: richardtinkler | last post by:
I need to communicate with a POP3 server using ASP. Does anybody have any ideas how I can set up a telnet connection using an ASP script? There are some components out there, but my host won't...
1
2338
by: Michael | last post by:
Dear all, Given the following challenge: 1. The application is an exe on a W2K server 2. User logs via telnet to the server 3. After checking the user account and password, a bat file runs...
5
33802
by: Greg Martz | last post by:
I'd like to do the following in C# and prefer using tcpclient rather than raw sockets... Connect to a unix box Login run date +%H%M%S retrieve the response. That's it, nothing more. This...
2
4555
by: eight02645999 | last post by:
hi i am using a telnet session to simulate an authentication mechanism USER = "user" PASSWORD = "password" try: telnet = telnetlib.Telnet(HOST) telnet.set_debuglevel(5)...
2
7680
by: vmalhotra | last post by:
Hi I am new in python scripting. I want to open a Multiple telnet session through once script. In other way i can tell i want to open two linux consoles through one script. I wrote one...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.