-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver1.py
More file actions
125 lines (116 loc) · 2.25 KB
/
server1.py
File metadata and controls
125 lines (116 loc) · 2.25 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
import sys
import socket # Import socket module
from _thread import *
import threading
s = socket.socket()
host = '172.16.20.144'
port = 1234
data = ""
data1=" "
fla=0
flag=0
name=[]
sd=[]
first=0
def on_new_client(conn,addr):
global data
global flag
global fla
global data1
global first
temp=1
nm=conn.recv(1024)
nm=nm.decode()
sd.append(nm)
dat=conn.recv(1024)
dat=dat.decode()
if dat=="1":
while True:
if first == 0:
if temp==1 and flag == 0:
lock.acquire()
data=conn.recv(1024)
data=data.decode()
print(data)
lock.release()
temp=0
flag=1
first=1
elif first == 2:
if temp==1 and flag == 0:
lock.acquire()
data=conn.recv(1024)
data=data.decode()
print(data)
lock.release()
temp=0
flag=1
elif temp == 0 and flag == 0:
conn.send(data.encode('utf-8'))
temp=1
elif dat=="2":
lock.acquire()
data1=conn.recv(1024)
data1=data1.decode()
lock.release()
fla=1
def on_new_client1(conn1,addr):
global data
global flag
global fla
global data1
global firs
t tempp=0
nm=conn1.recv(1024)
nm=nm.decode()
sd.append(nm)
name.append(conn1)
dt=conn1.recv(1024)
dt=dt.decode()
if dt=="1":
while True:
if tempp==0 and flag==1 and first==1:
print("xx")
st=data.split(":")
print(st[1])
if st[0] in sd:
conn1.send(st[1].encode('utf-8'))
tempp=1
else:
print("name not found")
break
elif tempp == 0 and flag == 1 and first == 2:
conn1.send(data.encode('utf-8'))
tempp=1
elif tempp==1:
lock.acquire()
data=conn1.recv(1024)
data=data.decode()
print(data)
lock.release()
tempp=0
flag=0
first=2
else:
while True:
if fla==1:
for i in name:
print(i)
i.send(data1.encode('utf-8'))
fla=0
lock=threading.Lock()
print ('Server started!')
print ('Waiting for clients...')
s.bind((host, port))
s.listen(5)
print ('Got connection from')
while True:
c, addr = s.accept()
print("connected with"+addr[0]+":"+str(addr[1]))
x=c.recv(1024)
x=x.decode()
if x=='1':
start_new_thread(on_new_client,(c,addr))
elif x=='2':
start_new_thread(on_new_client1,(c,addr))
s.close()