Created
February 17, 2014 23:51
-
-
Save drgarcia1986/9061705 to your computer and use it in GitHub Desktop.
Classe feita em Python que encapsula um cliente UDP enviando e recebendo mensagens de um servidor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'diego.garcia' | |
import socket | |
class UDPClient: | |
__IP = '255.255.255.255' | |
__PORT = 49152 | |
__udp_cli = '' | |
def __init__(self): | |
self.__udp_cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
self.__udp_cli.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) | |
self.__udp_cli.settimeout(5000) | |
def send_msg(self, msg): | |
self.__udp_cli.sendto(bytes(msg, "utf-8"), (self.__IP, self.__PORT)) | |
return self.__udp_cli.recv(256).decode("UTF-8") | |
def set_port(self, port): | |
self.__PORT = port | |
def close(self): | |
self.__udp_cli.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'diego.garcia' | |
import udp_client | |
udpClient = udp_client.UDPClient() | |
print('Para sair use "fim"') | |
msg = input('Digite a mensagem a ser enviada\n') | |
while msg != 'fim': | |
print(udpClient.send_msg(msg)) | |
msg = input('Digite a mensagem a ser enviada\n') | |
udpClient.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment