Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()