Whenever I put something within my try-catch blocks, I get this error "cannot find symbol" when I have already defined everything clearly for each try-catch blocks. None of the try catch blocks work, dunno why.
Printable View
Whenever I put something within my try-catch blocks, I get this error "cannot find symbol" when I have already defined everything clearly for each try-catch blocks. None of the try catch blocks work, dunno why.
Unless you show us the code and the full error message it's hard to say what is wrong other than maybe you haven't declared a variable/method you are using or you haven't added an import statement you need.
If this is a duplicate of your other thread then please close one of them or it's going to get really confusing.
And where's the full error message?
client1 is declared inside the try block and so is local to that block. You need to declare client1 outside the try block.
BTW I should point out you should be closing the socket in a finally block else if any of your code throws an exception the socket won't be closed.
And learn how to use code tags - read the blue text below this line to see how it is done.
I haven't looked - it's hard to read code that hasn't been formatted (hence we keep asking you to use code tags). And I don't have the time at the moment so you'll just have to run it and see if it works.
Good Luck.
Oh nvm tks, will get back 2 u if I face any more probs, tks 4 ur tym.
You are creating the TimeStamp objects but then not doing anything with them. What are you trying to achieve?Quote:
My time-stampping doesn't give any output, dunno why
Oh ya, I din realise earlier, was so caught up in fixing my exceptions. Basically, I need time-stamps to check that curr time doesn differ from the time-stamp time by more than 1 min & if it does to retransmit data. I m not sure how to reestablish connection to retransmit data, do I create a new socket for connection re-establishment? Since m implementing a simplified Kerberos protocol, m not sure how to handle the creation of challenges either. Coding on Kerberos seems to be very little on the net, in fact I can hardly find exaamples. If u have knowledge on Kerberos pls share, tks.
Just a small point on exception handling Susan, that will make coding simpler and the finished code clearer...
The try...catch structure is designed to allow you to write the bulk of your code without worrying about the errors until the end of the method. So you should open a single 'try' block, write all the method code, then catch all the errors at the end, e.g.:Sometimes you may want to handle an exception and carry on, in which case you could just nest a single try..catch in the body of the code, but the preferred way is to put that piece of code into it's own method so its try..catch is hidden from the main code. This keeps the code body clear of messy exception handling, and ensures all methods follow the same 'single try, multiple catch' formula.Code:public void aMethod {
// declare variables that may need tidying up after errors
...
try {
// initialise variables
...
// put main body of method code here, ignoring exceptions
...
...
}
// now the main work has been done, handle any problems that may have occurred
catch (ExceptionA a) {
... // handle a
}
catch (ExceptionB b) {
... // handle b
}
finally {
... // tidy up variables, close handles, etc.
}
} // end of method
It's only a guideline, but it is Best Practice ;)
Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves...
Alan Kay
I can do that but m trying to fix my socket connection now again, it says my client hasn't been intialized.
This is my errorneous code
Client:
Code:Socket client1; //Defined outside
BufferedReader userInput; //Defined outside
DataOutputStream ServerOut; //Defined outside
BufferedReader ServerInfo; //Defined outside
try {
client1 = new Socket("127.0.0.1",9001);
} catch (UnknownHostException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
Server:
Code:Socket socket1; // Defined outside
BufferedReader ClientInfo; //Defined outside
ClientInfo =
new BufferedReader(new InputStreamReader(socket1.getInputStream()));
If you get an error, please post the full error message and stack trace, if present.
Incidentally, socket1 on the server has been declared but hasn't been initialised.
Optimism is an occupational hazard of programming; feedback is the treatment...
Kent Beck
Ok, I changed my code, now since m sending strings from client to server, I want to know how I can send multiple strings to server. Eg. If client types hello then server echoes hello, then client types world server should echo world & it sholuld continue until client stops so how to send multiple strings in java?
You need the server code to loop back to waiting for the client after it handles the client message, and the client to loop round and send a new message after every server acknowledgement.
I can't say more without seeing the relevant code.
Programs must be written for people to read, and only incidentally for machines to execute...
Abelson and Sussman
I will post now.
Server:
Code:import java.io.*;
import java.net.*;
import java.util.*;
public class AS3{
public static void main(String[] args ){
int i = 1;
try{
ServerSocket s = new ServerSocket(9001);
for (;;){
Socket incoming = s.accept( );
System.out.println("Spawning " + i);
new RealEchoHandler(incoming, i).start();
i++;
}
} catch (Exception e){ System.out.println(e); }
}
}
class RealEchoHandler extends Thread{
DataInputStream in;
DataOutputStream out;
private Socket incoming;
private int counter;
public RealEchoHandler(Socket i, int c){
incoming = i;
counter = c;
}
public void run(){
try {
in = new DataInputStream(incoming.getInputStream());
out = new DataOutputStream(incoming.getOutputStream());
//boolean done = false;
String str="";
String [16] store;
out.writeUTF("Connected!\n");
out.flush();
int idx = 0;
while (idx != 17){
out.writeUTF(">");
out.flush();
str= in.readUTF();
System.out.println(in+":"+ str);
if (str != null)
{
store[idx] = str;
if(idx % 4 == 0)
{
out.writeUTF("Echo (" + counter + "): " + str+"\n");
out.flush();
}
}
idx++;
}
incoming.close();
} catch (Exception e){
System.out.println(e);
}
}
}
Client:
Code:import java.io.*;
import java.net.*;
class Client3{
public static void main(String[] args) {
String clientCar="";
String clientBranch="";
String clientDriver="";
String clientPasswd="";
DataOutputStream out;
DataInputStream in;
try {
Socket t = new Socket("127.0.0.1", 9001);
in = new DataInputStream(t.getInputStream());
out = new DataOutputStream(t.getOutputStream());
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
boolean more = true;
System.out.println(in.readUTF());
while (more) {
clientCar = in.readUTF();
clientBranch = in.readUTF();
clientDriver = in.readUTF();
clientPasswd = in.readUTF();
if ((clientCar == null) || (clientBranch == null) || (clientDriver == null) || (clientPasswd == null))
more = false;
else
out.writeUTF(clientCar+"\n");
out.flush();
out.writeUTF(clientBranch+"\n");
out.flush();
out.writeUTF(clientDriver+"\n");
out.flush();
out.writeUTF(clientPasswd+"\n");
out.flush();
}
} catch(IOException e){
System.out.println("Error" + e);
}
}
}
2nd page and still no error message.
You've been asked many times - either post the error message or nobody will be able to help you.
M so sorry, my software is spoilt so m unable to test my code hence I can't post error messages yet. Waiting for a frnd to cum online so I can test on her compiler. Reinstalling my compiler has failed many times. Now, m partly running out of time & have yet to find java code for symmetric key block cipher which works, a working md5 algorithm & do string comparison successfully. Hence m asking some1 to see if my code is alright. I can't find working NTLM authentication java code either. So many things & m still @ a trial & error phase.