prabhu das

Greenhorn
+ Follow
since Jul 24, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by prabhu das

Which is the better one to use java.Timer or Quartz scheduler?
I want my run my main method indefinitely.
can please send me some examples
Hi all

Today i have copied the same file in my friends machine(which has same configuration as mine and same operating system i.e windows xp)and that code is working fine..
I feel that there should some problem with eclipse, what do you say?
If this is the configuration issue how we can solve this problem ?

Thanks
Yes that's what iam trying to say it will take some time to start second thread.

so it means that read thread is starting first but i dont want to do that i want to call them at the same time (not even nano seconds delay)

can i do it?
Hi Ilja,

My program really is not the example of what i require,
In my main method first iam calling read thread and then write thread, i dont want this. i want both to be started at the same time.

For example: i have a main method and two threads say A and B .
If time right now is 10 am and i should call both the threads at 10 am.
I dont want it to be delayed for even one sec.

I think i have explained in a correct way...
Thank you very much for clearing my doubt
Can you please provide me some examples to start two threads simultaneously please.
Iam unable to find the examples anywhere

Thanks In Advance
I have edited the above program , the real program is also the same as the above and but in real write message iam taking values from the dto's and sending for that reason i have given some bytes to make sure that it looks like the real program.
the bytes which iam showing is actually a acknowledgement to one of the command sent by the server socket.

My question is can i call two threads from one programm simutaneously,because in my above program first the main method will call the read class and then the write class, here iam facing problem as the read writer is not ending (and it should not because it should always ask for the data in the server).
we are using java version=1.4.2_07 under eclipse environment
I have added os.close(); and one system.out.println("");

public class MessageWriter extends Thread
{
public OutputStream os;
public void write(Socket socket){

try {
System.out.println(" In Write Message ");
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);

os.close();
}catch(IOException ie) {
ie.printStackTrace();
}
}
}
And the result is the same,it is not even printing "In Write Message".
My doubt is,as the read message function is continuosly looking for the data in the socket it seems to be blocking the whole process.

Can i call both the process at the same time ?
Please help me in this

Thanks
Here is my sample code :

My main method :

class Main {

public static void main(String ar[]) {

try {

Socket socket = new Socket("url",portNumber);

MessageReader readThread = new MessageReader(socket);
readThread.start();

MessageWriter writeThread = new MessageWriter(socket);
writeThread.start();

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

My Read Thread is :

//As Socket is not sending me end of stream iam finding the end of stream by the message length in command sent by the socket.

public class MessageReader extends Thread
{
private static int eof = -1;

private static String string;

private static int messageLength;

private static int byteRec = 0;

public Socket socket = null;

public MessageReader(Socket socket) {
this.socket = socket;
}

/**
* @param socket
* This Method is to read From the Input Stream and give the STRING to the Session Bean
*/

public static void readMessage(Socket socket)
{
log.info(" In Read Message ");
try
{
log.info(" going to read the inputstream ");

StringBuffer buffer = new StringBuffer();
StringBuffer buffer2 = new StringBuffer();
int in;

while(true)
{
in = socket.getInputStream().read();

byteRec++;

String incomingString = Integer.toHexString(in);

if(incomingString.length() < 2)
{
incomingString ="0"+incomingString;
}//end of if
buffer.append(incomingString);
string = buffer.toString();

if(byteRec == 12)
{
messageLength = //Calling the parsing function here

log.info(" Length of the Message excluding headers is " + messageLength +" Bytes ");
eof = 12 + messageLength + 1;
log.info(" Length of the Message including the headers and the checksum is "+ eof +" Bytes ");
}//end of if

if(byteRec == eof)
{
buffer = buffer2;
byteRec = 0;
log.info(" Size of the string is "+string.length());

if(13+messageLength == (string.length()/2)) {
// as we got one command complete iam sending to other class which will presist this command
}
}// end of while
}//end of try
catch (IOException e)
{
log.info(" Error reading from InputStream "+e);//
}//end of catch
}//end of else
}//end of readMessage method

public void run() {
readMessage(socket);
}//end of run method

}// end of class


// MY Write Thread is

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class MessageWriter extends Thread
{
public Socket socket = null;

public MessageWriter(Socket socket)
{
this.socket = socket;
}
public void writeMessage(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();
}
}
public void run(){
writeMessage(socket);
}
}
[ July 27, 2006: Message edited by: prabhu das ]
I know it is not working because, i have written some log's and if it send some outgoing commands the server socket would have sent me some message received command..
Hi
Iam connecting to a socket and getting a input stream and output stream and written two threads.
One thread will read from the socket and other will write to the socket.
My main program is like this :
public static void main(String ar[]) {

Socket socket = getConnection();//this method will return me a socket
object
readThread().start();
writeThread().start();
}
when i am starting readThread() the read thread is continuously fetching the data from the socket and not ending since it is looking for more data.
Actually i have to start my writeTheard simultaneosly with read thread but while readThread is not ending my program is never going to write thread.

Can any please explain me how to call two threads simultaneously

Thanks in Advance..

Prabhu
Hi everyone,
Can i read/write from/to socket simultaneously.
for example - can i write data to socket when iam reading it?