简体   繁体   中英

How to remove the socket server from “busy” state after `kill PID`?

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" 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM