ipv6 python sockets not working

1.9k views Asked by At

I have a simple client server program and the server side works but for some reason I can't get the the client to interact to the server. I am able to launch the server and use nc -u ::1 50007 and connect to it and it works as intended.

Server code:

import socket
import sys



def main():
    HOST = '::1'
    PORT = 50007
    res = socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
    af, socktype, proto, canonname, sa = res[0]
    try:
        s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
        s.bind(sa)
        while True:
            data, addr = s.recvfrom(1024)
            if not data:
                break
            print 'server received %r from %r' % (data, addr)
            s.sendto(data, addr)
        s.close()
    except socket.error, msg:
        print msg



if __name__ == '__main__':
    main()

Client code:

import socket
import sys

def main():
    HOST = '2015:cc00:bb00:aa00::2'   
    PORT = 50007              
    res = socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM)
    af, socktype, proto, canonname, sa = res[0]
    try:
        s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
        s.connect(sa)
        s.send('Hello, world')
        data, addr = s.recvfrom(1024)
        s.close()
        print 'Received', repr(data)
    except socket.error as e:
        print e

if __name__ == '__main__':
    main()

I am able to ping from client to server, but I can't nc -u 2015:cc00:bb00:aa00::2 50007 either. When I run the client code I get a connection refused error. Not sure why it is not connecting, any ideas?

1

There are 1 answers

1
Doon On BEST ANSWER

The problem is that your server is listening on localhost ::1 but you are trying to connect to 2015:cc00:bb00:aa00::2 which is a different Interface. Try setting HOST = "::" in your server in order to have it bind to all interfaces.