-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcServer.py
More file actions
110 lines (88 loc) · 3 KB
/
calcServer.py
File metadata and controls
110 lines (88 loc) · 3 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
# Import socket module
from socket import *
from threading import Thread
import sys # In order to terminate the program
FLAG = False # this is a flag variable for checking quit
# function for receiving message from client
Number = 0
def recv_from_client(conn):
global FLAG
global Number
while True:
try:
# Receives the request message from the client
message = conn.recv(1024).decode()
# if 'q' is received from the client the server quits
if message == 'q':
conn.send('q'.encode())
print('Client', 'Closing connection')
conn.close()
conn.send(message.encode())
return False
else:
print('Client', 'sends:', message)
message = str(eval(message))
conn.send(message.encode())
return True
except:
conn.close()
return False
# function for receiving message from client
def send_to_client(conn):
global FLAG
try:
send_msg = input('Type Message: ')
# the server can provide 'q' as an input if it wish to quit
if send_msg == 'q':
conn.send('q'.encode())
print('Closing connection')
conn.close()
FLAG = True
conn.send(send_msg.encode())
except:
conn.close()
def KeepCalculate(conn):
while True:
temp = recv_from_client(conn)
if temp == False:
break
# this is main function
def main():
global FLAG
global Number
# TODO (1) - define HOST name, this would be an IP address or 'localhost' (1 line)
HOST = 'localhost'
# TODO (2) - define PORT number (1 line) (Google, what should be a valid port number)
# make sure the ports are not used for any other application
serverPort = 12345
# Create a TCP server socket
#(AF_INET is used for IPv4 protocols)
#(SOCK_STREAM is used for TCP)
# TODO (3) - CREATE a socket for IPv4 TCP connection (1 line)
serverSocket = socket(AF_INET, SOCK_STREAM)
# Bind the socket to server address and server port
# TODO (4) - bind the socket for HOST and serverPort (1 line)
serverSocket.bind((HOST, serverPort))
# Listen to at most 1 connection at a time
# TODO (5) - listen and wait for request from client (1 line)
serverSocket.listen(5)
# Server should be up and running and listening to the incoming connections
print('The chat server is ready to connect to a chat client')
# TODO (6) - accept any connection request from a client (1 line)
# This is the loop for chating, the connection is already made with the
# client now the server and client will be able to chat with each other
while True:
connectionSocket, addr = serverSocket.accept()
print('Server is connected with a chat client\n')
# call the receive function
client = Thread(target=KeepCalculate, args=(connectionSocket,))
client.start()
Number = Number + 1
# this condition checks if anyone has pressed 'q' or not
# closing serverScoket before exiting
serverSocket.close()
#Terminate the program after sending the corresponding data
sys.exit()
# This is where the program starts
if __name__ == '__main__':
main()