2

我正在尝试使用套接字建立服务器/客户端连接。但是它们不会正确关闭,我无法理解为什么。

更新 1

我已经纠正了我在下面没有实际调用问题中的 s.close 函数的愚蠢错误。但事实证明这不是我的问题。

更新结束

这是我的服务器代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

if __name__ == '__main__':
    # Server connection
    s = socket.socket()          # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12345                 # Reserve a port for your service.

    print 'Server started!'
    print 'Waiting for clients...'

    s.bind((host, port))        # Bind to the port
    s.listen(5)                 # Now wait for client connection.
    c, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    msg = c.recv(1024)

    print addr, ' >> ', msg

    if msg == 'close':
        print 'Closing down'
        c.send('SENT: Closing down')

    c.shutdown(socket.SHUT_RDWR)
    c.close()

这是我的客户代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket

if __name__ == '__main__':
    # Server
    s = socket.socket()          # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12345                 # Reserve a port for your service.

    print 'Connecting to ', host, port
    s.connect((host, port))

    msg = raw_input('CLIENT >> ')
    s.send(msg)
    msg = s.recv(1024)
    print 'SERVER >> ', msg

    s.close()                     # Close the socket when done

这是它产生的错误消息:

In [13]: %run cjboxd.py
Server started!
Waiting for clients...
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    173             else:
    174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/home/nine/slask/cjboxd.py in <module>()
     19     print 'Waiting for clients...'
     20 
---> 21     s.bind((host, port))        # Bind to the port
     22     s.listen(5)                 # Now wait for client connection.
     23     c, addr = s.accept()     # Establish connection with client.

/usr/lib/python2.7/socket.pyc in meth(name, self, *args)
    222 
    223 def meth(name,self,*args):
--> 224     return getattr(self._sock,name)(*args)
    225 
    226 for _m in _socketmethods:

error: [Errno 98] Address already in use

它会在一分钟后工作。

4

3 回答 3

9

你需要socket.socket.setsockopt,.ies.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

虽然套接字会在进程结束时被操作系统关闭,但显式调用 close() 是一种很好的行为。但是,在那之后,本地 addr(local_ip, local_port) 直到 2MSL(maximum segment lifetime)过去后才可用。为什么?我们能做什么?你可以阅读这些:

http://www.tcpipguide.com/free/t_TCPConnectionTermination-3.htmhttp://www.unixguide.net/network/socketfaq/4.5.shtml

我很难比他们更清楚地发布它:)。

于 2012-09-17T11:16:12.173 回答
8
s.close                     # Close the socket when done

突出我的眼睛,因为你实际上什么都不叫,你应该试试s.close()

于 2012-09-17T11:15:28.403 回答
3

你打电话s.close,而不是s.close()

如果您希望客户端终止连接,则需要调用 socket.close() 方法。`

于 2012-09-17T11:15:42.397 回答