1

The UDP server:

# -*- coding: utf-8 -*-
#!/usr/bin/python3
#server UDP

from socket import *

def main():
    # Cria host e port number
    host = ""
    port = 5000

    # Cria socket                  #UDP
    server = socket(AF_INET, SOCK_DGRAM)

    # Indica que o servidor foi iniciado
    print("Servidor iniciado")

    # Bloco infinito do servidor
    while True:
        # Recebe a data e o endereço da conexão
        print("server.recvfrom(1024)",server.recvfrom(1024))
        data, endereço = server.recvfrom(1024)

        # Imprime as informações da conexão
        print("Menssagem recebida de", str(endereço))
        print("Recebemos do cliente:", str(data))

        # Vamos mandar de volta a menssagem em eco
        resposta = "Eco=>" + str(data)
        server.sendto(data, endereço)

    # Fechamos o servidor
    server.close()

if __name__ == '__main__':
    main()

The UDP client:

# -*- coding: utf-8 -*-
#!/usr/bin/python3
#client UDP
from socket import *

def main():
    # Cria host e port number
    host = "localhost"
    port = 5000

    # O servidor será um par endereço e port
    server = (host, port)

    # Criamos o socket
    sock = socket(AF_INET, SOCK_DGRAM)
    sock.bind((host, port))

    # Vamos mandar mensagem enquanto a mensagem for diferente de sair (s)
    msg = input("-> ")
    while msg != 's':
        # Mandamos a mensagem através da conexão
        sock.sendto(msg.encode(), server)

        # Recebemos uma resposta do servidor
        data, endereco = sock.recvfrom(1024)

        # Imprimimos a mensagem recebida
        print("Recebida ->", str(data))

        # Podemos mandar mais mensagens
        msg = input("-> ")

    # Fechamos a conexão
    sock.close()

if __name__ == '__main__':
    main()

The codes are working but I am not sure what is the server or the client: Difference between UDP server and UDP client: sock.bind((host, port)) is on the client or server side?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Paul Sigonoso
  • 575
  • 2
  • 5
  • 18
  • 2
    For UDP there is really no "server-client" relationship like with TCP. If multiple programs are communicating to a central program, then the central program could be called "server" and the other programs "clients". Or, if you have a program that provides a *service* then that could be considered a "server" while the other programs that requests this *service* (whatever it might be) could be considered clients. – Some programmer dude Aug 18 '16 at 22:37
  • 1
    @JoachimPileborg, thanks. So, If I call the script that has sock.bind((host, port) "client", would it be wrong? Is it indistinguible? – Paul Sigonoso Aug 18 '16 at 22:47
  • 2
    On the server (receiver) side; see the answer from EJP here: http://stackoverflow.com/questions/6189831/whats-the-purpose-of-using-sendto-recvfrom-instead-of-connect-send-recv-with-ud – VPfB Aug 19 '16 at 09:07

1 Answers1

1

As @VPfB answered, see: What's the purpose of using sendto/recvfrom instead of connect/send/recv with UDP sockets?

The model server/client :

client is the part that iniciate the communication and server is the receiver one.

Client:

# -*- coding: utf-8 -*-
#!/usr/bin/python3

from socket import *

def main():
    # Cria host e port number
    host = "localhost"
    port = 5000

    # O servidor será um par endereço e port
    server = (host, port)

    # Criamos o socket
    sock = socket(AF_INET, SOCK_DGRAM)
    ##sock.bind((host, port)) #server side

    # Vamos mandar mensagem enquanto a mensagem for diferente de sair (s)
    msg = input("-> ")
    while msg != 's':
        # Mandamos a mensagem através da conexão
        sock.sendto(msg.encode(), server) #encode para enviar no formato de bytes

        # Recebemos uma respota do servidor
        data, endereco = sock.recvfrom(1024)

        # Imprimimos a mensagem recebida
        print("Recebida ->", str(data))

        # Podemos mandar mais mensagens
        msg = input("-> ")

    # Fechamos a conexão
    sock.close()

if __name__ == '__main__':
    main()

Server:

# -*- coding: utf-8 -*-
#!/usr/bin/python3


from socket import *

def main():
    # Cria host e port number
    host = ""
    port = 5000

    # Cria socket                  #UDP
    server = socket(AF_INET, SOCK_DGRAM)
    server.bind((host, port))


    # Indica que o servidor foi iniciado
    print("Servidor iniciado")

    # Bloco infinito do servidor
    while True:
        # Recebe a data e o endereço da conexão
        print("server.recvfrom(1024)",server.recvfrom(1024))
        data, endereço = server.recvfrom(1024)

        # Imprime as informações da conexão
        print("Menssagem recebida de", str(endereço))
        print("Recebemos do cliente:", str(data))

        # Vamos mandar de volta a menssagem em eco
        resposta = "Eco=>" + str(data)
        server.sendto(data, endereço)

    # Fechamos o servidor
    server.close()

if __name__ == '__main__':
    main()
Community
  • 1
  • 1
Ed S
  • 385
  • 8
  • 31