i have a semi-complex socket program that I just wrote ( first foray into sockets in python ) but for the purposes of figuring this out, I'll use a much simpler example.
#client.py
from socket import *
#host = "localhost"
host = "127.0.0.1"
#host = "70.107.93.83";
port = 21567
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)
login = 0;
while (1):
data = raw_input('enter a message')
if not data:
break
else:
if(UDPSock.sendto(data,addr)):
print "data", data, "sent";
UDPSock.close()
#server.py
from socket import *
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"
UDPSock.close()
in these examples, client.py sends data to server.py. Its my understanding that server.py checks for incoming data because it has binded itself to the specified address. Beyond that, the two sockets seem identical. So, i wrote a method that has server.py use the same send command that client does to send a message to the client. this didnt work. I thought it was because I hadn't bound client to an address, so I did. However, i bound it to the same address as the server, so I changed the port (because theyre both running on the same computer). This didnt work, the program crashed, and good old python doesnt stay on to show you the run time errors, it just turns itself off. Can anyone modify the above example so that both can act as client/servers on the same computer? Thanks.