Method problems

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 6
Reputation: nonshatter is an unknown quantity at this point 
Solved Threads: 0
nonshatter nonshatter is offline Offline
Newbie Poster

Method problems

 
0
  #1
Nov 8th, 2009
Hey, can anyone tell me what's wrong with this method? I keep getting <identifier> expected error on line 1.

  1. public static void FaultHandler (DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray, rec_bytes) {
  2. int faultCount = 0;
  3. while ( faultCount < 4 ) {
  4. DatagramPacket request = new DatagramPacket(requestArray, requestArray.length, aHost, proxyPort);
  5. aSocket.send(request);
  6. try {
  7. aSocket.setSoTimeout(4000);
  8. aSocket.receive(reply);
  9. }
  10. catch ( SocketTimeoutException e ) {
  11. faultCount++;
  12. continue;
  13. }
  14. catch ( IOException e ) {
  15. break;
  16. if ( faultCount >= 3 )
  17. System.exit(0);
  18. }
  19. }
  20. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark
 
0
  #2
Nov 8th, 2009
Change:
byte[] requestArray, rec_bytes

To:
byte[] requestArray, byte[] rec_bytes

I don't think you can initialize multiple variables from a single type like that within the method header.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: nonshatter is an unknown quantity at this point 
Solved Threads: 0
nonshatter nonshatter is offline Offline
Newbie Poster
 
0
  #3
Nov 8th, 2009
Thanks, now that solves that problem, but I'm now it's throwing 14 different errors. I think they're related to the same method but can't find what the problem is!
Here is the full code:

  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class Ex2Client {
  5.  
  6. public static void FaultHandler (DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray, byte[] rec_bytes) {
  7. int faultCount = 0;
  8. while ( faultCount < 4 ) {
  9. DatagramPacket request = new DatagramPacket(requestArray, requestArray.length, aHost, proxyPort);
  10. aSocket.send(request);
  11. try {
  12. aSocket.setSoTimeout(4000);
  13. aSocket.receive(reply);
  14. }
  15. catch ( SocketTimeoutException e ) {
  16. faultCount++;
  17. continue;
  18. }
  19. catch ( IOException e ) {
  20. break;
  21. if ( faultCount >= 3 )
  22. System.exit(0);
  23. }
  24. }
  25. }
  26.  
  27. public static void Requests (DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray) {
  28. try{
  29. //CREATE REQUEST DATAGRAM PACKET
  30. DatagramPacket request = new DatagramPacket(requestArray, requestArray.length, aHost, proxyPort);
  31. //SEND PACKET
  32. aSocket.send(request);
  33. }
  34. catch (SocketException e) {}
  35. catch (IOException e) {
  36. System.out.println("IO: " + e.getMessage());
  37. }
  38. }
  39.  
  40. public static byte[] Responses(DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray) {
  41. //CREATE BUFFER ARRAY
  42. byte[] buffer = new byte[1000];
  43. // RECEIVE RESPONSE DATAGRAM PACKET
  44. DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
  45.  
  46. try{
  47. //SET TIMEOUT TO 4 SECONDS
  48. //aSocket.setSoTimeout(4000);
  49.  
  50. // RECEIVE RESPONSE
  51. aSocket.receive(reply);
  52.  
  53. }
  54. catch (SocketTimeoutException e) {
  55. System.out.println("Have not received response from the server within 4 sec: request lost or server crashed"); FaultHandler(aSocket, aHost, proxyPort, requestArray);
  56. }
  57.  
  58. catch (IOException e) {
  59. byte [] rec_bytes = reply.getData();
  60. return rec_bytes;
  61. }
  62. }
  63.  
  64.  
  65. public static void main(String args[]) throws Exception {
  66. DatagramSocket aSocket = null;
  67. try {
  68. //CREATE NEW DATAGRAM SOCKET
  69. aSocket = new DatagramSocket();
  70. //SPECIFY PORT + LOCALHOST IP
  71. int proxyPort = 3591;
  72. InetAddress aHost = InetAddress.getByName("127.0.0.1");
  73. //REQUEST ARRAYS
  74. byte[] arrayRequest1 = new byte[45];
  75. byte[] arrayRequest2 = new byte[45];
  76. byte[] arrayRequest3 = new byte[45];
  77.  
  78. // REQUEST TEXT
  79. String Request1 = "Withdrawal";
  80. String Request2 = "Interest";
  81. String Request3 = "Lodgement";
  82. //REQUEST AMOUNTS
  83. byte Num1 = (byte) Integer.parseInt ("-15");
  84. byte Num2 = (byte) Integer.parseInt ("5");
  85. byte Num3 = (byte) Integer.parseInt ("100");
  86. //PAD THE UNUSED BYTES OF ARRAY WITH SPACES (ASCII 32)
  87. for (int i = 0; i < 45; i++) {
  88. arrayRequest1[i] = 32;
  89. arrayRequest2[i] = 32;
  90. arrayRequest3[i] = 32;
  91. }
  92. //GET REQUEST TEXT AND PUT INTO BYTE ARRAYS
  93. byte [] text1 = Request1.getBytes ("US-ASCII");
  94. byte [] text2 = Request2.getBytes ("US-ASCII");
  95. byte [] text3 = Request3.getBytes ("US-ASCII");
  96. //PUTS INTEGER VALUES INTO SLOT 0 OF REQUEST ARRAYS
  97. arrayRequest1[0] = Num1;
  98. arrayRequest2[0] = Num2;
  99. arrayRequest3[0] = Num3;
  100.  
  101. for (int a = 0; a < text1.length; a++) {
  102. arrayRequest1[a+1] = text1[a];
  103. }
  104.  
  105. for (int a = 0; a < text2.length; a++) {
  106. arrayRequest2[a+1] = text2[a];
  107. }
  108.  
  109. for (int a=0; a < text3.length; a++) {
  110. arrayRequest3[a+1] = text3[a];
  111. }
  112.  
  113. // COMPOSE REQUEST 1
  114. byte[] requestArray = arrayRequest1;
  115. Requests(aSocket, aHost, proxyPort, requestArray);
  116. rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
  117. String rec_text1 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
  118. int balance1 = rec_bytes[0];
  119.  
  120. // COMPOSE REQUEST 2
  121. requestArray = arrayRequest2;
  122. Requests(aSocket, aHost, proxyPort, requestArray);
  123. rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
  124.  
  125. String rec_text2 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
  126. int balance2 = rec_bytes[0];
  127.  
  128. // COMPOSE REQUEST 3
  129. requestArray = arrayRequest3;
  130. Requests(aSocket, aHost, proxyPort, requestArray);
  131. rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
  132.  
  133. String rec_text3 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
  134. int balance3 = rec_bytes[0];
  135.  
  136. // PRINT OUT RESULTING RESPONSE AND NEW BALANCE
  137. System.out.println("Sending request: " + Request1 + " " + Num1);
  138. System.out.println("Got Response | New Balance: " + balance1);
  139. System.out.println("Sending request: " + Request2 + " " + Num2);
  140. System.out.println("Got Response | New Balance: " + balance2);
  141. System.out.println("Sending request: " + Request3 + " " + Num3);
  142. System.out.println("Got Response | New Balance: " + balance3);
  143. }
  144.  
  145. catch (SocketException e) {
  146. }
  147. // CLOSE SOCKET IF NO CONNECTION CAN BE MADE
  148. finally {
  149. if(aSocket != null) aSocket.close();
  150. }
  151. }
  152. }

and here are the compiler errors...

Ex2Client.java:21: cannot find symbol
symbol : variable reply
location: class Ex2Client
aSocket.receive(reply);
^
Ex2Client.java:63: FaultHandler(java.net.DatagramSocket,java.net.InetAddress,int,byte[],byte[]) in Ex2Client cannot be applied to (java.net.DatagramSocket,java.net.InetAddress,int,byte[])
System.out.println("Have not received response from the server within 4 sec: request lost or server crashed"); FaultHandler(aSocket, aHost, proxyPort, requestArray);
^
Ex2Client.java:124: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
^
Ex2Client.java:125: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
String rec_text1 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
^
Ex2Client.java:125: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
String rec_text1 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
^
Ex2Client.java:126: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
int balance1 = rec_bytes[0];
^
Ex2Client.java:131: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
^
Ex2Client.java:133: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
String rec_text2 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
^
Ex2Client.java:133: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
String rec_text2 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
^
Ex2Client.java:134: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
int balance2 = rec_bytes[0];
^
Ex2Client.java:139: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
^
Ex2Client.java:141: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
String rec_text3 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
^
Ex2Client.java:141: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
String rec_text3 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
^
Ex2Client.java:142: cannot find symbol
symbol : variable rec_bytes
location: class Ex2Client
int balance3 = rec_bytes[0];
^
14 errors


Please ignore the poor coding style - I'm still learning! Thanks
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 218 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC