0

I am writing a code to transfer a large file over the internet. Therefore, I have to open a socket over a specific port (port = 45678) for sending the data. However, it takes a while that the operating system (Ubuntu 14.04) release that specific port (45678) after the program termination.

I want to run my code for multiple times to use that specific port without waiting for the OS to release the port. Is there any way that python forces the OS to release the port?

In addition, all sockets are closed at the end of the file transmission.

Mahyar Hosseini
  • 131
  • 4
  • 12
  • http://superuser.com/questions/307624/how-to-close-or-unbind-a-port-left-open-by-a-rogue-application – thebjorn Jun 25 '15 at 22:49
  • 1
    possible duplicate of [How to close a socket left open by a killed program?](http://stackoverflow.com/questions/5875177/how-to-close-a-socket-left-open-by-a-killed-program) – tzaman Jun 25 '15 at 22:53
  • possible duplicate of [How to use socket in Python as a context manager?](http://stackoverflow.com/questions/16772465/how-to-use-socket-in-python-as-a-context-manager) – Peter Wood Jun 25 '15 at 23:01
  • @thebjorn I tried "netstat" to find the process to kill it, But no process is shown using port 45678. I also tried "sudo pkill python", but it does not solve the problem. – Mahyar Hosseini Jun 25 '15 at 23:03
  • @tzaman Thank you. It worked :) – Mahyar Hosseini Jun 25 '15 at 23:36

1 Answers1

2

Most likely your socket is stuck in TIME_WAIT state. Try SO_REUSEADDR call from this question:

Python: Binding Socket: "Address already in use"

Community
  • 1
  • 1
theamk
  • 1,420
  • 7
  • 14