-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
128 lines (81 loc) · 3.15 KB
/
Server.py
File metadata and controls
128 lines (81 loc) · 3.15 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
import socket
import sys
from thread import *
c= raw_input('ENTER T for tcp , U for UDP servers\n')
if c=='T':
HOST = '' # Symbolic name meaning all available interfaces
PORT = 5558 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
#Start listening on socket
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn,addr):
#infinite loop so that function do not terminate and thread do not end.
while 1:
#Receiving from client
try:
data =conn.recv(1024)
if not data:
break
data1,data2=data.split(',')
data3=int(data1) + int(data2)
reply=str(data3)
conn.sendall(reply)
except:
sys.exit()
#came out of loop
conn.close()#fin from server
print 'connection with ' +str(addr)+ ' closed\n'
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])+'\n'
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,addr[1],))
elif c=='U':
HOST = '' # Symbolic name meaning all available interfaces
PORT = 6667 # Arbitrary non-privileged port
# Datagram (udp) socket
try :
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print 'Socket created'
except socket.error, msg :
print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
# Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
def clientthread(d):
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data=d[0]
addr=d[1]
data1,data2=data.split(',')
data3=int(data1) + int(data2)
reply=str(data3)
s.sendto(reply , addr)
while 1:
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
d =s.recvfrom(1024)
if not d:
break
addr=d[1]
print d
start_new_thread(clientthread, (d,))
else:
print 'invalid option'