Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help needed in File transfering and socket problem

Reply
Thread Tools

Help needed in File transfering and socket problem

 
 
mimisam
Guest
Posts: n/a
 
      04-30-2004
Hi,

I have an application which uses java socket to transfer file within 2
computers. Problem occurs when the network connection is lost during
file transfering(example, cable is loose).
In my client program, it is ok as it will show me an exception of
'Connection reset by peer' and exit the program. However in server
program, it simply stops at the read() line and hang forever. I just
wonder if there is a way for the server to catch any exception, or
detect the lost connection so that it can exit the program
automatically.
Please help! Thanks!!

I have simplified my code, the file transfering parts for client and
server are similar:

***client***
try{
Socket socket=new Socket(server,port)
out = new BufferedOutputStream(socket.getOutputStream());
fileIn = new BufferedInputStream(new FileInputStream(file));

int numRead;
byte[] buffer=new byte[1024];
//file transfering
while( (numRead = fileIn.read(buffer)) >=0)
{
out.write(buffer, 0, numRead);
}
}catch(Exception e){e.printStackTrace();}


***server***
server = new ServerSocket(port);
client = null;
while (true) {
try{
client = server.accept();
in = new BufferedInputStream(client.getInputStream());
fileout = new BufferedOutputStream(new
FileOutputStream(filedest));
byte[] buffer = new byte[1024];
int readMe;
// program hang in the 'while'loop during transfering,
// probably the in.read(buffer)
while( (readMe = in.read(buffer)) >= 0 ){
fileout.write(buffer, 0, readMe);
}
}catch(Exception e){e.printStackTrace();}

}//end while
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-30-2004
mimisam wrote:
> Hi,
>
> I have an application which uses java socket to transfer file within 2
> computers. Problem occurs when the network connection is lost during
> file transfering(example, cable is loose).
> In my client program, it is ok as it will show me an exception of
> 'Connection reset by peer' and exit the program. However in server
> program, it simply stops at the read() line and hang forever. I just
> wonder if there is a way for the server to catch any exception, or
> detect the lost connection so that it can exit the program
> automatically.
> Please help! Thanks!!
>
> I have simplified my code, the file transfering parts for client and
> server are similar:
>
> ***client***
> try{
> Socket socket=new Socket(server,port)
> out = new BufferedOutputStream(socket.getOutputStream());
> fileIn = new BufferedInputStream(new FileInputStream(file));
>
> int numRead;
> byte[] buffer=new byte[1024];
> //file transfering
> while( (numRead = fileIn.read(buffer)) >=0)
> {
> out.write(buffer, 0, numRead);
> }
> }catch(Exception e){e.printStackTrace();}
>
>
> ***server***
> server = new ServerSocket(port);
> client = null;
> while (true) {
> try{
> client = server.accept();
> in = new BufferedInputStream(client.getInputStream());
> fileout = new BufferedOutputStream(new
> FileOutputStream(filedest));
> byte[] buffer = new byte[1024];
> int readMe;
> // program hang in the 'while'loop during transfering,
> // probably the in.read(buffer)
> while( (readMe = in.read(buffer)) >= 0 ){
> fileout.write(buffer, 0, readMe);
> }
> }catch(Exception e){e.printStackTrace();}
>
> }//end while


It will eventually time out. You can set the timeout with
Socket.setSoTimeout() if you wish.

--

Knute Johnson
email s/nospam/knute/
Molon labe...
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-30-2004
On 30 Apr 2004 03:26:59 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (mimisam) wrote or
quoted :

>However in server
>program, it simply stops at the read() line and hang forever.


You can set a timeout on the server's read so that it will give up
after say a minute.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-30-2004
On Fri, 30 Apr 2004 18:13:05 GMT, Roedy Green
<(E-Mail Removed)> wrote or quoted :

>You can set a timeout on the server's read so that it will give up
>after say a minute.

see http://mindprod.com/jgloss/socket.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
Help needed in transfering VC++ program to WWW ranishobha21@gmail.com ASP .Net 2 01-15-2007 04:49 PM
Help needed in transfering VC++ program to WWW ranishobha21@gmail.com C++ 1 01-15-2007 01:57 PM



Advertisments