Skip to content

Instantly share code, notes, and snippets.

@huberflores
Last active October 30, 2018 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huberflores/6a5ecee3ef4920d16b4c0cb1c737bb6f to your computer and use it in GitHub Desktop.
Save huberflores/6a5ecee3ef4920d16b4c0cb1c737bb6f to your computer and use it in GitHub Desktop.
# From socket module we import the required structures and constants.
from socket import AF_INET, SOCK_DGRAM, socket
if __name__ == '__main__':
print 'Application started'
# Creating a UDP/IP socket
s = socket(AF_INET, SOCK_DGRAM)
# Binding the UDP/IP socket to address and port
s.bind(('127.0.0.1',7778))
print 'Socket is bound to %s:%d' % s.getsockname()
# Receiving the message, maximal payload to receive in one peace
recv_buffer_length = 1024
print 'Waiting for message ...'
message,source = s.recvfrom(recv_buffer_length)
print 'Received message from %s:%d' % source
print 'Payload lengh %d bytes: [%s]' % (len(message),message)
raw_input('Press Enter to teminate ...')
print 'Closing the UDP socket ...'
s.close()
print 'Terminating ...'
# From socket module we import the required structures and constants.
from socket import AF_INET, SOCK_DGRAM, socket
if __name__ == '__main__':
print 'Application started'
# Creating a UDP/IP socket
s = socket(AF_INET, SOCK_DGRAM)
# Binding the UDP/IP socket to address and port
s.bind(('127.0.0.1',7777))
print 'Socket is bound to %s:%d' % s.getsockname()
# Sending the message
message = 'Hell world!'
destination = ('127.0.0.1',7778)
s.sendto(message,destination)
print 'Sent message to %s:%d' % destination
print 'Payload lengh %d bytes: [%s]' % (len(message),message)
raw_input('Press Enter to teminate ...')
print 'Closing the UDP socket ...'
s.close()
print 'Terminating ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment