• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Never Ending Threads

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic