Header Ads

Peer to Peer Chatting using UDP Socket

Program :


Server Side :
import random
from socket import *
from threading import Thread, Lock


def receiver(serverSocket):
global recv_msg
global name
while flag:
'''Receive data from using recvfrom'''
if client_addr == str(address[0]):
if recv_msg == '*quit*':
print name+" is offline"
else:
print "\t\t"+recv_msg



flag = True
'''Declare a socket for UDP'''
serverSocket.bind(('', 12000))

recv_msg, address = serverSocket.recvfrom(1024)
client_addr = str(address[0])
print recv_msg + " is connected from IP:"+str(address[0])+" and Port:"+str(address[1])+"\n"
name = recv_msg

# Start channel receiver thread
recv_thread = Thread(target=receiver, args=(serverSocket,))
recv_thread.daemon = True
recv_thread.start()


while flag:
send_msg = raw_input()

if send_msg == '*quit*':
flag = False
'''Send data to client using sendto'''
else:
'''Send data to client using sendto'''

Client Side :
from socket import *
import time
from threading import Thread, Lock

def receiver(clientSocket):
while flag:
'''Receive data from server using recvfrom'''
print "\t\t"+recv_msg

flag = True

clientSocket = socket(AF_INET, SOCK_DGRAM)
addr = ('127.0.0.1', 12000)
name = str(raw_input("Enter Your Name:"))

clientSocket.sendto(name, addr)
#try:

# Start channel receiver thread
recv_thread = Thread(target=receiver, args=(clientSocket,))
recv_thread.daemon = True
recv_thread.start()

recv_msg = 'NUll'

while recv_msg != '*quit*':
send_msg = raw_input()
if send_msg == '*quit*':
'''Send data to client using sendto'''
flag = False
recv_thread.stop()
else:
'''Send data to client using sendto'''

No comments:

Powered by Blogger.