2016-04-24 10 views
0

こんにちは、これを読んでくれてありがとう。私が知っている何が問題ですが、私はそれを修正する方法を見つけることができません。tkコマンドでソケット関数を使用する場合の 'WinError 10061'

だから、問題は(私が思う)このように書き、Pythonはそのためのpythonが順番に[WinError 10061]が発生し、ブランクホスト('')に接続しようとしている(hostname)ユーザがホスト名を入力する前connect()を実行しようとしています発生する。私はhostnameは('')空白だった場合'localhost'hostnameを設定ifの文を追加した場合でも、エラーが起こっに保たれ、別の関数(connect_buffer())でバッファリングconnect()を試してみましたが、それはどちらかの仕事と同じように上がっていませんでしたエラー。

私の質問はこれをどのように修正するのですか?ここで

は誤りです:

WinError 10049: The requested address is not valid in its context

Traceback (most recent call last): 
    File "H:\server\New folder\Tk_cleint.py", line 89, in <module> 
    setup_confirm_button = tk.Button(window,text = 'Connect', command = setup()) 
    File "H:\server\New folder\Tk_cleint.py", line 18, in setup 
    create_sock(host, int(port)) 
    File "H:\server\New folder\Tk_cleint.py", line 36, in create_sock 
    connect(cleintsocket, nhost, nport) 
    File "H:\server\New folder\Tk_cleint.py", line 27, in connect 
    self.connect((hostname, connectingport)) 
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it 

そしてここでは、私のコード

#---Import statments---# 
import socket, os, multiprocessing 
import tkinter as tk 

#---global variables---# 
setup = '' 
cleintsocket = '' 

#---Defs---# 
def setup(): 
    global host, port, user 
    host = setup_host_box.get() 
    port = setup_port_box.get() 
    user = setup_user_box.get() 

def connect_buffer(self, hostname, connectingport): 
    connect(self, hostname, connectingport) 

def connect(self, hostname, connectingport): 
    if hostname == '': 
     hostname = 'localhost' 
    self.connect((hostname, int(connectingport))) 
    print('connected') 
    multiprocessing.Process(target = resv()).start() 

def create_sock(nhost, nport): 
    global cleintsocket 
    cleintsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    connect(cleintsocket, nhost, nport) 

def send(username, cleintsock): 
    '''to send a message''' 
    usrmsg = (username + ' - ' + chat_msg_box.get()).encode() 
    cleintsock.send(usrmsg) 

def resv(sock): 
    '''resive subscript, run through mutiprosses module''' 
    while True: 
     rmsg = sock.recv(1024).decode() 
     chat_msg_display_text.insert('end.0.', rmsg) 

def chat(): 
    '''loads chat page''' 
    setup_host_text.pack_forget() 
    setup_host_box.pack_forget() 
    setup_port_text.pack_forget() 
    setup_port_box.pack_forget() 
    setup_user_text.pack_forget() 
    setup_user_box.pack_forget() 
    setup_confirm_button.pack_forget() 
    chat_msg_display_text.pack() 
    chat_msg_box.pack() 
    chat_msg_send_button.pack() 

def start(): 
    '''starts the setup page''' 
    setup_host_text.pack() 
    setup_host_box.pack() 
    setup_port_text.pack() 
    setup_port_box.pack() 
    setup_user_text.pack() 
    setup_user_box.pack() 
    setup_confirm_button.pack() 

def send_button_callback(): 
    '''add a buffer to allow time for 'cleintsocket' to be defined in "create_sock()"''' 
    send(user, cleintsocket) 

#---TK Setup---# 
#--window setup--# 
window = tk.Tk() 
window.title('Chat') 
window.geometry('600x600') 
window.configure(background='#ffffff') 
#--connection setup page--# 
setup_host_text = tk.Label(window, text = 'Host') 
setup_host_box = tk.Entry(window, bg = '#ffffff') 
setup_port_text = tk.Label(window, text = 'Port') 
setup_port_box = tk.Entry(window, bg = '#ffffff') 
setup_user_text = tk.Label(window, text = 'Username') 
setup_user_box = tk.Entry(window, bg = '#ffffff') 
setup_confirm_button = tk.Button(window,text = 'Connect', command = setup()) 
#--chat page--# 
chat_msg_box = tk.Entry(window, bg='#ffffff') 
chat_msg_send_button = tk.Button(window, text = 'send', command = send_button_callback) 
chat_msg_display_text = tk.Text(window, width=600, height=500, wrap = 'word') 
#--------------# 

start() 

、ここで助けにはならなかった質問へのリンクありありConnecting to myself through my public IP through TCP

Webscraping with Python: WinError 10061: Target machine actively refused

お手伝いいただきありがとうございます。あなたがcommand = setup()を使用しているあなたのsetup_confirm_buttonで

+0

@Pythonisa私はそれだけで答えとして、それを追加し、病気それは –

+0

おかげでそれを試してみましょうあなたは、このような大きな助けとなっているありがとうございます! –

+0

編集答えをクリックして働いたおかげ@Pythonista – Pythonista

答えて

1

これはあなたが実際に関数を呼び出す代わりに、コマンドの関数への参照として、それを設定し、それはあなたを実行しているしているsetup()呼び出すことによりcommand = setupまたはcommand = lambda: setup()

する必要がありますボタンをクリックする代わりに機能します。

lambda: setup()を使用する理由は、lambdaが匿名機能を作成するためです。

また、resvを渡すのではなく、resv()にも同様に呼び出しています。これは、whileループでこの関数を呼び出し、メインイベントループをブロックしています。

+0

凍結だ理由について、ダニ –

関連する問題