1

I'm currently trying to understand sockets in Python and I'm using the following code to receive data transmitted via TCP from a Smartphone on a Hotspot network.

import socket
import logging

# Logging routine
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 5000)
logger.info('starting up on %s port %s' % server_address)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

# Wait for a connection
logger.info('waiting for a connection')
connection, client_address = sock.accept()

try:
    logger.info('connection from', client_address)

    while True:
        data = connection.recv(16)

        if data:
            print 'Do stuff here'
        else:
            print 'no more data from', client_address
            break

finally:
    # Clean up the connection
    connection.close()

The first time I run the code everything works correctly. After running the script and want to run again I have the following error: [Errno 48] Address already in use?. However, if I run the script again after the program crashes, it all works correctly. I had checked and confirmed that it ends on the finally statement for connection.close(). It seems that I have always to run the program two times to be able to run two consecutive times.

dudas
  • 403
  • 6
  • 23
  • After each run, execute this command at the shell prompt: `netstat -an | egrep :5000`. Let us know what it says. – Robᵩ Nov 05 '15 at 17:07
  • @Rob The supplied command does not produce output. – dudas Nov 05 '15 at 17:13
  • My request was to run that command after each run. The test you describe runs your command three times. After which of the three runs you describe did the command produce no output? – Robᵩ Nov 05 '15 at 17:21

1 Answers1

2

This is because the previous execution has left the socket in a TIME_WAIT state, and can’t be immediately reused.

in order to prevent this, set socket.SO_REUSEADDR:

change

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

to

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28
  • Thanks for your reply. But unfortunately did not work either. I run the script one time. On the second time I get the error. If I run again it run with no problems. It did not solve. – dudas Nov 05 '15 at 16:17
  • what is the error? is it `socket.error: [Errno 98] Address already in use` ? – Fujiao Liu Nov 05 '15 at 16:21
  • Negative, it's the very same error: `[Errno 48] Address already in use` – dudas Nov 05 '15 at 16:26
  • because you run the script before, it is most likely that process still bound to the port(5000). I think you need kill the process binding to the port and then re-run the script. check this, http://stackoverflow.com/questions/19071512/socket-error-errno-48-address-already-in-use – Fujiao Liu Nov 05 '15 at 16:36
  • Humm, ok that makes sense. Is there any chance to make that programmatically on the end of the script? – dudas Nov 05 '15 at 16:40
  • it is wired, I run the code on ubuntu and it works. . – Fujiao Liu Nov 05 '15 at 16:51
  • @ I'm running on a Mac. I can try Ubuntu but I would prefer running this on Mac. It's a odd problem thought. – dudas Nov 05 '15 at 17:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94377/discussion-between-lfj-and-dudas). – Fujiao Liu Nov 06 '15 at 02:11