File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Never Ending Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Never Ending Threads" Watch "Never Ending Threads" New topic
Author

Never Ending Threads

ramakrishna baga
Greenhorn

Joined: Jul 19, 2006
Posts: 4
Hi friends,
Iam new to coding, in my new project we have to connect to socket and read and write continuosly.
It is not like first read or first write , depending on the conditions it should read/write.
But both the read and write threads should be up and running continuosly.
And i should maintain only one socket object throughout the project.

Iam giving you my sample code :

//This Class connect to the Server socket and read the message
public class Connect
{
public static void main(String[] args)
{
ReadSocket rs = new ReadSocket();
WriteToSocket ws = new WriteToSocket();
try
{
Socket socket = new Socket("url",portNumber);
ws.write(socket);
rs.run(socket);//Here the thread is continuosly reading from the socket and never returning back
}
catch(IOException ie)
{
ie.printStackTrace();
}
}
}
//read thread
public class ReadSocket extends Thread
{
public void run(Socket socket){
try {

InputStream in = socket.getInputStream();

while(in.read() != -1) {
System.out.println(" Contents of the InputStream is "+in);
}
}
catch(IOException ie) {
ie.printStackTrace();
}
}
}
//write socket
public class WriteToSocket extends Thread
{
public void write(Socket socket){

try {

OutputStream os = socket.getOutputStream();
byte[] bytes = {3,0,0,1,2,3,0,0,1,1,0,9,118,111,108,118,111,53,49,53,0,26};
os.write(bytes);

}catch(IOException ie) {
ie.printStackTrace();
}
}
}

1) MY PROBLEM IS iam not able to write any more as the ReadSocket is continuosly reading the socket and it will never end and it should not.
2) What i have to do if i want to continuosly read and write to a socket.

Thanks In Advance
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Just extending "Thread" doesn't somehow magically make things run in multiple threads. You need to study this tutorial to learn how to use threads the right way.


[Jess in Action][AskingGoodQuestions]
ramakrishna baga
Greenhorn

Joined: Jul 19, 2006
Posts: 4
Thanks for your respone, i will read it
But my question is not simply that.
In My main program :
i) iam getting the socket connection object
ii) sending the username and password
iii) and reading the socket (here server socket is continuosly sending
data to my socket)
iv) and trying to send the commands to the socket.

My problem is it is not going to the iv and stucking up in the iii line itself and code is

ReadSocket rs = new ReadSocket();//seperate java classes
WriteToSocket ws = new WriteToSocket();//seperate java classes
try
{
Socket socket = new Socket("url",portNumber);
ws.write(socket);
rs.run(socket);//reading from the socket and never
ending here and not going to next line
ws.write(socket)//this is line is never running

}
///classs extended
My problem my not be completely with Threads but my be some thing else..Pls help me
Thanks
rk
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



Well, the reason that the run(socket) method is never ending is because it is reading until there is no more data. For that to happen, there has to be no more data -- meaning the socket needs to be closed by the other end.

You need to read what you are expecting, not read til there is no more data.

Henry
[ July 20, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Never Ending Threads