-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.py
More file actions
156 lines (128 loc) · 5.08 KB
/
echo_server.py
File metadata and controls
156 lines (128 loc) · 5.08 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import socket
import ipaddress
import sys
import select
import threading
import queue
import time
import os
from threading import Thread
#initiliaze IP, port and name
SIZE = 1024
PORT = int(sys.argv[1])
NAME = sys.argv[2]
COUNT = 0
ip = socket.gethostbyname(socket.gethostname())
HOST = ip
openservers = []
openservers.append(HOST)
respondservers = []
respondservers.append(HOST)
#listen UDP packets
def listenUDP():
global COUNT
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', PORT))
while True:
data, addr = s.recvfrom(SIZE)
if data:
data = str(data)
if data[2:10] == 'discover':
# parsing if the received data is a discover
bracIndex = data.find('[')
bracStop = data.find(',', bracIndex)
otherName = data [bracIndex + 1:bracStop]
otherIPStop = data.find(',', bracStop + 1)
otherIP = data[bracStop + 1:otherIPStop]
duplicate = False
for x in openservers:
if (x == otherIP):
duplicate = True
if not duplicate and HOST != otherIP:
print('From ', otherIP,', ', otherName + ' : DISCOVER ME!')
openservers.append(otherName)
resp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
resp.settimeout(2)
COUNT = COUNT + 1
if COUNT > 2:
openservers.clear()
respondservers.clear()
openservers.append(HOST)
respondservers.append(HOST)
COUNT = 0
resp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
resp.settimeout(2)
try:
if HOST != otherIP:
resp.connect((addr[0], PORT))
msg = 'response,broadcast TCP,[' + NAME + ',' + HOST + ',' + 'response' + ']'
msg = msg.encode('ascii')
resp.sendall(bytes(msg))
resp.close()
except:
print('err 2')
#listen TCP packets
def listenTCP():
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.bind(('',PORT))
while True:
tcp.listen()
conn, addr = tcp.accept()
data = conn.recv(SIZE)
if data:
data = str(data)
if data [2:9] == 'message':
# parsing if the received data is a massage
nameIndex = data.find('[')
nameStop = data.find(',', nameIndex)
name = data [nameIndex + 1:nameStop]
msgIndex = data.find(',', nameStop+1 )
msgIndex = data.find(',', msgIndex+1 )
msgStop = data.find(']', msgIndex+1 )
msg = data[msgIndex + 1 : msgStop]
print('From ', addr,', ', name + ' : ' + msg)
elif data [2:10] == 'response':
# parsing if the received data is a response
nameIndex = data.find('[')
nameStop = data.find(',', nameIndex)
name = data [nameIndex + 1:nameStop]
otherIPStop = data.find(',', nameStop + 1)
otherIP = data[nameStop + 1:otherIPStop]
duplicate = False
for x in respondservers:
if (x == otherIP):
duplicate = True
if not duplicate and name != NAME:
print('From ', otherIP,', ', name + ' : RESPONDED')
respondservers.append(otherIP)
def main():
global HOST
ThreadUDP = Thread(target=listenUDP)
ThreadTCP = Thread(target=listenTCP)
ThreadUDP.start()
ThreadTCP.start()
# try to send discover message to open servers
try:
ann = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ann.bind(('',0))
ann.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
#ann.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
msg = 'discover,broadcast UDP,[' + NAME + ',' + HOST + ',' + 'announce' + ']'
msg = msg.encode('ascii')
#broadcast 3 times to cover up losses
for x in range(3):
ann.sendto(msg,('<broadcast>', PORT))
bcast = '192.168.1.'
#for some reason the sendto() function with <broadcast> is not sent to
#all ip addresses accross the network,
#so it is done with the for loop below
for y in range(256):
bcastTO = bcast + str(y)
for x in range(3):
ann.sendto(msg,(bcastTO, PORT))
ann.close()
except socket.error as socketerror:
print('Error happened :(')
print('Your server is now open on : ', HOST,':', PORT)
if __name__ == ("__main__"):
main()