how to make a URL conection through a seperate thread in the a client program

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

how to make a URL conection through a seperate thread in the a client program

hi
look into the program..... this is a client progrm... which will print on the screen all the characters sent from the server program..... but..... if a character '1' is received from the server it opens a thread inputthread1 and makes a URL connection to www.myportal.com. but i am getting 14 errors in the program..... will any kind heart can look into the bug and send a reply?
thanks in advance

Thanks a lot evlich........
i tried the code in the alternate way ....it s same as your suggestion..... thanks .... but ... but why the line
if (theTime.equals(the)){
InputThread1 st=new InputThread1(cs.getInputStream());
st.start();
}
is not working. I am not getting any syntax errors here.
i want to start the thread if the string 'theTime " equals 'the' string only
and i am not able to switch to the thread when the server sends the string "1".
if i modify my code like bellow it is working and the program switches to the threas st.
(theTime!=(the)){
InputThread1 st=new InputThread1(cs.getInputStream());
st.start();
}

When you compare references (any type of Object) in Java with the == or != operators, java is comparing to see whether the two objects are the same object, not whether they represent the same data. For instance, if you want to check to see if the input from the console is "DONE" then you need to do this:

Code:

String input = in.readLine();
input.equals("DONE");

If you use == here, you will always evaluate to false since the in.readLine() is constructing a String object and not using the one that was created at compile time. So when you say: "theTime!=(the)" that will probably always evaluate to false. Try
"!theTime.equals(the)"

Excellent evlich... excellent suggestion... thank you. The code works fine.
but my purpose is not fully solved. i want InputThread1 st to start when theTime.equals(the) . it doesn't work when i say the following
if ((theTime.equals(the))){
InputThread1 st=new InputThread1(cs.getInputStream());
st.start();
}

what is the modification ... kindly suggest . the complete code is as follows.
(The program basically prints the input stream is from the server , now if the server sends the "1" string, the InputThread1 st should start .... but it doesnt work ....here is the full code

sorry i could not state the problem in the above thread.
if i type 1 in the server screen ... the client must run the st thread right?. but it does not do so..
Is it because the way i compare the strings in the client program is wrong?
thanks

What exactly is the error? This code looks decently fine. There is an odd '/' at the start of the try block and the v two lines after that doesn't seem to be declared. These are all errors that your compilier should give you. As far as logic goes, you are going to have to explain what exactly is going wrong with the program.

A few notes/modifications:
1) Use BufferedReader instead of DataInputStream. e.g.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
2) Compare strings using .equals(). e.g.
"a".equals("b")

I hope these help.

PS. When posting code, it is preferable to pretty-print it in some way (indention and such, if it isn't too much trouble). Also, when you include code, please use the code tag ([\code] and [/\code], without the backslashes). Thanks.

Dera evlich
i am extremly thankfull to you for your quick reply. Now i request you a few minutes of your valuable time to glance through the server program.
let me first briefly explain the logic of the server program. The server send fixed messages to all its clients. when ever i type the string "1" in the server screen , the server program creates a thread called output thread ot and send that srting to the client( see above ). Now the client will compare the string with "1" , if true then creates a thread called InputThread1 st and makes a URL connection. The problem is the that even though the string"1" is typed in the server screen, the client is not able to run the st thread.
i request to have a gance through the server program .i am posting the full server program however it is suffucient to glance only the outputThread ot .
many many thaks again
srinivasan

here is the code of the server (and the cleint program is the same as you have given in this thread post No 3.) also there is obsolutely no syntax errors in both the programs.Only i am unab;e to resolve it logically.
The server program
import java.io.*;

((PrintWriter)v.elementAt(i)).println(" The total clinets at precent are : "+v.size()+" The motion is proposed .Let us start the Voting process.The BALLOT is opened.please vote for a motion.Rules of voting: press y if you agree with the motion or press n if you wont agree with the motion.press any other key to defer the motion");
}
}
catch(Exception e)

System.out.println("The vote is recieved from"+id1+"and it is :"+c);
System.out.println("The yes are:"+d1.size()+"And the No are:"+d2.size());
if(d1.size()>d2.size()){
System.out.println("The Motion is passed at this movement");
((PrintWriter)v.elementAt(id1)).println("The Motion is passed at this movement.");
}
else if(d1.size()<d2.size()){
System.out.println("The motion is not passed at this movement");
((PrintWriter)v.elementAt(id1)).println("The Motion is not passed at this movement.");
}
else{ System.out.println("There is tie for the motion at this movement");
((PrintWriter)v.elementAt(id1)).println("The Motion is tie at this movement.");
}

((PrintWriter)v.elementAt(id1)).println("There are some more votes to be polled,or waiting for the ballot to close");
d.add(i);

So, from what I understand, this is the code that you are having problems with. When I run it, nothing happens because I'm not running a client as well.
Some more comments:
1) Is there a reason to use so many threads? I understand that if the program has a lot of clients then it would be necessary to handle things asyncronously, but for testing it would probably be best to remove them.
2) Instead of subclassing Thread, you should probably just implement Runnable with some classes and then construct Threads around them.
3) Your InputThread and OutputThread are using different protocols for transfering data. InputThread is expecting binary encoded data which OutputThread is writing as character encoded data.

I'm attaching a small chat program that I wrote a long time ago. It is fairly simple and should work (hopefully I attached the correct version). Like I said, I wrote the program a long time ago before I really understood java. But if you take a look at the layout it might help.