🤬
  • ■ ■ ■ ■ ■ ■
    Projects/1_TCP_chat_server/README.md
     1 +# UDP server just to receive messages
     2 + 
     3 +https://user-images.githubusercontent.com/32443720/133366580-3903e6ea-6e81-4f22-b614-2b5f20b9603f.mp4
     4 + 
     5 +## :information_source: Tecnologias Usadas
     6 + 
     7 +* Python
     8 + 
     9 +## :information_source: How use?
     10 +```bash
     11 +# Clone the repository
     12 +$ git clone https://github.com/kurogai/100-redteam-projects
     13 + 
     14 +# Enter the repository
     15 +$ cd 100-redteam-projects/Projects/1_TCP_chat_server
     16 + 
     17 +# Open a terminal and run
     18 +$ python3 TCPserver.py 127.0.0.1 5555
     19 + 
     20 +# Open a new terminal and run
     21 +$ python3 TCPclient.py 127.0.0.1 5555
     22 + 
     23 +# Open a new terminal and run
     24 +$ python3 TCPclient.py 127.0.0.1 5555
     25 +```
     26 + 
     27 +## :books: References
     28 + https://www.neuralnine.com/tcp-chat-in-python/
     29 +
     30 + https://www.geeksforgeeks.org/simple-chat-room-using-python/
     31 + 
  • ■ ■ ■ ■ ■ ■
    Projects/1_TCP_chat_server/TCPclient.py
     1 +import socket
     2 +import threading
     3 +import sys
     4 + 
     5 +# checks whether sufficient arguments have been provided
     6 +if len(sys.argv) != 3:
     7 + print ("Correct usage: script, IP address, port number")
     8 + exit()
     9 + 
     10 +# Choosing Nickname
     11 +nickname = input("Choose your nickname: ")
     12 +
     13 +# takes the first argument from command prompt as IP address
     14 +host = str(sys.argv[1])
     15 +
     16 +# takes second argument from command prompt as port number
     17 +port = int(sys.argv[2])
     18 + 
     19 +# Connecting To Server
     20 +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     21 +client.connect((host, port))
     22 + 
     23 +# Listening to Server and Sending Nickname
     24 +def receive():
     25 + while True:
     26 + try:
     27 + # Receive Message From Server
     28 + # If 'NICK' Send Nickname
     29 + message = client.recv(1024).decode('utf-8')
     30 + if message == 'NICK':
     31 + client.send(nickname.encode('utf-8'))
     32 + else:
     33 + print(message)
     34 + except:
     35 + # Close Connection When Error
     36 + print("An error occured!")
     37 + client.close()
     38 + break
     39 + 
     40 +# Sending Messages To Server
     41 +def write():
     42 + while True:
     43 + message = '{}: {}'.format(nickname, input(''))
     44 + client.send(message.encode('utf-8'))
     45 + 
     46 +# Starting Threads For Listening And Writing
     47 +receive_thread = threading.Thread(target=receive)
     48 +receive_thread.start()
     49 + 
     50 +write_thread = threading.Thread(target=write)
     51 +write_thread.start()
  • ■ ■ ■ ■ ■ ■
    Projects/1_TCP_chat_server/TCPserver.py
     1 +import socket
     2 +import threading
     3 +import sys
     4 + 
     5 +# checks whether sufficient arguments have been provided
     6 +if len(sys.argv) != 3:
     7 + print ("Correct usage: script, IP address, port number")
     8 + exit()
     9 +
     10 +# takes the first argument from command prompt as IP address
     11 +host = str(sys.argv[1])
     12 +
     13 +# takes second argument from command prompt as port number
     14 +port = int(sys.argv[2])
     15 + 
     16 +# Starting Server
     17 +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     18 +server.bind((host, port))
     19 +server.listen()
     20 + 
     21 +# Lists For Clients and Their Nicknames
     22 +clients = []
     23 +nicknames = []
     24 + 
     25 +# Sending Messages To All Connected Clients
     26 +def broadcast(message):
     27 + for client in clients:
     28 + client.send(message)
     29 + 
     30 +# Handling Messages From Clients
     31 +def handle(client):
     32 + while True:
     33 + try:
     34 + # Broadcasting Messages
     35 + message = client.recv(1024)
     36 + broadcast(message)
     37 + except:
     38 + # Removing And Closing Clients
     39 + index = clients.index(client)
     40 + clients.remove(client)
     41 + client.close()
     42 + nickname = nicknames[index]
     43 + broadcast('{} left!'.format(nickname).encode('utf-8'))
     44 + nicknames.remove(nickname)
     45 + break
     46 + 
     47 +# Receiving / Listening Function
     48 +def receive():
     49 + while True:
     50 + # Accept Connection
     51 + client, address = server.accept()
     52 + print("Connected with {}".format(str(address)))
     53 + 
     54 + # Request And Store Nickname
     55 + client.send('NICK'.encode('utf-8'))
     56 + nickname = client.recv(1024).decode('utf-8')
     57 + nicknames.append(nickname)
     58 + clients.append(client)
     59 + 
     60 + # Print And Broadcast Nickname
     61 + print("Nickname is {}".format(nickname))
     62 + broadcast("{} joined!".format(nickname).encode('utf-8'))
     63 + client.send('Connected to server!'.encode('utf-8'))
     64 + 
     65 + # Start Handling Thread For Client
     66 + thread = threading.Thread(target=handle, args=(client,))
     67 + thread.start()
     68 + 
     69 +print('-----Server UP-----')
     70 +receive()
Please wait...
Page is in error, reload to recover