Help needed in File transfering and socket problem

M

mimisam

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
 
K

Knute Johnson

mimisam said:
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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top