我目前正在尝试制作自己的私人消息程序,仅供我和我的朋友使用,并且我一直在使用" s.listen(1)"客户聊天程序的一部分,我不明白我遇到的原因,我尝试了很多不同的东西来弄清楚是什么问题。
错误是:
Unhandled exception in thread started by <function GetConnected at 0x027E42F0>
Traceback (most recent call last):
File "client.pyw", line 84, in GetConnected
s.listen(1)
File "<string>", line 1, in listen
socket.error: (10022, 'Invalid argument')
以下是3个代码文件:
from Tkinter import *
from socket import *
import urllib
import re
import pygame
import win32gui
def getmixerargs():
pygame.mixer.init()
freq, size, chan = pygame.mixer.get_init()
return freq, size, chan
def initMixer():
BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
FREQ, SIZE, CHAN = getmixerargs()
pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
def playsound(soundfile):
pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound(soundfile)
clock = pygame.time.Clock()
sound.play()
while pygame.mixer.get_busy():
clock.tick(1000)
def playmusic(soundfile):
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
pygame.mixer.music.load(soundfile)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
clock.tick(1000)
def stopmusic():
pygame.mixer.music.stop()
#HOW TO PLAY SONG:
initMixer()
#playmusic(filename)
def FlashMyWindow(title):
ID = win32gui.FindWindow(None, title)
win32gui.FlashWindow(ID,True)
def FlashMyWindow2(title2):
ID2 = win32gui.FindWindow(None, title2)
win32gui.FlashWindow(ID2,True)
def GetExternalIP():
url = "http://checkip.dyndns.org"
request = urllib.urlopen(url).read()
return str(re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request))
def GetInternalIP():
return str(gethostbyname(getfqdn()))
def FilteredMessage(EntryText):
EndFiltered = ''
for i in range(len(EntryText)-1,-1,-1):
if EntryText[i]!='\n':
EndFiltered = EntryText[0:i+1]
break
for i in range(0,len(EndFiltered), 1):
if EndFiltered[i] != "\n":
return EndFiltered[i:]+'\n'
return ''
def LoadConnectionInfo(ChatLog, EntryText):
if EntryText != '':
ChatLog.config(state=NORMAL)
if ChatLog.index('end') != None:
ChatLog.insert(END, EntryText+'\n')
ChatLog.config(state=DISABLED)
ChatLog.yview(END)
def getName():
x = GetExternalIP()
def LoadMyEntry(ChatLog, EntryText):
if EntryText != '':
ChatLog.config(state=NORMAL)
if ChatLog.index('end') != None:
if getName() is not ("96.231.3." + range(1, 1000)):
name = "Conor"
else:
name = "Ceci"
LineNumber = float(ChatLog.index('end'))-1.0
ChatLog.insert(END, name + ": " + EntryText)
ChatLog.tag_add(name, LineNumber, LineNumber+0.4)
ChatLog.tag_config(name, foreground="#FF8000", font=("Arial", 12, "bold"))
ChatLog.config(state=DISABLED)
ChatLog.yview(END)
def LoadOtherEntry(ChatLog, EntryText):
if EntryText != '':
ChatLog.config(state=NORMAL)
if ChatLog.index('end') is not None:
try:
LineNumber = float(ChatLog.index('end'))-1.0
except:
pass
def getName():
if GetInternalIP() is not ("96.231.3." + range(1, 1000)):
name2 = "Ceci"
else:
name2 = "Conor"
getName()
ChatLog.insert(END, name2 + ": " + EntryText)
ChatLog.tag_add(name2, LineNumber, LineNumber+0.6)
ChatLog.tag_config(name2, foreground="#04B404", font=("Arial", 12, "bold"))
ChatLog.config(state=DISABLED)
ChatLog.yview(END)
文件2:
import thread
from ChatFns import *
# ---------------------------------------------------#
# ---------INITIALIZE CONNECTION VARIABLES-----------#
# ---------------------------------------------------#
# Initiate socket and bind port to host PC
WindowTitle = 'Messenger v1 - Host'
s = socket(AF_INET, SOCK_STREAM)
HOST = gethostname()
PORT = 801111
conn = ''
s.bind((HOST, PORT))
# ---------------------------------------------------#
# ------------------ MOUSE EVENTS -------------------#
# ---------------------------------------------------#
def ClickAction():
# Write message to chat window
EntryText = FilteredMessage(EntryBox.get("0.0", END))
LoadMyEntry(ChatLog, EntryText)
# Scroll to the bottom of chat windows
ChatLog.yview(END)
# Erase previous message in Entry Box
EntryBox.delete("0.0", END)
# Send my message to all others
conn.sendall(EntryText)
# ---------------------------------------------------#
# ----------------- KEYBOARD EVENTS -----------------#
# ---------------------------------------------------#
def PressAction(event):
EntryBox.config(state=NORMAL)
ClickAction()
def DisableEntry(event):
EntryBox.config(state=DISABLED)
# ---------------------------------------------------#
# -----------------GRAPHICS MANAGEMENT---------------#
# ---------------------------------------------------#
# Create a window
base = Tk()
base.title(WindowTitle)
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)
# Create a Chat window
ChatLog = Text(base, bd=0, bg="white", height="8", width="50", font="Arial", )
ChatLog.insert(END, "Waiting for your partner to connect..\n")
ChatLog.config(state=DISABLED)
# Bind a scrollbar to the Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="heart")
ChatLog['yscrollcommand'] = scrollbar.set
# Create the Button to send message
SendButton = Button(base, font=30, text="Send", width="12", height=5,
bd=0, bg="#FFBF00", activebackground="#FACC2E",
command=ClickAction)
# Create the box to enter message
EntryBox = Text(base, bd=0, bg="white", width="29", height="5", font="Arial")
EntryBox.bind("<Return>", DisableEntry)
EntryBox.bind("<KeyRelease-Return>", PressAction)
# Place all components on the screen
scrollbar.place(x=376, y=6, height=386)
ChatLog.place(x=6, y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=90, width=265)
SendButton.place(x=6, y=401, height=90)
# ---------------------------------------------------#
# ----------------CONNECTION MANAGEMENT--------------#
# ---------------------------------------------------#
def GetConnected():
s.listen(1)
global conn
conn, addr = s.accept()
LoadConnectionInfo(ChatLog, 'Connected with: ' + str(
addr) + '\n---------------------------------------------------------------')
while 1:
try:
data = conn.recv(1024)
LoadOtherEntry(ChatLog, data)
if base.focus_get() == None:
FlashMyWindow(WindowTitle)
playsound('notif.wav')
except:
LoadConnectionInfo(ChatLog, '\n [ Your partner has disconnected ]\n [ Waiting for him to connect..] \n ')
GetConnected()
conn.close()
thread.start_new_thread(GetConnected, ())
base.mainloop()
文件3:
import thread
from ChatFns import *
# ---------------------------------------------------#
# ---------INITIALIZE CONNECTION VARIABLES-----------#
# ---------------------------------------------------#
# Initiate socket and bind port to host PC
WindowTitle = 'Messenger v1 - Client'
HOST = "(My local ip would normally be here)"
PORT = 801111
s = socket(AF_INET, SOCK_STREAM)
# ---------------------------------------------------#
# ------------------ MOUSE EVENTS -------------------#
# ---------------------------------------------------#
def ClickAction():
# Write message to chat window
EntryText = FilteredMessage(EntryBox.get("0.0", END))
LoadMyEntry(ChatLog, EntryText)
# Scroll to the bottom of chat windows
ChatLog.yview(END)
# Erase previous message in Entry Box
EntryBox.delete("0.0", END)
# Send my message to all others
conn.sendall(EntryText)
# ---------------------------------------------------#
# ----------------- KEYBOARD EVENTS -----------------#
# ---------------------------------------------------#
def PressAction(event):
EntryBox.config(state=NORMAL)
ClickAction()
def DisableEntry(event):
EntryBox.config(state=DISABLED)
# ---------------------------------------------------#
# -----------------GRAPHICS MANAGEMENT---------------#
# ---------------------------------------------------#
# Create a window
base = Tk()
base.title(WindowTitle)
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)
# Create a Chat window
ChatLog = Text(base, bd=0, bg="white", height="8", width="50", font="Arial", )
ChatLog.insert(END, "Waiting for your partner to connect..\n")
ChatLog.config(state=DISABLED)
# Bind a scrollbar to the Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="heart")
ChatLog['yscrollcommand'] = scrollbar.set
# Create the Button to send message
SendButton = Button(base, font=30, text="Send", width="12", height=5,
bd=0, bg="#FFBF00", activebackground="#FACC2E",
command=ClickAction)
# Create the box to enter message
EntryBox = Text(base, bd=0, bg="white", width="29", height="5", font="Arial")
EntryBox.bind("<Return>", DisableEntry)
EntryBox.bind("<KeyRelease-Return>", PressAction)
# Place all components on the screen
scrollbar.place(x=376, y=6, height=386)
ChatLog.place(x=6, y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=90, width=265)
SendButton.place(x=6, y=401, height=90)
# ---------------------------------------------------#
# ----------------CONNECTION MANAGEMENT--------------#
# ---------------------------------------------------#
def GetConnected():
s.listen(1)
global conn
conn, addr = s.accept()
LoadConnectionInfo(ChatLog, 'Connected with: ' + str(
addr) + '\n---------------------------------------------------------------')
while 1:
try:
data = conn.recv(1024)
LoadOtherEntry(ChatLog, data)
if base.focus_get() == None:
FlashMyWindow(WindowTitle)
playsound('notif.wav')
except:
LoadConnectionInfo(ChatLog, '\n [ Your partner has disconnected ]\n [ Waiting for him to connect..] \n ')
GetConnected()
conn.close()
thread.start_new_thread(GetConnected, ())
base.mainloop()
答案 0 :(得分:0)
端口号为0-65535(或者至少它们是在IPv4时代,确定v6)。
要有机会让这个工作,你应该改变:
PORT = 801111
到0-65535范围内尚未使用的东西,最好是现有的协议/应用程序已经众所周知,例如:
PORT = 8011