• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending/Receiving files over Streams continuously

 
Rukshan De Silva
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm writing a small program to transfer files back and forth between 2 computers using sockets. I am using a thread class to listen to incoming streams as well so that it can be two way. My problem is, after I send the file from the sending computer using BufferedOutputStream which is received by a BufferedInputStream at the other end, I cannot receive the file at the receiver's end fully until the BufferedOutputStream is closed at the sending end, which closes the sending socket also which should not happen in the program cos I want to keep the connection available all times so that I can repeatedly send and receive files between the computers. Can anyone pls show me how I can create a while(true)loop or something of that sort so that both computers will be able to send and receive files at all times without the sockets being closed.

Any help would be greatly appreciated!!
Thankx,
Rukshan

Source code is below:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Myframe extends JFrame implements ActionListener
{
JButton ser,cli,send;
boolean isClient=false;
Socket s;

public Myframe()
{
setTitle("MY FRAME");
setSize(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);


ser = new JButton("SERVER");
cli = new JButton("CLIENT");
send = new JButton("SEND");

Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(ser);
c.add(cli);
c.add(send);

ser.addActionListener(this);
cli.addActionListener(this);
send.addActionListener(this);
}


public void getSocket(Socket s)
{
this.s=s;
}


public void actionPerformed(ActionEvent e)
{
Object ob = e.getSource();

if(ob==ser)
{
isClient=false;
Listen ls = new Listen(this);
ls.start();
}

if(ob==cli)
{
isClient=true;
Listen ls = new Listen(this);
ls.start();
}

if(ob==send)
{
try{
FileInputStream in = new FileInputStream("c:\\my.zip");
BufferedOutputStream out=new BufferedOutputStream(s.getOutputStream());
int k;

System.out.println("SENDING...");

while((k=in.read())!=-1)
out.write(k);
out.flush();
out.close();

System.out.println("FILE SENT");
}
catch(Exception e1){System.out.println("ERROR SENDING:"+e1);}
}

}//end actionPerformed


public static void main(String arg[])
{
Myframe mf = new Myframe();
mf.show();
}

}//end Class


class Listen extends Thread
{
ServerSocket ss;
Socket s;
Myframe mf;

public Listen(Myframe mf)
{
this.mf = mf;
}

public void run()
{
if(mf.isClient)
{
try{
System.out.println("CONNECTING AS CLIENT");
s = new Socket("lab2pc42.iics.ac.lk",4444);
mf.getSocket(s);
System.out.println("CONNECTED");
}catch(Exception e){System.out.println("ERROR CLIENT: " + e);}
}

if(!mf.isClient)
{
try{
System.out.println("CONNECTING AS SERVER");
ss = new ServerSocket(4444);
s = ss.accept();
mf.getSocket(s);
}catch(Exception e){System.out.println("ERROR SERVER: " + e);}
System.out.println("CONNECTED");
}

try{

BufferedInputStream in = new BufferedInputStream(s.getInputStream());
FileOutputStream out=new FileOutputStream("my.zip");
int k;


while((k=in.read())!=-1)
out.write(k);

out.close();
System.out.println("FILE RECEIVED");
}
catch(Exception e){System.out.println("ERROR RECEIVING: "+e);}
}//end run();

}//end Class
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem is in this line:

You are reading until end-of-file. When does end-of-file occur on an input stream? When the corresponding output stream gets closed. Since you don't want to close the stream you have to figure out some other way to delimit your data. Perhaps a walk through the Custom Networking part of the Java Tutorial will give you some ideas. Or read up on the File Transfer Protocol(FTP).
 
Rukshan De Silva
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thx for the reply. like u had mentioned it was the problem with the end-of-file cos. So I managed to get around the problem by reconnecting the client & server after each file...now it works okay...


Thanks again!
Rukshan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!