Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 19th, 2009  |  syntax: Python  |  size: 2.44 KB  |  views: 316  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import socket, appuifw, e32, thread          
  2.  
  3. def chat_server():
  4.     server = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
  5.     channel = socket.bt_rfcomm_get_available_server_channel(server)
  6.     server.bind(("", channel))                                    
  7.     server.listen(1)                                              
  8.     socket.bt_advertise_service(u"btchat", server, True, socket.RFCOMM)
  9.     socket.set_security(server, socket.AUTH | socket.AUTHOR)          
  10.     print "Waiting for clients..."                                    
  11.     conn, client_addr = server.accept()                                
  12.     fd = conn.makefile("r+")                                          
  13.     waiting_for_msg_same_thread = e32.ao_callgate(receive_msg)        
  14.     waiting_for_msg = thread.start_new_thread(waiting_for_msg_same_thread, (fd, ))
  15.     while(True):                                                                  
  16.         msg = appuifw.query("Send:", "text")                                      
  17.         send_msg(fd, msg)                                                        
  18.     print "Client connected!"                                                    
  19.  
  20. def chat_client():
  21.     conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
  22.     address, services = socket.bt_discover()              
  23.     if 'btchat' in services:                              
  24.         channel = services[u'btchat']                    
  25.         conn.connect((address, channel))                  
  26.         print "Connected to server!"                      
  27.         fd = conn.makefile("r+")                          
  28.         waiting_for_msg_same_thread = e32.ao_callgate(receive_msg)
  29.         waiting_for_msg = thread.start_new_thread(waiting_for_msg_same_thread, (fd, ))
  30.         while(True):
  31.             msg = raw_input("send: ")
  32.             send_msg(fd, msg)
  33.  
  34.     else:
  35.         appuifw.note(u"Target is not running a btchat server",
  36.                 "error")
  37.  
  38.  
  39. def receive_msg(fd):
  40.     print "Waiting for message.."
  41.     reply = fd.readline()
  42.     print "Received: " + reply
  43.     for i in xrange(4):
  44.         print
  45.  
  46. def send_msg(fd, msg):
  47.     msg = appuifw.query(u"Send:", "text")
  48.     print "Sending: " + msg
  49.     print >> fd, msg
  50.  
  51. index = appuifw.popup_menu([u"New server", u"Connect to server"],
  52.                             u"BTChat mode")
  53. if index != None:
  54.         if index:
  55.                 chat_client()
  56.         else:
  57.                 chat_server()