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

public static void FaultHandler (DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray, rec_bytes) {
	int faultCount = 0;
    while ( faultCount < 4 ) {
      DatagramPacket request = new DatagramPacket(requestArray, requestArray.length, aHost, proxyPort);	
      aSocket.send(request);	
      try {
	      aSocket.setSoTimeout(4000);
	      aSocket.receive(reply);
	  }
    catch ( SocketTimeoutException e ) {
	      faultCount++;
	      continue; 
	  }
    catch ( IOException e ) {	
	break;
	if ( faultCount >= 3 )
	System.exit(0);
     }
    }
}

Thanks

Recommended Answers

All 2 Replies

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.

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:

import java.net.*;
import java.io.*;

public class Ex2Client {

public static void FaultHandler (DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray, byte[] rec_bytes) {
	int faultCount = 0;
    while ( faultCount < 4 ) {
      DatagramPacket request = new DatagramPacket(requestArray, requestArray.length, aHost, proxyPort);	
      aSocket.send(request);	
      try {
	      aSocket.setSoTimeout(4000);
	      aSocket.receive(reply);
	  }
    catch ( SocketTimeoutException e ) {
	      faultCount++;
	      continue; 
	  }
    catch ( IOException e ) {	
	break;
	if ( faultCount >= 3 )
	System.exit(0);
     }
    }
}

public static void Requests (DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray) {	
    try{
	//CREATE REQUEST DATAGRAM PACKET
	DatagramPacket request = new DatagramPacket(requestArray, requestArray.length, aHost, proxyPort);
	//SEND PACKET	
	aSocket.send(request);			
	}
    catch (SocketException e) {}
    catch (IOException e) {
    System.out.println("IO: " + e.getMessage());
    }
}

public static byte[] Responses(DatagramSocket aSocket, InetAddress aHost, int proxyPort, byte[] requestArray) {
			//CREATE BUFFER ARRAY
			byte[] buffer = new byte[1000];
			// RECEIVE RESPONSE DATAGRAM PACKET
			DatagramPacket reply = new DatagramPacket(buffer, buffer.length);

		try{
			//SET TIMEOUT TO 4 SECONDS
			//aSocket.setSoTimeout(4000);
			
			// RECEIVE RESPONSE
			aSocket.receive(reply);	
			
		}
		catch (SocketTimeoutException e) {
           		System.out.println("Have not received response from the server within 4 sec: request lost or server crashed");				FaultHandler(aSocket, aHost, proxyPort, requestArray);
        	}
        
		catch (IOException e) {
		byte [] rec_bytes = reply.getData();
		return rec_bytes;
		}
	}


public static void main(String args[]) throws Exception { 
	DatagramSocket aSocket = null;
 	try {
		//CREATE NEW DATAGRAM SOCKET
		aSocket = new DatagramSocket();
		//SPECIFY PORT + LOCALHOST IP
		int proxyPort = 3591;
		InetAddress aHost = InetAddress.getByName("127.0.0.1");
		//REQUEST ARRAYS
		byte[] arrayRequest1 = new byte[45];
		byte[] arrayRequest2 = new byte[45];
		byte[] arrayRequest3 = new byte[45];

		// REQUEST TEXT
		String Request1 = "Withdrawal";
		String Request2 = "Interest";
		String Request3 = "Lodgement";
		//REQUEST AMOUNTS
		byte Num1 = (byte) Integer.parseInt ("-15");
		byte Num2 = (byte) Integer.parseInt ("5");
		byte Num3 = (byte) Integer.parseInt ("100");
	//PAD THE UNUSED BYTES OF ARRAY WITH SPACES (ASCII 32)
	for (int i = 0; i < 45; i++) {
		arrayRequest1[i] = 32;				
		arrayRequest2[i] = 32;
		arrayRequest3[i] = 32;
	}
		 //GET REQUEST TEXT AND PUT INTO BYTE ARRAYS
		 byte [] text1 = Request1.getBytes ("US-ASCII");
		 byte [] text2 = Request2.getBytes ("US-ASCII"); 
		 byte [] text3 = Request3.getBytes ("US-ASCII");
		//PUTS INTEGER VALUES INTO SLOT 0 OF REQUEST ARRAYS
		arrayRequest1[0] = Num1;
		arrayRequest2[0] = Num2;
		arrayRequest3[0] = Num3;

	for (int a = 0; a < text1.length; a++) {
		arrayRequest1[a+1] = text1[a];
	}

	for (int a = 0; a < text2.length; a++) {
		arrayRequest2[a+1] = text2[a];
	}

	for (int a=0; a < text3.length; a++) {
		arrayRequest3[a+1] = text3[a];
	}

		// COMPOSE REQUEST 1
		byte[] requestArray = arrayRequest1;
		 	Requests(aSocket, aHost, proxyPort, requestArray);
			rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
			String rec_text1 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
			int balance1 = rec_bytes[0];

		// COMPOSE REQUEST 2
		requestArray = arrayRequest2;
		 	Requests(aSocket, aHost, proxyPort, requestArray);
			rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
			
			String rec_text2 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
			int balance2 = rec_bytes[0];

		// COMPOSE REQUEST 3 
		requestArray = arrayRequest3;
		 	Requests(aSocket, aHost, proxyPort, requestArray);
			rec_bytes = Responses(aSocket, aHost, proxyPort, requestArray);
			
			String rec_text3 = new String(rec_bytes, 1, rec_bytes.length-1, "US-ASCII");
			int balance3 = rec_bytes[0];

		// PRINT OUT RESULTING RESPONSE AND NEW BALANCE
	 	System.out.println("Sending request: " + Request1 + "   " + Num1);
		System.out.println("Got Response   |   New Balance: " + balance1);
		System.out.println("Sending request: " + Request2 + "   " + Num2);
		System.out.println("Got Response   |   New Balance: " + balance2);
		System.out.println("Sending request: " + Request3 + "   " + Num3);
		System.out.println("Got Response   |   New Balance: " + balance3);
	}
	        
        catch (SocketException e) {
        }
        // CLOSE SOCKET IF NO CONNECTION CAN BE MADE
        finally {
            if(aSocket != null) aSocket.close();
        }	
    }
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.