Hi, I made a client and server program using the UDP connection. When I try and run it on my lan it works fine. Everything works right and lets me log into the server, but when I try to do it over the net, there is no connection or anything being send. Does UDP not work over the www only on lan? Or did I miss something… I am behind a router so I tried setting up port forwarding like so:
App: Client2, Port From: 21567, Protocol: Both, IP Address: 192.168.1.135, Port to: 21568, and enable
WAN: 75.134.61.245
LAN: 192.168.1.135
My code:
Code:
# Server program
from socket import *
import threading
import time
# Set the socket parameters
host = "192.168.1.135"
port = 21568
port2 = 21567
buf = 1024
buf2 = 1024
addr = (host,port)
# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
data,addr = UDPSock.recvfrom(buf)
client = data
addr2 = (client, port2)
data,addr = UDPSock.recvfrom(buf)
if data == "Hey you there?":
data = "Yes"
(UDPSock.sendto(data,addr2))
print client, " trying to log on."
# Receive messages
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
data2 = "Users/" + data + ".txt"
f = open(data2,"r")
lineList = f.readlines()
f.close()
name = data
name2 = "Name: " + data + "\n"
if name2 == lineList[0]:
data,addr = UDPSock.recvfrom(buf)
password = "Password: " + data + "\n"
if password == lineList[1]:
print name,"log on."
data = name
(UDPSock.sendto(data,addr2))
data = "===Welcome==="
(UDPSock.sendto(data,addr2))
while 1:
class receiver(threading.Thread) : # Receiver thread
def __init__(self, socket ):
threading.Thread.__init__(self)
self.setDaemon(True)
def run(self) :
while True : # Receive thread
data,addr = UDPSock.recvfrom(buf)
if not data : break
print data
UDPSock.close()
receiver(UDPSock).start()
while True : # Send in main thread
data = time.ctime() + " Mradr: " + raw_input()
(UDPSock.sendto(data,addr2))
UDPSock.close()
else:
print "Wrong."
else:
print "No one by that name."
# Close socket
UDPSock.close()