Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Keep Socket alive while send file

Iam sending a image from my android to pc ,its part of my app, Iam using sockets for this The sending part code includes

public void sendfile()
{
    try {           
    System.out.println("looppppppp");
    File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
    byte [] mybytearray  = new byte [(int)file.length()];  
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    bis.read(mybytearray,0,mybytearray.length);         
    System.out.println("Send:"+mybytearray.length);
    bis.close();
    OutputStream ous = socket.getOutputStream();
    System.out.println("Sending...");
    ous.write(mybytearray,0,mybytearray.length);
    ous.flush();
    //ous.close();  
       // socket.close();
    System.out.println("send overrrrrrrrrrr");
    }catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

The Socket is connected in a thread when the program starts. The receiver is a java code in pc as follows

@Override
public void run() 
{
try
{
ServerSocket servsocket = new ServerSocket(13267);
System.out.println("Thread Waiting...");
Socket socket = servsocket.accept();
System.out.println("Accepted connection : " + socket);
System.out.println("Connected..");
while(true)
{
// filesize temporary hardcoded
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
mybytearray  = new byte [filesize];
File f=new File("d:\\ab.jpg");
f.createNewFile();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
System.out.println("b4");
bytesRead = is.read(mybytearray,0,mybytearray.length);
System.out.println("after");
current = bytesRead;
do {
   bytesRead =
   is.read(mybytearray, current, (mybytearray.length-current));
   if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
fos.close();
}
}catch(IOException e)
{
System.out.println("errorr");
}
}
}

The problem is File Does'nt appear on my pc Unless i close my output stream or socket If i close output stream the socket is getting closed why this?

Upvotes: 0

Views: 752

Answers (2)

user207421
user207421

Reputation: 310860

Keep Socket alive while send file

Sending the file keeps the socket alive. I don't see the relevance of your title to your question.

The problem is File Does'nt appear on my pc Unless i close my output stream or socket

So close it. Closing the socket at the sender causes the receiver to exit your receive loop, which has no other way of exiting. Your stream copy loops are far more elaborate than necessary. It is neither necessary nor advisable to buffer entire files in memory before or after sending.

If i close output stream the socket is getting closed why this?

Because that's what it's specified to do. Closing the input or output stream of a socket closes the other stream and the socket.

Upvotes: 1

hamilton.lima
hamilton.lima

Reputation: 1920

If I understand correct you want to keep the socket open with the client, and also send files ...

My suggestion is : - keep one main thread open to notify the server about new files - open new threads to send each new file - add code to control the maximum number of files that you can send at the same time (optional)

So the flow would be :

1. client open main socket
2. server open the main socket and assign a client id 
3. client request the send of a new file
4. server keep in memory the file name and client id
5. server send response authorizing the client to send the file
6. client open a new thread to send the file

Upvotes: 0

Related Questions