0

I have a simple socket server set-up to send some numbers to a client in the intranet. While testing, I stop the server.py script from terminal(CTRL+C) which later causes busy server error in Safari when I try to reach the same page.

I saw this serverfault question But the issues are:

  1. I could not find the /etc/init.d/networking restart file. I am using Mac and this is for Linux. Also it is an overkill for every-time I test my server. At least 10 times an hour.

  2. Inserting the option SO_REUSEADDR in the code did help Address already in use but I reckon that my server is up but is in a busy state. Using the answer here, I had edited the code to this:

host = <my machine address>  
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = 8080
s.bind((host, port))

What changes do I make on my server to remove this error in Safari?

Safari can’t open the page "my-ip" because the server unexpectedly dropped the connection. This sometimes occurs when the server is busy. Wait for a few minutes and then try again.

Output of lsof -i:8080, after stopping the script was

python3.7 11881 <user>    3u  IPv4 <Device>   0t0  TCP <my machine address>?:http-alt (LISTEN)

I did kill 11881 and then re-run the code but got the same message in Safari.

Output of netstat -na | grep "8080" was

tcp4       0      0  <my machine address>.8080     *.*    LISTEN    

while the script was running and nothing when I KeyboardInterrupt it.

Firefox shows the page momentarily and then shows the error page

The connection was reset

Chrome shows an empty page only.

Update: It works in python 2.7 but not in 3.5. The difference in the code comes at:

c.send('\n')

in 2.7 and

c.send(bytes('\n'.encode('utf-8')))

in 3.5 where

c is

c, (client_host, client_port) = s.accept()

Rest is all the same.

  • The file name `/etc/init.d/networking` is completely specified; it points to a file named `networking` in the `init.d` directory in the `etc` directory in the root directory. However, it is distro-specific (probably Debian) so if you are on a different platform, you need to find out how to restart networking services on your system. (This is probably overkill in this situation, though.) – tripleee Jun 26 '19 at 08:16
  • @tripleee yes it a platform issue, I'll include it in my question. Also that is really an overkill as visible in the comments here: https://serverfault.com/a/329846 –  Jun 26 '19 at 08:20
  • @barmar I have edited the question to include why this is different. Could you please have a look? –  Jun 26 '19 at 08:25
  • Have you tried doing what it says in the duplicate? My money is on "still a duplicate" even if the symptoms are different. – tripleee Jun 26 '19 at 08:34
  • 1
    https://apple.stackexchange.com/questions/284251/how-to-restart-network-services-on-sierra Is my third topmost Google hit for "macos restart network" (the top two were specifically about wifi). – tripleee Jun 26 '19 at 08:36
  • I edited the code as I said. Not other things. Can you specify which one in particular? –  Jun 26 '19 at 08:48
  • My recollection is that you get an error every second time with `SO_REUSE` but this might have been on Linux. In this case, the slightly dismal but manageable workaround is to try twice. – tripleee Jun 26 '19 at 08:52
  • Why do you think this is different? The "address already in use" error happens on the server, not the client; the client error message is a separate symptom of the same problem. – Barmar Jun 26 '19 at 15:26
  • The `en0` answer only wrks for the interface with that name; there is no way to know just from that name if that's a wifi interface or not. There are other answers which shut down all interfaces; or just repeat for every interface you want to reset. – tripleee Jun 26 '19 at 17:54
  • @tripleee Using `ifconfig` revelas 8-9 names. Do I need to reset them all? –  Jun 26 '19 at 18:08
  • Probably not; some are probably specific to Docker and some might be present but not up. In fact just the one which your program is connecting to should be sufficient I guess. – tripleee Jun 26 '19 at 18:26
  • (Not in a place where chat is convenient.) – tripleee Jun 26 '19 at 18:26
  • @tripleee it is wifi then. I reset it and it didnt work. –  Jun 26 '19 at 18:27
  • @Barmar it doesn't work, for one . the output in the terminal is different and are in two different stages of a server setup, for two. Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195590/discussion-between-ankiiiiiii-and-tripleee). –  Jun 26 '19 at 18:40
  • 1
    I've reopened the question. You did ask where you're supposed to put the `SO_REUSEADDR` option, so it seemed like that question answered it. – Barmar Jun 26 '19 at 18:46

1 Answers1

0

I had accidentally commented out a line

c.recv(1000)

Where c is mentioned in the question.

I found it when I compared this to an older version of the same file.