xxmbabanexx
xxmbabanexx

Reputation: 8706

How to transfer data over web using sockets?

Question

How to create a socket server which works over the web?

Is it simply a matter of correctly port forwarding, or will I have to buy a domain?

If I need a domain, is there any free way to do this?

Background

I am currently working on a basic IM project using Python 2.7.3 on Windows 7 (32 bit).

I want to send messages from the console to one of my friend's computers, using Python. This has been invaluable, but it only works within one machine.

In my attempts to send messages over the internet, I have tried using several strategies.

  1. Since I will ultimately be using my Mac as the server, I have employed Port Map to port forward different ports. When I do this, Python responds by giving me this error message:

    Enter the PORT number (1 - 10,000)4235
    Socket Created
    Bind failed. Error Code : 49 Message Can't assign requested address
    
    Traceback (most recent call last):
       File "/Users/BigKids/Desktop/Server v3.py", line 18, in <module>
          sys.exit()
      SystemExit 
    
  2. I have tried to simply put my IP address in as the port but it responds with an error message:

    Please enter the host's IP >>> 68.***.***.128
    Enter the PORT number (1 - 10,000)2432
    Socket Created
    Bind failed. Error Code : 10049 
    Message The requested address is not valid in its context
    
    Traceback (most recent call last):
      File "D:\Python Programs\Sockets\First Project\Server v3.py", line 19, in   <module>
        sys.exit()
      SystemExit
    

    Here is the necessary code:

    HOST = raw_input("Please enter the host's IP >>> ")
    
    PORT = input ("Enter the PORT number (1 - 10,000)")
    
    
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    print "Socket Created"
    
    try:
        s.bind((HOST, PORT))
    except socket.error, msg:
        print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1])
        sys.exit()
    

Upvotes: 1

Views: 2673

Answers (1)

luc
luc

Reputation: 43146

Your example code should be able to work on the Internet. On the programming point of view, there is no big difference between something working on LAN or on the Internet.

However, if you want to connect to socket over the Internet, the listening socket must be bound to a routable public IP address.

If your friend's computer is just a PC on another LAN, it is very likely that it doesn't have a public IP address and as a consequence, it is not possible for you to connect.

One solution can be to make something similar to this chat example and to run the server.py on an Internet server (with a public IP address). You and your friend would both run the client.py.

Upvotes: 1

Related Questions