■ ■ ■ ■ ■ ■
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 | + | |