{"action":"opened","issue":{"url":"https://api.github.com/repos/OGG-MAH/ChatRoom/issues/3","repository_url":"https://api.github.com/repos/OGG-MAH/ChatRoom","labels_url":"https://api.github.com/repos/OGG-MAH/ChatRoom/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/OGG-MAH/ChatRoom/issues/3/comments","events_url":"https://api.github.com/repos/OGG-MAH/ChatRoom/issues/3/events","html_url":"https://github.com/OGG-MAH/ChatRoom/issues/3","id":149415053,"number":3,"title":"简单的例子","user":{"login":"originals-tz","id":15152973,"avatar_url":"https://avatars.githubusercontent.com/u/15152973?v=3","gravatar_id":"","url":"https://api.github.com/users/originals-tz","html_url":"https://github.com/originals-tz","followers_url":"https://api.github.com/users/originals-tz/followers","following_url":"https://api.github.com/users/originals-tz/following{/other_user}","gists_url":"https://api.github.com/users/originals-tz/gists{/gist_id}","starred_url":"https://api.github.com/users/originals-tz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/originals-tz/subscriptions","organizations_url":"https://api.github.com/users/originals-tz/orgs","repos_url":"https://api.github.com/users/originals-tz/repos","events_url":"https://api.github.com/users/originals-tz/events{/privacy}","received_events_url":"https://api.github.com/users/originals-tz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-04-19T10:19:39Z","updated_at":"2016-04-19T10:19:39Z","closed_at":null,"body":"服务器\r\n```\r\nfrom asyncore import dispatcher\r\nimport socket\r\nimport asyncore\r\n\r\nPO = 5005 #端口号\r\n\r\nclass ChatServer(dispatcher):\r\n\r\n\r\n    def __init__(self, port):\r\n        dispatcher.__init__(self)\r\n        self.create_socket(socket.AF_INET, socket.SOCK_STREAM) #创建套接字\r\n        self.set_reuse_addr() #可以在服务器没有正常关闭的情况下重用同一个端口号\r\n        self.bind(('',port)) #绑定主机ip和端口\r\n        self.listen(5) #监听端口\r\n        self.sessions = [] #可使用的客户端队列\r\n        self.count = 0 #测试使用，当有两个客户端连接时，向所有客户端广播一条信息\r\n\r\n    def handle_accept(self):\r\n        conn, addr = self.accept() #接收连接，并将新的socket赋予conn\r\n        self.sessions.append(conn) #加入可用客户端队列\r\n        print addr, \" has joined !\" #打印客户端ip\r\n        self.count = self.count+1 #测试使用\r\n        if self.count == 2: #可用客户端是否达到两个\r\n            for i in self.sessions: #向每个客户端发送一条信息\r\n                i.send(\"hello\")\r\n\r\nif __name__ == '__main__':\r\n    s = ChatServer(PO)\r\n    asyncore.loop()\r\n```\r\n\r\n客户端\r\n```\r\n# coding=utf-8\r\nimport socket\r\ncline = socket.socket() #创建套接字\r\nhostname = socket.gethostname() #获得本地主机名，因为服务器也是本地的\r\nport = 5005 #指定服务器开放的端口\r\nprint cline.connect(('',port)) #连接\r\nprint cline.recv(3534) #接收信息并打印\r\n```"}}