Socket IO Continued

C

Chase Preuninger

I have posted this a number of times but now I have realised that only
files like c:\myFile.dat wont work however desktop files will. How
come?
______________________________________________________
I am writing a program that transfers files over a socket, the only
problem is that when the receiving side calls the read method it
always returns -1.

//SENDING END
public void run()
{
Socket s = null;
InputStream in = null;
try
{
s = server.accept();


if(s.getInetAddress().getHostAddress().equalsIgnoreCase(validIP))
{
in = new BufferedInputStream(new
FileInputStream(from));
OutputStream out = new
BufferedOutputStream(s.getOutputStream());
for(int b = in.read(); b != -1; b = in.read())
{
out.write(b);
}
out.flush();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
s.close();
}
catch(Exception ex){}
try
{
server.close();
}
catch(Exception ex){}
try
{
in.close();
}
catch(Exception ex){}
}
}


__________________________________________________________________________


//DOWNLOADING SIDE
public void run()
{
Socket s = new Socket();
OutputStream out = null;
try
{
s.connect(addr);
ProgressMonitorInputStream in = new
ProgressMonitorInputStream(parent, "Downloading " + file,
s.getInputStream());
in.getProgressMonitor().setMaximum(1000);
out = new BufferedOutputStream(new FileOutputStream(to));
int b;
int read = 0;
while(!s.isClosed() && (b = in.read()) != -1)
{
out.write(b);
read++;
double frac = (double)read / (double)size;
frac *= 1000;
in.getProgressMonitor().setProgress((int)frac);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(s != null && s.isConnected())
{
try
{
s.close();
}
catch(Exception ex){}
}
if(out != null)
{
try
{
out.close();
}
catch(Exception ex){}
}
}
}
 
G

Gordon Beaton

I have posted this a number of times but now I have realised that only
files like c:\myFile.dat wont work however desktop files will. How
come?
______________________________________________________
I am writing a program that transfers files over a socket, the only
problem is that when the receiving side calls the read method it
always returns -1.

First, stop creating new threads. Post additional questions in the
original thread. Reply to those who are offering their advice.

Second, there is nothing in your new post to indicate that you've even
read the advice you've already been given, let alone followed any of
it.

There is nothing inherent about socket communication that cares or
even knows where the data comes from. Did you try breaking down the
problem, maybe testing if your sender can even read the files that
"don't work"? Are they large files or small ones?

Do you get any exceptions? You might consider mentioning these things,
we aren't mindreaders.

Are you sure the "validIP" test passes? What about displaying a
message either way, just to be sure.

Do you know how much data the sender thinks it has sent? Why don't you
try displaying some information while it sends the data? And similarly
for the receiver. Count the bytes at each end.

Are you closing the correct stream after sending the data? No, you've
closed the Socket itself. Close the BufferedOutputStream (not the
Socket, not the SocketOutputStream) or you will almost always lose at
least some of the data you're trying to send. If the file is small,
that might be all of the data.

This advice isn't new, it has been posted by others in response to
your previous posts but you've apparently ignored it. Read it and
follow it. If you don't understand something, reply to *this* post,
don't start yet another new thread.

/gordon

--
 

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

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top