This commit is contained in:
2021-03-14 20:59:45 +01:00
parent e383a73425
commit 9a402a9312
5 changed files with 116 additions and 6 deletions

View File

@@ -1 +1,35 @@
#!/usr/bin/env python3
#!/usr/bin/env python3
import socket
# socket heeft een listening and accept method
SERVER = "127.0.0.1" #dynamisch fixen in project
PORT = 5055
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ADDR = (SERVER, PORT) # hier wordt de socket gebonden aan mijn IP adres, dit moet wel anders
server.bind(ADDR) # in het project gebeuren
HEADER = 64 # maximum size messages
FORMAT = 'utf-8' # recieving images through this format does not work
DISCONNECT_MESSAGE = "DISCONNECT!" # special message for disconnecting client and server
# function for starting server
def start():
pass
server.listen()
while True: # infinite loop in which server accept incoming connections, we want to run it forever
conn, addr = server.accept() # Server blocks untill a client connects
print("new connection: ", addr[0], " connected.")
connected = True
while connected: # while client is connected, we want to recieve messages
msg = conn.recv(HEADER).decode(FORMAT).rstrip() # Argument is maximum size of msg (in project look into details of accp), decode is for converting bytes to strings, rstrip is for stripping messages for special hidden characters
print("message: ", msg)
if msg == DISCONNECT_MESSAGE:
connected = False
print("close connection ", addr[0], " disconnected.")
conn.close()
print("server is starting ... ")
start()