Python sockets
YO again!
I've just started reading about socket programming in python.
import socket
addr = ("localhost", 3737)
buf = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(addr)
while True:
try:
data, address = s.recvfrom(buf)
print str(data) + "______" + str(address)
s.close()
break
except socket.error:
print "shit happens"
s.close()
break
#Python Client
import socket
addr = ("localhost", 3737)
buf = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto("MOHAHAHA IT'S ALIVE!!!", addr)
s.close()
Now when I run either of the scripts I get the following error:
markuperror: [Errno 10049] The requested address is not valid in its context
This should not happen :/
Google says:
markupThis normally results from an attempt to bind to an address that is not valid for the local computer.
which should not be the case -_-
any suggestions? :P
Edit: It must be my firewall. There is nothing wrong with the freaking code -_-
MoshBat wrote: Works perfectly for me, though your code spat out errors. Firewall/OS?
well using the code above (I updated the code fields) gave no errors on "localhost". But when I replace the "localhost" with "remote ip", the errors starts popping up :/
I'm using Win7 x86 with built in firewall :P nothing fancy xD
ooooooh I see!!!
you can appearently not bind an external/remote ip. so what you have to do is leave the address field empty. I'll post the code here in case anyone runs into a similar problem:
import socket
addr = ("", 3737) #Notice that there is no given address
buf = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Using UDP!
s.bind(addr)
while True:
try:
data, address = s.recvfrom(buf) #recieving info
print str(data) + " " + str(address)
except socket.error:
print "shit happens"
s.close()
break
import socket
addr = ("192.168.0.64", 3737) #my stationarys LAN address
buf = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Using UDP!
s.sendto("I'm here to serve you master!!!", addr)
s.close()
Also, you must make sure that you OS firewall allows connections to be made by/to the server!
or 0.0.0.0 should work
thnx, good to know =)
MoshBat wrote: Wait, you were putting the remote machine's IP as the server's host?
yes ;$ I thought I had to bind that remote ip to the server in order to be able to accept incoming data. apparently I was wrong since the server is listening for whoever connects!
Also I noticed 1 thing. The server is listening to port 3737, and the client sends the data to that port. Then why is it that I get this:
markupI'm here to serve you master!! ('192.168.0.66', 65011)
notice that 192.168.0.66 (my laptop) connected to my server 192.168.0.64. Although the connection was not made @ port 3737, instead they used 65011.
Is there a explanation for that or am I being stupid once again? :P
techb wrote: the port it is listening on is just that. It listens for a connection, once one is established it is passed to another port for communication or "talk" between the server and client.
Are you sure that's right? I was always under the impression that the client does not open a connection on it's 3737, to avoid another application that may be using that port, so it instead binds the connection to it's own, much higher, random, port and communicates with port 3737 on the server.
The server talks on 3737 the whole time, which is why only one client can be connected (at least in TCP) and to have multiple clients you need asynchronus or threaded connections.
That was at least what I've been told, I could just be shooting out random blabber.
stealth- wrote: I was always under the impression that the client does not open a connection on it's 3737, to avoid another application that may be using that port, so it instead binds the connection to it's own, much higher, random, port and communicates with port 3737 on the server.
The server talks on 3737 the whole time, which is why only one client can be connected (at least in TCP) and to have multiple clients you need asynchronus or threaded connections.
That was at least what I've been told, I could just be shooting out random blabber. This is right.
Here's a reference link for further reading: http://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_ports
techb wrote: I guess my impression was wrong. Good to know though. I'm only in my first year of networking classes. So try not to go TOO hard on me lol. We're just now going over subnetting, joy….
pfft, try not to go too hard on you? I took it as far as to say "Are you sure that's right?", I'm not sure I can get nicer than that :P