Thread: Sockets???

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    Sockets???


    Okay I am gonna get a book on this tomorrow, but till then I have a few questions...

    1. How can I recv a sent sring from another computer across the net on a port(9999) and there is a router in between. I would have the IP and PORT but how do i send omething that the other recvs something?
    2. Does by sockets, in a network(LAN), do u need the outside IP or how do I do it?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Well first of all, to receive a string sent by another user, obviously (I know you know this) you must be connected to them. They can do something such as s.send('Hello, how is it going?'). In order for you to receive that data, you need to be receiving data from the socket, there are two ways you can do this. You can either use (this isn't the best way) the Timer function from the threading module, or you can start a while loop and do s.recv(1024). This will receive the max amount of bytes from the ip you are connected to. Eventually, you may receive a string of data with the value of, 'Hello, how is it going'?). Sockets are actually very simply to control, you just have to know how they work and how to make them work like you want them to.
    PS. I don't think that a router should inferfere with anything.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    well how would I be able to communicate with you for example with a program that i wrote and u have a server/client running. I tried it but the other person never recvieded the message. Because i did not have a connection to the port the client was okay, and i tested it localy
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    if the other person is behind a nat and they are the server then that other person must port foward the port to there machein that way when you connect to that users on there wan ip, or the closes network that attaches you guys. So if the user is on your lan then you will use the lan ip, and if the user is over the net or if there is a nat between you, you connect to whatever there net ip or there nats ip.

    also if your the server and your behind a nat and anouther user is trying to connect to you then you must foward that port to your system.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    Lawrence, Kansas [KU]
    Posts
    1,559
    Rep Power
    15
    test_server.py
    Code:
    import socket
    
    port = 9999
    
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    
    #Accept UDP datagrams, on the gaiven port, from any sender
    
    s.bind(("",port))
    print "waiting on port:", port
    
    while True:
    	#rec
    	data, addr = s.recvfrom(1024)
    	print "Received:", data, "from", addr

    test_client.py

    Code:
    # client
    
    
    import socket
    port = 9999
    host="192.168.10.7" # change to server's IP address
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    
    s.sendto("hey there", (host,port))

    that should work.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Ya well I already have that but with STREAM any ways how do u forward a port to the system?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    well generaly, this changes for every router/nat. But you can usually connect to a web interface on the router, usaully something like 192.168.0.1 or 192.168.1.1. That depends on the class of the router both of those are for class c routers. It also veries for witch port it could be on 80 or 81 usaully, so try something like 192.168.0.1:81 in your browser to see if that works.

    Hopefully you have configured your router before, because if you havn't then you probably have a big security hole. Once you are in your routers config just for something about port forwarding Once there select the port you wish to foward to, then put in the computer you wish to foward the port too.

    It is hard to explain because so meny routers are diff, sorry that i can't give you a step by step. Possible if you post your brand of routers it someone here has it or i have used it and i can tell you more info about that brand specifly
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    it is gonna be every type if that helps u :P

IMN logo majestic logo threadwatch logo seochat tools logo