Skip to content

Instantly share code, notes, and snippets.

@lmuntaner
Created March 12, 2022 08:47
Show Gist options
  • Save lmuntaner/2b0371ee87fe5331846b74e93e9d5143 to your computer and use it in GitHub Desktop.
Save lmuntaner/2b0371ee87fe5331846b74e93e9d5143 to your computer and use it in GitHub Desktop.
Sockets
from socket import AF_INET, SOCK_STREAM, socket
server_port = 12000
server_ip = 'localhost'
# Create a TCP socket
# AF_INET indicats undelying network IPv4
# SOCK_STREAM means TCP socket rather than UDP
# OS handles the port of the client
client_socket = socket(AF_INET, SOCK_STREAM)
# initiates TCP connection between client and server
# parameters are the address of the server
# after this the handshake has been done
client_socket.connect((server_ip, server_port))
# send data to the connected socket through TCP
# client drops the bytes to be handled by the TCP connection
client_socket.send('Hello, server'.encode())
# we wait to receive 1024 bytes
data = client_socket.recv(1024)
print('From Server: ', data.decode())
# close TCP connection opened in line 10
client_socket.close()
from socket import socket, AF_INET, SOCK_STREAM
server_port = 12000
# Create a TCP socket
# AF_INET indicats undelying network IPv4
# SOCK_STREAM means TCP socket rather than UDP
# OS handles port of the client
server_socket = socket(AF_INET, SOCK_STREAM)
# associate port with socket process
server_socket.bind(('', server_port))
# listen for TCP connection requests
# parameter specifies maximum number of queued connections
server_socket.listen(1)
print('The server is ready to receive')
# when a client knocks on the door, the program invokes the `accept` method
# which creates a new socket in the server dedicated to this particular client
# the client socket and the new socket do complete the TCP handshake
# Is this socket listening in another port than 12000?
connection_socket, addr = server_socket.accept()
# after the handshake, client and server can send bytes to each other over the connection
# new socket receives data
data = connection_socket.recv(1024)
print('From client: ', data.decode())
# new socket sends back data
connection_socket.send('Hello, client'.encode())
# we close the new socket
connection_socket.close()
from socket import socket, AF_INET, SOCK_STREAM
server_port = 12000
# Create a TCP socket
# AF_INET indicats undelying network IPv4
# SOCK_STREAM means TCP socket rather than UDP
# OS handles port of the client
server_socket = socket(AF_INET, SOCK_STREAM)
# associate port with socket process
server_socket.bind(('', server_port))
# listen for TCP connection requests
# parameter specifies maximum number of queued connections
server_socket.listen(1)
print('The server is listening on port 12000')
while True:
# when a client knocks on the door, the program invokes the `accept` method
# which creates a new socket in the server dedicated to this particular client
# the client socket and the new socket do complete the TCP handshake
# Is this socket listening in another port than 12000?
connection_socket, addr = server_socket.accept()
# after the handshake, client and server can send bytes to each other over the connection
# new socket receives data
data = connection_socket.recv(1024)
print('From client: ', data.decode())
# new socket sends back data
connection_socket.send('Hello, client'.encode())
# we close the new socket
connection_socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment