how to loop client connecting attempt while server is not ready
hello everyone
i am coding a file transfer application that sends and receive file using thread for the server side .
i got some code from
Transfer a file via Socket - Real's Java How-to
after adding GUI from sun examples (FileChooserDemo)
i added extra connection to transfer the name and size of the file to be transfered
my problem is that the client attempt to connect before the server close the first connection and start listening for the second one holding the file data
then added dummy input to add some delay in the client side for the the server
to get the name and size and start listening .This worked but still what if there is congestion and the user was faster ?
my question is how to make the client keep trying to connect till the server is ready ,
thus, looping while something is not true
thank you in advance
this is the code:
client
Code:
public void connect() throws IOException{
String ip =
JOptionPane.showInputDialog( "Please enter reciever ip?" );
Socket sock1 = new Socket(ip,2001);
File myFile = fc.getSelectedFile();
DataOutputStream fileInfo = new DataOutputStream( sock1.getOutputStream());
fileInfo.writeBytes(myFile.getName()+ "," +myFile.length() +'\n' );
sock1.close();
ctrl =
JOptionPane.showInputDialog( "Please enter reciever ip?" );// only to make delay
[COLOR="red"]Socket sock = new Socket(ip,2000);// loop is needed here [/COLOR]
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
log.append("sending: " + myFile.getName() + "." + newline);
os.write(mybytearray,0,mybytearray.length);
sock.close();
}
server
Code:
public class Receiver extends Thread {
public void run(){
try {
receiver();
}
catch(Exception eio) {
}
}
public static void receiver()throws IOException {
// create socket
int filesize=0; //
int bytesRead;
int current = 0;
String[] temp;
String clientMsg;
String fileName = null;
int cntrl=1;
while (true) {
ServerSocket servsock2 = new ServerSocket(2000);
ServerSocket servsock3 = new ServerSocket(2001);
while (cntrl==1) {
Socket sock3 = servsock3.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(sock3.getInputStream()));
clientMsg = inFromClient.readLine();
temp = clientMsg.split(",");
fileName = temp[0];
filesize = Integer.parseInt(temp[1])+1;
System.out.println(filesize);
sock3.close();
servsock3.close();
cntrl = 0;
}
while (cntrl==0) {
Socket sock2 = servsock2.accept();
byte [] mybytearray = new byte [filesize];
InputStream is = sock2.getInputStream();
FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
bos.close();
sock2.close();
servsock2.close();
cntrl = 1 ;
}
}// end while true
//}//end method receiver
}
}