Python Socket (TCP / UDP)

Dark-Base

>_ عضو ماسي _<
.:: كاتب تقني ::.

Sep
262
288
  • التقييمات: 0
  • #1
اتصال TCP مع خادم وعميل - ( server and client )TCP socket
Python:
#server.py:

import socket  # Importing the socket module to create network sockets

# Defining the host and port to listen on
HOST = '127.0.0.1'  # Localhost IP address
PORT = '8080'  # Port number to listen on

# Creating a socket object for the server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Binding the socket object to the host and port
server.bind((HOST, int(PORT)))

# Listening for incoming connections from clients, with a maximum queue of 5 clients
server.listen(5)

# Infinite loop to keep the server running
while True:
    # Accepting incoming connection from client and getting the communication socket and client address
    communication_socket, address = server.accept()
    print(f"connected to {address}")

    # Receiving data from the client and decoding it to a string using UTF-8 encoding
    message = communication_socket.recv(1024).decode('utf-8')
    print(f"message: {message}")

    # Sending a message back to the client to acknowledge that the message was received
    communication_socket.send(f"message received".encode('utf-8'))

    # Closing the communication socket with the client
    communication_socket.close()
    print(f"communication with {address} ended")

################################################################################

#client.py:

import socket  # Importing the socket module to create network sockets

# Defining the host and port to connect to
HOST = '127.0.0.1'  # Localhost IP address
PORT = '8080'  # Port number to connect to

# Creating a socket object for the client
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connecting to the server at the specified host and port
client_socket.connect((HOST, int(PORT)))

# Sending a message to the server by encoding a string to bytes using UTF-8 encoding and sending it through the socket
client_socket.send("Hello world!".encode('utf-8'))

# Receiving a response from the server by receiving up to 1024 bytes of data through the socket and decoding it to a string using UTF-8 encoding
print(client_socket.recv(1024))

# Closing the socket connection with the server
client_socket.close()

اتصال UDP مع خادم وعميل - ( server and client )UDP socket
Python:
# server.py
import socket

# Defining the host and port to listen on .
Host = "127.0.0.1"
Port   = 5554

# Configration Send Data .
msgFromServer = "Hello UDP Client"
data = msgFromServer.encode()

# Creating a socket object for the server .
socket_object = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

# Binding the socket object to the host and port .
socket_object.bind((Host , Port))

print("UDP server up and listening")

# Listen for incoming datagrams .
while(True):
    message , address = socket_object.recvfrom(1024) # Buffer Size .

    clientMsg = f"Message from Client:{message}"
    clientIP  = f"Client IP Address:{address}"
 
    print(clientMsg)
    print(clientIP)

    # Sending a reply to client .
    socket_object.sendto(data , address)

###################################################################################
# client.py

import socket
msgFromClient       = "Hello UDP Server"
data = msgFromClient.encode()
serverAddressPort   = ("127.0.0.1", 5554)
# Create a UDP socket at client side .
socket_obj = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Send to server using created UDP socket .
socket_obj.sendto(data, serverAddressPort)
msgFromServer = socket_obj.recvfrom(1024) # Buffer Size .
msg = f"Message from Server :{msgFromServer}"
print(msg)
 
التعديل الأخير:
الله يعطيك الف عافيه بارك الله فيك 🤍
 
عودة
أعلى