Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cancel file transfer over sockets  RSS feed

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I made a sample file transfer program using io sockets. I indicate the file transfer using a progress bar and when I click on the cancel button, I need to cancel the file send action. i tried closing the socket but my program stops. How can I achieve the cancellation of file transfer in sockets?
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:
I made a sample file transfer program using io sockets. I indicate the file transfer using a progress bar and when I click on the cancel button, I need to cancel the file send action. i tried closing the socket but my program stops. How can I achieve the cancellation of file transfer in sockets?


Closing a socket in use should cause the other thread, that is using the socket, to throw an IOException. As for the "program stops", you will need to explain what you mean by that a bit more.

Henry
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheriff

Sorry. You are right an IOException is thrown and no other communication can be done through these sockets.
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:
Sorry. You are right an IOException is thrown and no other communication can be done through these sockets.


In terms of next steps, you probably should also modify the other thread (once it receives the exception) to confirm that a cancel is in progress, and clean up after the socket (to properly shut everything down). You probably also need to setup some sort of synchronization, to make sure that the two threads inter-operate correctly.



As a side note, I am not a fan of closing the socket that is used by another thread. A better option would be to lower the portion size of the I/O, and check for a cancellation more often. This way, the cancel is just done via synchronization between the two threads.

Henry
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheriff

I think I confused you here. I use different sockets for different threads. Let us take thread 1 which is a connection 1 from a client. This connection 1 not only does file transfer but other data transfer too. If I close the connection when file transfer is cancelled, the connection gets disconnected and as you told there is an IOException thrown.

A better option would be to lower the portion size of the I/O,


Lower the portion size of I/O? Can you please elaborate on it.

This way, the cancel is just done via synchronization between the two threads.


How can we can cancel via synchronization? I am not able to understand what you are explaining here. Kindly do elaborate on this.
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:
A better option would be to lower the portion size of the I/O,


Lower the portion size of I/O? Can you please elaborate on it.


I was recommending tighter communication between the threads. Currently, there is no communication. Your I/O thread does the whole task of transferring the file in a loop (I assume it is a loop, and not one block read). I am recommended that you do a smaller portion, and then once in a while, check to see if the other thread is requesting that the transfer be cancelled.

Instead of doing something drastic as closing another thread's socket, your thread could kindly tell the I/O thread to cancel. And your I/O thread can cancel in a cooperative manner.

Henry
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:
This way, the cancel is just done via synchronization between the two threads.


How can we can cancel via synchronization? I am not able to understand what you are explaining here. Kindly do elaborate on this.


Again, I am recommend communication between the threads, to have the threads work cooperatively, instead of the current no communication (which needs synchronizations) between the threads.

Henry
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheriff

Henry Wong wrote:Instead of doing something drastic as closing another thread's socket, your thread could kindly tell the I/O thread to cancel. And your I/O thread can cancel in a cooperative manner.


This is what my question here is. How do I ask the thread to cancel the I/O operations. I have already synchronized the threads and they have proper communications.
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:
This is what my question here is. How do I ask the thread to cancel the I/O operations. I have already synchronized the threads and they have proper communications.


You have to implement it. You I/O thread need to check for a cancellation request once in a while. This is what I meant by smaller portions. Instead of doing the whole transfer, do part of it, and then once in a while, check for a cancellation request. And if not, continue the transfer.

And of course, "proper communications" means that there is something in the communication protocol to allow one thread to request that the other thread cancels the transfer.

Henry
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheriff

Now I understand. I will try it out and definitely get back to you in case of issues Thanks for the help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!