Results 1 to 10 of 10
  1. #1
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default 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

    Java 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


    Java 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
    }
    }
    Last edited by amro; 05-28-2011 at 07:42 PM. Reason: add red color to problem line in the code

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Perhaps you should look at using Threads on the server. When the server receives a connection, create a new thread to handle that connection, pass it the socket and loop back to await a new connection.
    The server code would be a short loop to wait for connection and pass socket to new thread to handle the traffic on that socket.

  3. #3
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    hi norm
    thank you for your help

    the server is working fine because it should not listen until it got the file name and size and then wait for the file it self
    the problem is that after the client sends the file name and size using the first connection it immediately start sending the file while the server still in the first connection phase .one approach is to send some OK sign from the server telling the client to send the file content plus your idea ,but that involves more lines,threads and complexity since i wish to keep it simple for presentation purpose to demonstrate TCP packet capture for a networking assignment that is complete and i wish to refine this weekend
    I was wondering if i could just add a loop around the second connection attempt in the client side so it try again and again till the server is ready or maybe increase its timeout if possible (not an optimum solution)

    when the second Joption....inputDialouge above the red line in the client is removed the second connection will not take place because it provides the delay for the server to start listening nothing more (dummy input)

    thank you again for your response
    Last edited by amro; 05-28-2011 at 09:02 PM. Reason: spelling

  4. #4
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    is there is a way to tel the client to keep trying until it finds the server socket ?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Is there a timeout value for a socket?

    Add some printlns to the code to show what is happening.

    Also add a printStackTrace to the empty catch blocks.
    Last edited by Norm; 05-28-2011 at 09:33 PM.

  6. #6
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    no idea if there is a value or its just shoot the attempt but i remember once i was trying to scan for servers in the LAN and after specifying the ip range and put the code inside a try and catch and put all in loop it took hours to cover the range with a print statement inside the loop to see how rapid it scans

    what i need is to add looping instead of the input message break
    Last edited by amro; 05-28-2011 at 09:44 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Is there any unexpected output from the printlns and printStackTrace ?

  8. #8
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    what is the printStackTrace ? is it the output when there is exception ?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Yes. Put it in any catch block when debugging code that might have Exceptions
    I have put several printlns in your code and see that sometimes the file is being read by the server.

    You make too many assumptions about values being returned by methods.
    Print them ALL out to see where your logic is going wrong.
    Last edited by Norm; 05-28-2011 at 10:13 PM.

  10. #10
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    I commented the extra input to check it out but i was surprised by the file transfered as planed

    it seems the problem occur only some time when the packets of the first connection are delayed by the network

    for now all i got to say is sorry for wasting your time ,i will try it in the lab where there is more traffic


    thank you norm

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  2. Replies: 3
    Last Post: 07-18-2009, 02:26 PM
  3. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  4. Connecting To SQL Server
    By rmaadil in forum JDBC
    Replies: 7
    Last Post: 08-20-2008, 06:53 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •