1

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

naoussa
  • 107
  • 1
  • 8

2 Answers2

1

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.

bobveringa
  • 230
  • 2
  • 12
  • If a similar question already exists, please flag or vote it as a duplicate. – MisterMiyagi Jun 08 '21 at 08:55
  • Thank you for your reply @bobveringa, It didn't work, I still have the same error, I changed ADDR to `ADDR = (HOST, PORT, 0, 0)` and `sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)` – naoussa Jun 08 '21 at 09:03
  • @naoussa You had another error in your code, I've updated the answer. I ran it on my machine, and it seems to work now. – bobveringa Jun 08 '21 at 09:24
  • @bobveringa Thank you, your last edit, data = data.encode('utf-8'), was helpful. – naoussa Jun 08 '21 at 10:50
0

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)
Shubhangi
  • 2,229
  • 2
  • 14
  • 14
  • new error when I launch client on raspberry trying to connect on the server launched on ubunto `Traceback (most recent call last): File "client.py", line 48, in tcp_ipv6() File "client.py", line 32, in tcp_ipv6 sock=create_connection(ADDR) File "/usr/lib/python3.7/socket.py", line 707, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known ` – naoussa Jun 08 '21 at 16:18