2013-03-09 57 views
0

我从人们更好的聊天客户端以下帮助:我的聊天客户端开始线程后冻结了

他们告诉我,如果我不想等待消息时,被阻塞的.recv,我需要使用线程,类,函数和队列来这样做。

所以我跟着一些帮助一个特定的人给了我从哪里创建一个类从一个线程,然后定义一个函数,应该读取传入的消息并打印出来。

我还创建了一个函数,允许您输入要发送的东西。

事情是,当我运行程序。什么都没发生。

有人可以帮助指出什么是错的? (我问过的问题,并研究了3天,不成气候,所以我也尝试)

from socket import * 
import threading 
import json 
import select 

print("Client Version 3") 
HOST = input("Connect to: ") 
PORT = int(input("On port: ")) 
# Create Socket 

s = socket(AF_INET,SOCK_STREAM) 
s.connect((HOST,PORT)) 
print("Connected to: ",HOST,) 

#-------------------Need 2 threads for handling incoming and outgoing messages-- 

#  1: Create out_buffer: 
Buffer = [] 

rlist,wlist,xlist = select.select([s],Buffer,[]) 

class Incoming(threading.Thread): 
    # made a function a thread 
    def Incoming_messages(): 
     while True: 
      for i in rlist: 
       data = i.recv(1024) 
       if data: 
        print(data.decode()) 

# Now for outgoing data. 
def Outgoing(): 
    while True: 
     user_input=("Your message: ") 
     if user_input is True: 
      Buffer += [user_input.encode()] 
     for i in wlist: 
      s.sendall(Buffer) 
      Buffer = [] 

感谢您抽空看看,这还要感谢托尼狮子的提示此

回答

1

看看在你的代码的这个修订版:(在python3.3)

from socket import * 
import threading 
import json 
import select 


print("client") 
HOST = input("connect to: ") 
PORT = int(input("on port: ")) 

# create the socket 
s = socket(AF_INET, SOCK_STREAM) 
s.connect((HOST, PORT)) 
print("connected to:", HOST) 

#------------------- need 2 threads for handling incoming and outgoing messages-- 

#  1: create out_buffer: 
out_buffer = [] 

# for incoming data 
def incoming(): 
    rlist,wlist,xlist = select.select([s], out_buffer, []) 
    while 1: 
     for i in rlist: 
      data = i.recv(1024) 
      if data: 
       print("\nreceived:", data.decode()) 

# now for outgoing data 
def outgoing(): 
    global out_buffer 
    while 1: 
     user_input=input("your message: ")+"\n" 
     if user_input: 
      out_buffer += [user_input.encode()] 
#  for i in wlist: 
      s.send(out_buffer[0]) 
      out_buffer = [] 

thread_in = threading.Thread(target=incoming, args=()) 
thread_out = threading.Thread(target=outgoing, args=()) 
thread_in.start() # this causes the thread to run 
thread_out.start() 
thread_in.join() # this waits until the thread has completed 
thread_out.join() 
    在你的程序
  • 你有各种各样的问题,即你需要调用线程;仅仅定义它们是不够的。
  • 你也忘了行中的函数input():user_input = input(“your message:”)+“\ n”。
  • “select()”函数被阻塞,直到你有东西要读,所以程序没有到达代码的下一部分,所以最好将它移到读线程。
  • python中的send函数不接受列表;在python 3.3中,它接受一组字节,这是由encode()函数返回的,所以必须修改部分代码。
+0

谢谢,我真的犯了很多错误,我会尝试立即使用它。最近让这个工作成为一场真正的斗争。相信与否我已经在2-3天没有多少运气。您的回答对我来说意义重大,而不仅仅是具有文档参考的另一个答案。如果我有他们,我会发布任何其他问题。 – Micrified 2013-03-09 20:37:09

+0

当我运行它时,它的行为很奇怪。以下是发生的事情:我打开客户端#1:并连接到服务器。然后我连接客户端#2(没有任何反应)。接下来,我在客户端1的'消息提示中输入一些内容。用户2没有收到它。但是,只要客户端2键入并发送一些内容,它就会收到该消息。现在另一个客户端没有得到客户端2发送的内容。它必须等待收到消息之前发送消息。它看起来像现在,而不是让.recv阻止我们。看起来,用户输入停止接收和打印的过程 – Micrified 2013-03-09 21:05:54

+0

并打印消息。有什么办法可以绕过这个吗?需要过程吗? – Micrified 2013-03-09 21:06:39