Skip to content

Instantly share code, notes, and snippets.

@ameenkhan07
Forked from dufferzafar/client.py
Last active August 29, 2015 14:19
Show Gist options
  • Save ameenkhan07/955bb3514c48c1880fe3 to your computer and use it in GitHub Desktop.
Save ameenkhan07/955bb3514c48c1880fe3 to your computer and use it in GitHub Desktop.
import sys
from socket import socket, AF_INET, SOCK_DGRAM
SERVER_IP = '127.0.0.1'
PORT_NUMBER = 5000
SIZE = 1024
print("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket(AF_INET, SOCK_DGRAM)
while True:
# Get Input & Send to Server
text = raw_input("Enter some text:")
mySocket.sendto(text, (SERVER_IP, PORT_NUMBER))
# Recieve Reply & Print
(data, addr) = mySocket.recvfrom(SIZE)
print data
sys.exit()
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname('0.0.0.0')
mySocket = socket(AF_INET, SOCK_DGRAM)
mySocket.bind((hostName, PORT_NUMBER))
print("Test server listening on port {0}\n".format(PORT_NUMBER))
while True:
# Recieve data from client
(data, addr) = mySocket.recvfrom(SIZE)
print data
# Calculate and send
ones = data.count("1")
zeroes = data.count("0")
mySocket.sendto("Number of 1s is %d, while 0s is %d" % (ones, zeroes), addr)
sys.ext()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment