Results 1 to 10 of 10
- 05-28-2011, 06:18 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
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 06:42 PM. Reason: add red color to problem line in the code
- 05-28-2011, 07:04 PM #2
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.
- 05-28-2011, 08:00 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
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 responseLast edited by amro; 05-28-2011 at 08:02 PM. Reason: spelling
- 05-28-2011, 08:11 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
is there is a way to tel the client to keep trying until it finds the server socket ?
- 05-28-2011, 08:16 PM #5
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 08:33 PM.
- 05-28-2011, 08:39 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
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 breakLast edited by amro; 05-28-2011 at 08:44 PM.
- 05-28-2011, 08:40 PM #7
Is there any unexpected output from the printlns and printStackTrace ?
- 05-28-2011, 08:52 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
what is the printStackTrace ? is it the output when there is exception ?
- 05-28-2011, 08:55 PM #9
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 09:13 PM.
- 05-28-2011, 09:13 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
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
-
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 11:31 AM -
[SOLVED] Ready to Program Java IDE Problem #3 PacMan For Loop to Slide Across the Scr
By Starr29 in forum New To JavaReplies: 3Last Post: 07-18-2009, 01:26 PM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Connecting To SQL Server
By rmaadil in forum JDBCReplies: 7Last Post: 08-20-2008, 05:53 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks