聊天应用程序在2条消息交换后停止

时间:2015-05-30 17:05:25

标签: python python-2.7 network-programming

我正在python中编写一个简单的聊天应用程序,它只适用于2条消息,之后它停止响应...对它的任何想法?

ServerClass.py

from socket import *
import time

class ServerClass:
    serverSocket = None
    serverPort = None
    def __init__(self):
        self.serverPort=20000
        self.serverSocket=socket(AF_INET,SOCK_STREAM)
        #self.serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.serverSocket.bind(("",self.serverPort))
        self.serverSocket.listen(10)

    def StartServer(self):
        while 1:
            connectionSocket,clientAddress=self.serverSocket.accept()
            message=connectionSocket.recv(1024)
            print message.split(':')[0]+": "+message
            modifiedMessage = raw_input("To send: ")
            connectionSocket.send(modifiedMessage)
    #connectionSocket.close()

客户端Class.py

from socket import *

class ClientClass:
    def __init__(self):
        self.serverIP="localhost"
        self.serverPort=20000
        self.clientSocket = socket(AF_INET,SOCK_STREAM)
        self.your_name="Nadeem Ahmed"

    def Connect(self):
        self.clientSocket.connect((self.serverIP,self.serverPort)) 

    def SendMessage(self,Message):
        self.clientSocket.send(Message) 
        modifiedMessage=self.clientSocket.recv(1024)
        print modifiedMessage

    def Leave(self):
        self.clientSocket.close()

0 个答案:

没有答案