Follow us on Twitter!
It is the path of least resistance that makes rivers and men crooked. - Bj Palmer
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 31
Guests Online: 25
Members Online: 6

Registered Members: 82895
Newest Member: kevy90
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 18-01-10 18:20
YO again!

I've just started reading about socket programming in python.

Code
#Python Server
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






Code

#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:

Code
error: [Errno 10049] The requested address is not valid in its context




This should not happen :/

Google says:
Code
This 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 -_-

Edited by Demons Halo on 18-01-10 21:45
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 18-01-10 21:39
thnx for the replay mosh =)

fact is that my code works if i use "localhost" as the address. but once I try to reach another computer within the LAN, I get an error.
This is a firewall issue right? or do I need to do something different when I code LAN applications?

Edited by Demons Halo on 18-01-10 21:39
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 18-01-10 21:45
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 Pfft nothing fancy xD
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 18-01-10 22:02
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:

Code
#Python Server, running on my stationary PC
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






Code
#Python Client, running on my laptop
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!

Edited by Demons Halo on 18-01-10 22:03
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 18-01-10 22:10
or 0.0.0.0 should work


Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 18-01-10 22:21

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:

Code
I'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
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 24-02-10 19:07
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.
kbcarte.wordpress.com
Author

RE: Python sockets

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 25-02-10 00:51
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.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: Python sockets


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 25-02-10 01:41
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.wikiped. . .#TCP_ports


Author

RE: Python sockets

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 25-02-10 03:26
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....
kbcarte.wordpress.com
Author

RE: Python sockets

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 25-02-10 04:08
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 Pfft


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com