• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Never Ending Threads

 
ramakrishna baga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramakrishna baga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic