🤬
  • ■ ■ ■ ■ ■
    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.
  • ■ ■ ■ ■ ■ ■
    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 + 
Please wait...
Page is in error, reload to recover