naoussa
naoussa

Reputation: 125

ipv6 python sockets not working ! OSError: [Errno 22] Invalid argument

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 -6 fe80::cbdd:d3da:5194:99be%eth1 2020 and connect to it.

Server code:

    #!/usr/bin/env python3
from socket import *
from time import ctime

HOST='::'
PORT = 2020
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET6, SOCK_STREAM)
##tcpSerSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
        print('Waiting for connection...')

        tcpCliSock, addr = tcpSerSock.accept()
        print('...connected from:', addr)

        while True:
                data = tcpCliSock.recv(BUFSIZ)
                if not data:
                        break
                tcpCliSock.send(('[%s] %s'%(bytes(ctime(), 'utf-8'), data)).encode('utf-8'))
        tcpCliSock.close()
tcpSerSock.close()

client code:

    #!/usr/bin/python3
from socket import *





def tcp_ipv6():
    HOST = 'fe80::cbdd:d3da:5194:99be%eth1'
    PORT = 2020
    ADDR = (HOST, PORT)
    BUFSIZ = 1024
    
    sock = socket(AF_INET6, SOCK_STREAM)
    sock.connect(ADDR)
    
    while True:
        data = input('> ')
        if not data:
            break
        sock.send(data)
        response = sock.recv(BUFSIZ)
        if not response:
            break
        print(response.decode('utf-8'))
    sock.close()        
   
tcp_ipv6() 

When I run the client code I get:

Traceback (most recent call last):
  File "client.py", line 44, in <module>
    tcp_ipv6() 
  File "client.py", line 31, in tcp_ipv6
    sock.connect(ADDR)
OSError: [Errno 22] Invalid argument

Edit1: Thanks to Establishing an IPv6 connection using sockets in python 4-tuple for AF_INET6

ADDR = (HOST, PORT, 0, 0)
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
sock.connect(ADDR)

Still having the same error

Any idea? Thanks in advance

Upvotes: 1

Views: 1343

Answers (2)

Shubhangi
Shubhangi

Reputation: 2264

The higher level function - create_connection , to connect to port works in such case. Sample scriptlet is given as follows. Though why sock.connect fails needs to be identified.

HOST = "xxxx::xxx:xxxx:xxxx:xxxx%en0"
PORT = 2020
ADDR = (HOST, PORT)
BUFSIZ = 1024

sock=create_connection(ADDR)

Upvotes: 0

bobveringa
bobveringa

Reputation: 270

Some parts of your question have been asked before.

Establishing an IPv6 connection using sockets in python

However, it is not the entire reason why it is not working correctly. If you look at your IPv6 address. fe80::cbdd:d3da:5194:99be%eth1 You can see the %eth1 at the end. That is not part of the internet address. Change HOST to HOST = 'fe80::cbdd:d3da:5194:99be'. And it should work.

I would also like to point out another error in your code. You are attempting to send a string (received from input) over the socket. However, this method only accepts byte like objects. You can add data = data.encode('utf-8') to fix this.

Upvotes: 1

Related Questions