diff --git a/server.py b/server.py index 55fadf0..f36349c 100644 --- a/server.py +++ b/server.py @@ -50,18 +50,26 @@ def newConnections(socket): print("New connection at ID " + str(connections[len(connections) - 1])) total_connections += 1 -def main(): - #Get host and port - host = input("Host: ") - port = int(input("Port: ")) +#Get host +try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("8.8.8.8", 80)) + host = (s.getsockname()[0]) + print('Host: ' + host) + s.close() +except OSError: + print('Unable to reach network and find IP.') + host = input('Please enter host manually: ') - #Create new server socket - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.bind((host, port)) - sock.listen(5) +#Get port +port = int(input("Port: ")) - #Create new thread to wait for connections - newConnectionsThread = threading.Thread(target = newConnections, args = (sock,)) - newConnectionsThread.start() +#Create new server socket +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +sock.bind((host, port)) +sock.listen(5) + +#Create new thread to wait for connections +newConnectionsThread = threading.Thread(target = newConnections, args = (sock,)) +newConnectionsThread.start() -main()