java client while loop PLEASE HELP!!!
dear all, i have a very important assignment and i require help badly.
i have a program that connects to a server, the server works fine however i have a problem with some of the client code. my problem is that the client uses a function to connect and the same function to receive the code using a continuous loop that freezes my application.
package old.client;
import java.io.*;
import java.net.*;
publicclass classConnect{
BufferedReader stdIn =new BufferedReader(new InputStreamReader( System.in ) );
PrintWriter out =null;
BufferedReader in =null;
Socket socket =null;
String fromServer, fromClient;
public String connect( String ip,int port ){
String value ="";
try{
socket =new Socket( ip, port );
out =new PrintWriter( socket.getOutputStream(),true );
in =new BufferedReader(new InputStreamReader( socket.getInputStream() ) );
value = ("State: connected to " + ip +": " + port +"." );
}catch( UnknownHostException errUnknown ){
value = ("State: Unknown host: " + ip +": " + port +"." );
}catch( IOException errIO ){
value = ("State: I/O error, check server's up and port." );
}
return value;
}
publicvoid run(){
String value ="";
try{
while( ( fromServer = in.readLine() ) !=null ){
if( fromServer.equals("close." ) ){ close();break;}
if( fromServer.equals("dontpass" ) ){ value ="dontpass";break;}
if( fromServer.length() > 0 ){break;}
}
}catch( IOException errIO ){}
//return value;
}
public String send( String dataToSend ){
fromClient = stdIn.readLine();
if( fromClient !=null ){
value = ( fromClient );
out.println( fromClient );
}
}
privatevoid close(){
try{
out.close();
in.close();
stdIn.close();
socket.close();
}catch( IOException errIO ){}
}
}
i am using this as a class created from a client form class using
classClient client =new classClient
and using the connection code as follows:
client.connect( ip_address, port_number );
i can also do:
client.send( string_to_send );
what happens though is when i call the connect code it enters the while loop and the java program freezes as it will never leave the loop if you see what i mean. what i want it to do is just print the output once the server sends it i.e. System.out.println( fromServer ); however if i remove the loop it will exit the function and the connection will be lost.....i think!
p.s. this code might be a little buggy as its in development. cheers again

