Add socket programming scripts

This commit is contained in:
Dishant Nagpal 2022-10-09 14:53:49 +05:30
parent d080931b09
commit 76b0c6beba
3 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,16 @@
# Socket Progamming in Python
We all know how to use python for basic stuff and general programming. But this script allows you to create a server and client of your own using sockets.
# What are sockets?
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
# Requirments
Clone Repo
You don't need to install anything just import sockets library.
More information here : https://docs.python.org/3/howto/sockets.html
Use server.py for server
Use client.py for client

View File

@ -0,0 +1,9 @@
import socket
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
port = 1024
host = socket.gethostbyname(socket.gethostname()) # get the hostname
client_socket.connect((host, port))
message = client_socket.recv(2048)
print(message.decode("utf-8"))

View File

@ -0,0 +1,11 @@
import socket
server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
port = 1024
host = socket.gethostbyname(socket.gethostname()) # get the hostname
server_socket.bind((host, port))
server_socket.listen(2)
while True:
conn, address = server_socket.accept()
print("Connection from: " + str(address))
conn.send(bytes("Socket programming in python","utf-8"))