この単純な python チャット サーバーを起動して実行しようとしています: http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server
毎回新しいポートを手動で選択する必要があることを除けば、問題なく動作しています。そうしないと、ポートが既に使用されているというエラー 98 が表示されます。
強制終了されたプログラムによって開いたままになっているソケットを閉じる方法は? SO_REUSEADDR: を使用することをお勧めしますが、それをベアボーンの python プログラムに実装する方法がわかりません。確かに、私はナブです。
このページでは、クレイジーな自動選択を行うことを提案していますが、それは必要以上に複雑に思えます。 http://twistedmatrix.com/pipermail/twisted-python/2005-August/011098.html
私を助けてくれる人に感謝します!
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
#print "data is ", data
a = data.split(':')
if len(a) > 1:
command = a[0]
content = a[1]
msg = ""
if command == "iam":
self.name = content
msg = self.name + " has joined"
elif command == "msg":
msg = self.name + ": " + content
print msg
for c in self.factory.clients:
c.message(msg)
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor.run()