Sunday, April 20, 2025

Sockets with Python - 4 - One Way Chat - Server to Client

Server Script

import socket 

msg = "Meeting at 14h, Office 279"

#Local IP Data

host = "192.168.0.103"
port = 54321
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Preparing for Connection

s.bind((host, port))

print("Server Listening")
s.listen(5)

conn, addr = s.accept()
print("Connection From: ", addr)
print("Sending Message: ")
conn.sendall(msg.encode())

while True:
    inp = input(">>")
    conn.sendall(inp.encode())
  1. import socket - This line imports the socket module, which is a built-in module in Python that provides low-level network communication functions.

  2. host = "192.168.0.103" - This line defines a string variable named host that stores the IP address of the remote host.

  3. port = 54321 - This line defines an integer variable named port that stores the port number to use for the connection.

  4. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - This line creates a new socket object named s using the socket module. The first argument, socket.AF_INET, specifies the address family to use (IPv4), and the second argument, socket.SOCK_STREAM, specifies the socket type to use (TCP).

  5. s.connect((host, port)) - This line connects the socket s to the remote host and port specified by host and port.

  6. while True: - This line starts an infinite loop.

  7. data_from_server = s.recv(1024) - This line receives data from the remote host through the socket s and stores it in the variable data_from_server. The argument 1024 specifies the maximum amount of data to be received at once.

  8. print("received: ", data_from_server.decode()) - This line prints the received data to the console. The decode() method is used to convert the bytes received into a string that can be printed.

The overall purpose of this script is to connect to a remote host at a specified IP address and port number, and then continuously receive data from the remote host and print it to the console.

Client Script

import socket

#Remote IP and port
host = "192.168.0.103"
port = 54321

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

#Data from server

while True:
    data_from_server = s.recv(1024)
    print("received: ", data_from_server.decode())
  1. The script imports the socket module for networking functionality.
  2. The script defines two variables host and port that represent the remote IP address and port number to connect to.
  3. A new socket object is created with socket.socket(socket.AF_INET, socket.SOCK_STREAM) where AF_INET indicates IPv4 addressing and SOCK_STREAM indicates a TCP socket.
  4. The connect() method is called on the socket object with the host and port arguments to establish a connection to the server.
  5. The script enters an infinite loop using while True: to continuously receive data from the server.
  6. The recv() method of the socket object is called with a buffer size of 1024 bytes to receive data from the server.
  7. The received data is decoded from bytes to string using the decode() method.
  8. The received data is printed to the console using the print() function, along with the message "received: ".

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...