Socket IO

C

Chase Preuninger

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){}
}
}
}
 
A

Arne Vajhøj

Chase said:
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.

A few things seems "unusual" to me:

1) The order you are closing streams and sockets in - I would
close streams before socket.

2) Maybe a flush when writing would be a good idea.

3) Single byte read and write are very inefficient (even when
buffered).

Arne
 
E

EJP

Chase said:
if(s.getInetAddress().getHostAddress().equalsIgnoreCase(validIP))

I suggest that this test is failing, so you are writing nothing and
immediately closing the socket, hence the immediate read() returning -1
at the receiver. I would print a trace if this test fails, as surely you
want to know about invalid source addresses, as well as debug this problem.
s.close();

Wrong. You should always close the outermost output stream of the
socket, and closing a socket or its output stream or its input stream
closes the other two.
while(!s.isClosed() && (b = in.read()) != -1)

The s.isClosed() test here is pointless, unless you have another thread
that might close the socket. It won't tell you when you have reached
EOF. read() returning -1 tells you that.
s.close();

See above. Close the output stream only.
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top