Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tcp sever HELP
#1
Hi have a chat client that works excellent, but i can't have other computers that are on the same router connect to me, so i try to change the address assigned from 0.0.0.0 to my public ip address (searched via google) and it keeps giving me error 49: can't assign the address... can anyone explain what i need to do to set my public address on here and it be able to work?
(ip address on line 27 to make your searching easier. )
server 
# Tcp Chat server
  
import socket, select
  
#Function to broadcast chat messages to all connected clients
def broadcast_data (sock, message):
    #Do not send the message to master socket and the client who has send us the message
    for socket in CONNECTION_LIST:
        if socket != server_socket and socket != sock :
            try :
                socket.send(message)
            except :
                # broken socket connection may be, chat client pressed ctrl+c for example
                socket.close()
                CONNECTION_LIST.remove(socket)
  
if __name__ == "__main__":
      
    # List to keep track of socket descriptors
    CONNECTION_LIST = []
    RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
    PORT = 5432
      
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # this has no effect, why ?
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((“0.0.0.0”, PORT))
    server_socket.listen(10)
  
    # Add server socket to the list of readable connections
    CONNECTION_LIST.append(server_socket)
  
    print "Chat server started on port " + str(PORT)
  
    while 1:
        # Get the list sockets which are ready to be read through select
        read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
  
        for sock in read_sockets:
            #New connection
            if sock == server_socket:
                # Handle the case in which there is a new connection recieved through server_socket
                sockfd, addr = server_socket.accept()
                CONNECTION_LIST.append(sockfd)
                print "Client (%s, %s) connected" % addr
                  
                broadcast_data(sockfd, "[%s:%s] entered room\n" % addr)
              
            #Some incoming message from a client
            else:
                # Data recieved from client, process it
                try:
                    #In Windows, sometimes when a TCP program closes abruptly,
                    # a "Connection reset by peer" exception will be thrown
                    data = sock.recv(RECV_BUFFER)
                    if data:
                        broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)                
                  
                except:
                    broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                    print "Client (%s, %s) is offline" % addr
                    sock.close()
                    CONNECTION_LIST.remove(sock)
                    continue
      
    server_socket.close()
client 
# telnet program example
import socket, select, string, sys
 
def prompt() :
    sys.stdout.write('<You> ')
    sys.stdout.flush()
 
#main function
if __name__ == "__main__":
     
    if(len(sys.argv) < 3) :
        print 'Usage : python telnet.py hostname port'
        sys.exit()
     
    host = sys.argv[1]
    port = int(sys.argv[2])
     
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
     
    # connect to remote host
    try :
        s.connect((host, port))
    except :
        print 'Unable to connect'
        sys.exit()
     
    print 'Connected to remote host. Start sending messages'
    prompt()
     
    while 1:
        socket_list = [sys.stdin, s]
         
        # Get the list sockets which are readable
        read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
         
        for sock in read_sockets:
            #incoming message from remote server
            if sock == s:
                data = sock.recv(4096)
                if not data :
                    print '\nDisconnected from chat server'
                    sys.exit()
                else :
                    #print data
                    sys.stdout.write(data)
                    prompt()
             
            #user entered a message
            else :
                msg = sys.stdin.readline()
                s.send(msg)
                prompt()
do i need to change the port??? or should people be  able to join regardless if the ip address is 0.0.0.0?

my main goal is to set this up so at school my friends can join the chat server and be able to talk. we all are connected to the same network at all times... I'm new to this so if you can fix and explain would be best... or if u can't fix, just try to be descriptive on what i should do.
Reply
#2
If you searched google, you didn't find your ip address. You found the ip address of the router you're connected through. You can't bind to your router's ip address, since that's a different device. To find your actual ip address, on windows, open a console and run ipconfig.

The problem you'll find, is that you can run your script on your computer, but nobody outside your gateway/local intranet can access it. To do THAT, you'd need to enable port forwarding on the router, or look into udp/tcp hole punching (which I think involves having an external source already that you connect to, and THAT just forwards all requests to you).
Reply
#3
This is the router causing it.
The address 0.0.0.0 means that your server could receive a connection from the internet. Not only local. It's the most obvious address you can use.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Via pm:
abrew132 Wrote:how do i port forward? I'm not that far in python yet so idk how

Port forwarding is unrelated to python, and is just a networking thing. https://en.wikipedia.org/wiki/Port_forwarding
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creation of an application for lora sever network moez 3 2,892 Apr-18-2019, 11:05 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020