python - UDP Hole Punching Not Working -


i trying create chat program works udp hole punching , connecting directly node provided broker.

the script have right works fine locally. problems come when try use external address connect to. can't figure out on own hoping here me out!

import socket import json import landerdb import threading  class peerchat:      address = none      def __init__(self):         self.sock = socket.socket(socket.af_inet, socket.sock_dgram)         self.db = landerdb.connect("nodes")         self.brok_ip = ""         self.brok_port = 5000         self.nick = "test"         self.friends = []      def listen(self):         self.command = {              "here":self.here,             "msg":self.msg,             }           global address         self.sock.bind(address)         while true:             msg, addr = self.sock.recvfrom(1024)             try:                 data = json.loads(msg)             except:                 continue             if data[u'cmd'] in self.command:                 threading.thread(target=self.command[data[u'cmd']], args=(addr, data)).start()      def main(self):         while true:             msg = raw_input("> ")             msg = msg.split()             try:                 msg = json.dumps({"cmd":msg[0], "data":' '.join(msg[2:]), "nick":self.nick, "to":msg[1]})             except:                 continue             x in self.db.find("nodes", "all"):                 self.sock.sendto(msg, tuple(x['addr']))      def here(self, addr, data):         if not self.db.find("nodes", {"addr":addr}):             self.db.insert("nodes", {"addr":addr})         if data['nick'] in self.friends:             print data['nick'] + " has come online."     def msg(self, addr, data):         if data['to'] == self.nick:             print data['nick']+": "+data['data']      def getnodes(self):         self.sock.sendto("", (self.brok_ip, self.brok_port))         open("nodes", 'wb') file:             while true:                 msg, addr = self.sock.recvfrom(1024)                 if msg == "\n":                     break                 file.write(msg)         msg, addr = self.sock.recvfrom(1024)         global address          address = ("0.0.0.0", int(msg))            x in self.db.find("nodes", "all"):             addr = tuple(x['addr'])             self.sock.sendto(json.dumps({"cmd":"here", "nick":self.nick}),addr)  if __name__ == "__main__":     peerchat().getnodes()     threading.thread(target=peerchat().listen).start()     peerchat().main() 

i haven't tested code externally yet, because i'm @ uni, use different lines setup udp connection.

they might give ideas...

#!/usr/bin/python import socket import struct  sock = socket.socket(socket.af_inet, socket.sock_dgram, socket.ipproto_udp) sock.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1) sock.bind(('239.255.60.60', 4876))  mreq = struct.pack("=4sl", socket.inet_aton("239.255.60.60"), socket.inaddr_any) sock.setsockopt(socket.ipproto_ip, socket.ip_add_membership, mreq) packet_number = 0  while true:      packet_number += 1     print packet_number     raw_data = sock.recv(1024)     print raw_data     print 

i don't understand that, works me. adapted here. should read entire page couple of times understand saying. search of page 'ip_multicast_ttl' gather need set socket.ip_multicast_ttl, 33 in sock.setsockopt, number number > 32. remember unsure @ stage...


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -