How do you make a socket that actually connects to other computers?

0

I tried to make a socket in Python 3.5.1, but when I went to test it, it only connects with me. I tried connecting to another computer (from my friend), but it does not connect. Does anyone know how to make a socket that actually connects?

If anyone wants to see, here is my problematic socket: link

#Cliente
import socket
HOST = 'Aqui eu coloco o ip'
PORT = 5000
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dest = (HOST, PORT)
tcp.connect(dest)
print('Para sair use CTRL+X\n')
msg = input()
while msg != '\x18':
    tcp.send(msg)
    msg = input()
tcp.close()
#Servidor
import socket
HOST = ''
PORT = 5000
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (HOST, PORT)
tcp.bind(orig)
tcp.listen(1)
while True:
    con, cliente = tcp.accept()
    print('Concetado por', cliente)
    while True:
        msg = con.recv(1024)
        if not(msg): break
        print(cliente, msg)
    print('Finalizando conexao do cliente', cliente)
    con.close()
    
asked by anonymous 13.07.2016 / 02:06

1 answer

2

Here it worked perfectly well, even running the server on a computer on the Internet, and using a named HOST instead of IP on the client.

Is the server that you tried pinging? Sometimes computers on the same network are not accessible, either because of access point problems (solving a disconnect), or because the access point has isolation enabled (it prevents a computer from connecting to another from the same network for security reasons , as in public Wi-Fi).

The only problem I faced when typing something for the client to send was the following:

Traceback (most recent call last):
  File "cli", line 11, in <module>
    tcp.send(msg)
TypeError: a bytes-like object is required, not 'str'

This is solved by changing the tcp.send line (msg) to

tcp.send(bytes(msg, 'utf-8'))
    
26.08.2016 / 21:17