[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Java Help!!!!!!!

This is a program i have written and it gives a Error when i run the code.
the command IDENTIFY is used to identify my progrm with the server following certain protocol rules.
the rules in detail could also be described

communicating with the server RULE:
Length - 2 bytes - {0,sum of all the datas in bytes}.
Command Code - 2 bytes {0,20}.
Data  - n bytes
protocol version number - (2 bytes) - {2,0}.
protocol version name - (String)
{
      length  - 2 bytes {0,6}.
      String .getBytes().
}

CheckSum - 2 bytes - {(byte)sum/256 ,(byte) sum%256}

//The IDENTIFY Command

long initTime = System.currentTimeMillis();

String protocol_Name = "Remote";
//byte[] identify = {0,10,0,20,2,0,0,0, 2,30};

byte[] identify = {0,16,0,20,2,0};
                  
String PR_SName = "PRS2.0";

byte[] strarray = {0,(byte)PR_SName.length()};

//get the bytes for the string to be appended

byte[] prs = PR_SName.getBytes("UTF-8") ;

byte[] tmp1 = new byte[ strarray.length + prs.length ] ;

System.arraycopy( strarray, 0, tmp1, 0, strarray.length ) ;

System.arraycopy( prs, 0, tmp1, strarray.length, prs.length ) ;


strarray = tmp1 ;
                  
                  
//create a temp array the length we will need for the two arrays joined

byte[] tmp = new byte[ identify.length + strarray.length ] ;

//copy the original array into the start of our temp array

System.arraycopy( identify, 0, tmp, 0, identify.length ) ;

//copy the appended array into the end of our temp array
                  
System.arraycopy( prs, 0, tmp, identify.length, strarray.length ) ;

//set the identify array to point to our newly created temp array
                  
identify = tmp ;
                  
int sum = 0;
for(int i = 0; i< identify.length; i++)
{
      sum += identify[i];
}
System.out.println("Sum of all the bytes =" + sum);
                  
identify[identify.length - 2] = (byte)(sum / 256);

identify[identify.length - 1] = (byte)(sum % 256);
                  
len = identify.length;
dos = new DataOutputStream(outstream);

dos.write(identify,0,len);
dos.flush();
                  
length = instream.available();

System.out.println("Available bytes in IDENTIFY command =" + length);
dis = new DataInputStream(instream);
//BufferedReader br = new BufferedReader(new InputStreamReader(dis));
//BufferedInputStream bufferinput1 = new BufferedInputStream(instream);
                  
for (int i=1; i<=length; i++)
{
               byte response = (byte)dis.read();            
      System.out.print(response);
}
long timeTaken = System.currentTimeMillis()-initTime;
System.out.println("\n" + timeTaken + "Milli Seconds taken");
                  
try
{
      Thread.sleep(2000);
}
catch(InterruptedException e)
{
      System.err.println("Error =" + e);
}
0
ashok3sep
Asked:
ashok3sep
1 Solution
 
zzynxSoftware engineerCommented:
>> This is a program i have written and it gives a Error when i run the code.
Maybe you can tell us what error exactly? (Stack trace)
0
 
RuadRauFlessaCommented:
Question is what is the error that it gives you?
0
 
ashok3sepAuthor Commented:
IO Exception Error
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
girionisCommented:
Post the full stack trace and also the line it occurs.
0
 
zzynxSoftware engineerCommented:
The more info you give us the better we can help you.
>>IO Exception Error
At what line in your code?
Like girionis said, please provide the full stack

Other question: what is the variable "instream"?
The definition isn't available in the code you posted.
0
 
ashok3sepAuthor Commented:
Anyway guys i solved it myself

thanks for your help

With regards,

Ashok.G
0
 
zzynxSoftware engineerCommented:
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now