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: -
import java.net.*;
-
import java.io.*;
-
-
public class Aster {
-
-
private static String host = "172.20.20.247";
-
private static int port = 5038;
-
private static String login = "Action: Login";
-
private static String usname = "UserName: admin";
-
private static String password = "Secret: 12345";
-
-
private static final String CRLF = "\r\n";
-
-
private static boolean DEBUG = true;
-
-
-
public static void main(String args[]) {
-
host = System.getProperty("host", host);
-
Integer Port = Integer.getInteger("port");
-
if(Port != null) port = Port.intValue();
-
login = System.getProperty("login", login);
-
usname = System.getProperty("usname", usname);
-
password = System.getProperty("password", password);
-
String debug = System.getProperty("debug", "false");
-
if("true".equalsIgnoreCase(debug)) DEBUG = true;
-
-
Socket socket = null;
-
boolean error = false;
-
try {
-
socket = new Socket(host, port);
-
BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
-
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
-
-
readLines(r, 3);
-
writeLine(w, "login");
-
readLines(r, 3);
-
writeLine(w, usname);
-
readLines(r, 1);
-
writeLine(w, password);
-
readLines(r, 1);
-
writeLine(w, SHUTDOWN);
-
readLines(r, 1);
-
} catch(IOException ioe) {
-
System.out.println("I/O Error: " + ioe);
-
error = true;
-
} finally {
-
if(socket != null) {
-
try {
-
socket.close();
-
} catch(IOException ignore) {}
-
}
-
}
-
if(error) System.exit(1);
-
}
-
-
-
public static void readLines(BufferedReader r, int count) throws IOException {
-
for(int i = 0;i < count;i++) {
-
String t = r.readLine();
-
if(DEBUG) System.out.println(t);
-
}
-
}
-
-
-
public static void writeLine(BufferedWriter w, String out) throws IOException {
-
w.write(out + CRLF, 0, (out + CRLF).length());
-
w.flush();
-
}
-
}
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....
4 1972
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 - w.write(out + CRLF, 0, (out + CRLF).length());
in your program,
then insert 2 lines right below - System.out.println("out + CRLF =" + out + CRLF);
-
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.
Nepomuk 3,112
Recognized Expert Specialist -
...
-
readLines(r, 3);
-
writeLine(w, "login");
-
readLines(r, 3);
-
writeLine(w, usname);
-
readLines(r, 1);
-
writeLine(w, password);
-
readLines(r, 1);
-
writeLine(w, SHUTDOWN);
-
readLines(r, 1);
-
...
-
}
-
-
-
public static void readLines(BufferedReader r, int count) throws IOException {
-
for(int i = 0;i < count;i++) {
-
String t = r.readLine();
-
if(DEBUG) System.out.println(t);
-
}
-
}
-
...
-
}
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
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
what digit "3" means here..I will figure it out today.) Nepomuk, thank you too! i figured that
"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!
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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,...
|
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...
|
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...
| |
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...
|
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...
|
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)...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |