0
我聊天客戶端,但是當我運行它,我得到這樣一個錯誤:的Socket程序需要一個整數
Traceback (most recent call last):
File "C:\Users\Public\Documents\Programming\Chat Client\Chat Client.py", line 21, in
<module>
s = socket.socket((socket.AF_INET, socket.SOCK_STREAM))
File "C:\Program Files\Python33\lib\socket.py", line 94, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
TypeError: an integer is required
我不知道這意味着什麼,以便任何人都可以幫我嗎?
這裏是我的程序代碼:
# Import Modules
from tkinter import *
from socket import *
from threading import *
# Window Setup
root = Tk()
root.title('Chat Client')
root.state('zoomed')
# Chat Variables
global s
s = socket.socket((socket.AF_INET, socket.SOCK_STREAM))
s.connect((TARGET, DEFAULT_PORT))
enter = StringVar()
TARGET = s.gethostname()
DEFAULT_PORT = 45000
# Chat Message Box Setup
chat = Text(root, height=31, state=DISABLED)
entry = Entry(root, fg='blue', textvariable=enter, width=200)
scroll = Scrollbar(root)
chat['yscrollcommand'] = scroll.set
scroll['command'] = chat.yview
scroll.pack(side=RIGHT, fill=Y)
chat.pack(side=TOP, fill=X)
entry.pack(side=BOTTOM)
# Send Command
def send(event):
msg = enter.get()
chat['state'] = NORMAL
chat['fg'] = 'blue'
chat.insert(END, ('You: ' + msg + '\n'))
while 1:
s.sendall(msg)
chat['state'] = DISABLED
chat['fg'] = 'black'
enter.set('')
s.close()
entry.bind('<Return>', send)
def recieve():
s.bind((TARGET, DEFAULT_PORT))
s.listen(True)
conn, addr = s.accept()
while True:
data = conn.recv(1024)
chat['state'] = NORMAL
chat['fg'] = 'red'
chat.insert(END, ('Stranger: ' + data + '\n'))
thread.start(recieve,())
thread.start(send,())
root.mainloop()
所以,任何人都可以請幫我這個,因爲我不知道我需要什麼我的代碼來解決這個問題做。有任何想法嗎?
在此先感謝!