Created
July 31, 2013 18:36
-
-
Save nehaljwani/6124816 to your computer and use it in GitHub Desktop.
TCP and UDP Listening On Same Port
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
udp_server.py | |
import socket | |
UDP_IP = "10.1.39.241" | |
UDP_PORT = 6005 | |
sock = socket.socket(socket.AF_INET, # Internet | |
socket.SOCK_DGRAM) # UDP | |
sock.bind((UDP_IP, UDP_PORT)) | |
while True: | |
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes | |
sock.sendto(data, addr) | |
print "received message:", data | |
tcp_server.py | |
import socket | |
TCP_IP = '10.1.39.241' | |
TCP_PORT = 6005 | |
BUFFER_SIZE = 20 # Normally 1024, but we want fast response | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((TCP_IP, TCP_PORT)) | |
s.listen(1) | |
while 1: | |
conn, addr = s.accept() | |
print 'Connection address:', addr | |
while 1: | |
data = conn.recv(BUFFER_SIZE) | |
if not data: break | |
print "received data:", data | |
conn.send(data) # echo | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment