🤬
  • ■ ■ ■ ■ ■
    Projects/0_TCP_server/0_TCP_server
     1 + 
  • ■ ■ ■ ■ ■ ■
    Projects/0_TCP_server/README.md
     1 +# TCP server just to receive messages
     2 +>This simple TCP server receives messages and echoes them back to the client. The client program sets up its socket differently from the way a server does. Instead of binding to a port and listening, it uses connect() to attach the socket directly to the remote address.
     3 + 
     4 +NOTE: Make sure that the terminals running the scripts are separate
     5 + 
     6 + 
     7 +![alt text](https://github.com/alisimran/100-redteam-projects/blob/master/Projects/0_TCP_server/tcpserver.png)
     8 + 
     9 +## :information_source: technologies used
     10 + 
     11 +* Python
     12 + 
     13 +## :information_source: How to use?
     14 + 
     15 +```bash
     16 +# Clone the repository
     17 +$ git clone https://github.com/kurogai/100-redteam-projects
     18 + 
     19 +# Enter the repository
     20 +$ cd 100-redteam-projects/0_TCP_server
     21 + 
     22 +# Open a terminal and run
     23 +python ./server.py
     24 + 
     25 +# Open another terminal and run
     26 +python ./client.py
     27 +```
     28 + 
     29 +## :books: References
     30 + https://pymotw.com/2/socket/tcp.html
     31 + 
     32 +## Developer
     33 + https://github.com/alisimran
     34 + 
  • ■ ■ ■ ■ ■ ■
    Projects/0_TCP_server/client.py
     1 +import socket
     2 + 
     3 +# Declaring and initializing local ip address and port to be used
     4 +localIP, localPort = "127.0.0.1", 65432
     5 + 
     6 +#creating a TCP/IP socket
     7 + 
     8 +TCPclientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     9 + 
     10 +TCPclientSocket.connect((localIP, localPort))
     11 + 
     12 + 
     13 + 
     14 +clientMsg = input("Type your message for the server here: ")
     15 +data = bytes(clientMsg, "utf-8")
     16 + 
     17 +# send message to the server using TCP socket
     18 +print("Sending message to {0} port {1}".format(localIP, localPort))
     19 +TCPclientSocket.sendall(data)
     20 + 
     21 +#receiving reply from the server
     22 +dataFromServer = str(TCPclientSocket.recv(1024))
     23 +print("Message received from the server: ", str(dataFromServer))
     24 + 
     25 + 
  • ■ ■ ■ ■ ■ ■
    Projects/0_TCP_server/server.py
     1 +import socket
     2 + 
     3 +serverAddressPort = ("127.0.0.1", 65432)
     4 + 
     5 +bytesToSend = b'Hey there! We received the message.'
     6 + 
     7 +# Creating a TCP/IP socket
     8 +TCPServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     9 + 
     10 +# Binding server socket to the port
     11 +TCPServerSocket.bind(serverAddressPort)
     12 +print("Server up and Listening")
     13 + 
     14 + 
     15 +# Listening for incoming messages
     16 + 
     17 +TCPServerSocket.listen(10)
     18 +msg, address = TCPServerSocket.accept()
     19 + 
     20 +while 1:
     21 + datafromClient = msg.recv(1024)
     22 + msg.sendall(datafromClient)
     23 + 
     24 + 
  • Projects/0_TCP_server/tcpserver.png
Please wait...
Page is in error, reload to recover