Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Socket IO
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 15975 of 16070
Post > Topic >>

Socket IO

by Chase Preuninger <chasepreuninger@[EMAIL PROTECTED] > Apr 15, 2008 at 03:09 PM

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




 3 Posts in Topic:
Socket IO
Chase Preuninger <chas  2008-04-15 15:09:53 
Re: Socket IO
=?ISO-8859-1?Q?Arne_Vajh=  2008-04-15 22:05:25 
Re: Socket IO
EJP <esmond.not.pitt@[  2008-04-16 11:50:52 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon May 12 2:12:19 CDT 2008.