TCP socket
TCP socket
TCP socket
What is a socket
It’s software needed to connect two components or computers
to form a communication links
Protocols
Communication protocols specify the rules of how and in
what format information should be sent and received.
These protocols are assembled into a hierarchy to
manage the various tasks involved in network
communication.
In other words, some protocols handle how hardware
receives, sends, or routes packets, while others are more
high-level and are concerned, for example, with
application-level communication etc.
Some commonly used and widely well-known network
communication protocols include:
Page 2
1- WI-FI
2- BLUETOOTH
3- IP (Internet Protocol)
IP is a network layer protocol mainly responsible for
routing packets and IP addressing.
TCP socket python have one socket class for all, the
difference is in methods and protocols
4. Call the client socket object’s send () or sendall() function to send text data
to the server socket. The function’s input parameter is a bytes object, so you
need to encode the send text before sending it.
client_socket.sendall(user_input_str.encode('utf-8'))
6. The received data is also a bytes object, you need to convert it to a text string
by invoking the bytes object’s function decode(‘utf-8’).
str_tmp = data_tmp.decode('utf-8')
7. Call the client socket’s close() function to close the socket connection.
client_socket.close()
3. Bind the socket object to a host and a port number with the socket
object’s bind( server_info_tuple_object ) function.
4. server_socket.bind(('localhost', 8080))
6. Call the server socket object’s accept() function to accept the client socket
request, it will return a connection object and the client socket IP address.
(connection, client_ip) = server_socket.accept()
7. Call the connection object’s recv(buffer_size) function to read the data sends
from the client socket.
data = connection.recv(1024)
8. The readout data is also a bytes type object, you need to invoke
it’s decode(‘utf-8’) function to convert it to a string.
data_str = data.decode('utf-8')
10. Call the connection object’s close() function to close the socket connection.
connection.close()
Page 7
# Client Socket
import socket
print(f"Received {data!r}")
Constants
The AF_* and SOCK_* constants are now AddressFamily and SocketKind IntEnum collections.
socket.AF_UNIX
socket.AF_INET
socket.AF_INET6
These constants represent the address (and protocol) families, used for the first argument
to socket(). If the AF_UNIX constant is not defined then this protocol is unsupported. More
constants may be available depending on the system.
socket.SOCK_STREAM
socket.SOCK_DGRAM
socket.SOCK_RAW
socket.SOCK_RDM
socket.SOCK_SEQPACKET
These constants represent the socket types, used for the second argument to socket(). More
constants may be available depending on the system.
(Only SOCK_STREAM and SOCK_DGRAM appear to be generally useful.)
socket.SOCK_CLOEXEC
socket.SOCK_NONBLOCK
Page 9
These two constants, if defined, can be combined with the socket types and allow you to set
some flags atomically (thus avoiding possible race conditions and the need for separate calls).