-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode2.py
71 lines (49 loc) · 1.84 KB
/
node2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python3 # This is client.py file
import socket
import threading
import time
def server(IP, Port):
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSock.bind((IP, Port))
while True:
data, addr = serverSock.recvfrom(1024)
print "Message: ", data
def client(Port, RightPort, LeftPort):
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto("hi from " + str(Port), ("127.0.0.6", RightPort))
clientSock.sendto("hi from " + str(Port), ("127.0.0.42", LeftPort))
def main():
# creating thread
t1 = threading.Thread(target=server, args=("127.0.0.5", 5050))
t2 = threading.Thread(target=client, args=(5050, 5060, 5000))
# starting thread 1
t1.start()
# starting thread 2
time.sleep(5)
t2.start()
t1.join()
t2.join()
'''Class Node:
def __init__(self, NodeId, NodeHost, NodePort, clockwiseNeighbor, antiClockwiseNeighbor):
self.NodeId=NodeId
self.IP=NodeHost
self.port=NodePort
self.clockwiseNeighbor=clockwiseNeighbor
self.antiClockwiseNeighbor=antiClockwiseNeighbor
def joinRing(self, rightnode): #raise RingException
# create a socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind to the port
serversocket.bind((self.IP, self.port))
# queue up to 5 requests
serversocket.listen(5)
while True:
# establish a connection
clientsocket,addr = serversocket.accept()
#create client socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def sendMessageClockwise(self, message): #raise RingException
def sendMessageCounterClockwise(self, message) #raise RingException '''
if __name__== "__main__":
main()