Here is your answer, SOCKET is a address made up of two independent entity first one is IP address and second one is the PORT number it is a 48 bits address where the first 32 bits represent a ip address and last 16 bits represent the port number.
- gethostname() Returns the host name.
- gethostbyname(host_name) Takes host name as an argument and returns ip address of that host.
- socket(socket.AF_INET, socket.SOCK_STREAM) Create a server or client and returns a socket object.
- bind(SOCKET_ADDR) Method which binds it to a specific IP and port so that it can listen to incoming requests on that IP and port.
- listen() Listen method which puts the server into listening mode, this allows the server to listen to incoming connections
- accept() The accept method initiates a connection with the client.
- send(message) Sends the messege in encoded format.
- recv(msg_length_in_bytes) Takes messege length as an argument and receives messege of that length.
- close() Close method closes the connection with the client.
- encode(FORMAT) Takes a format and encodes a normal string into a byte string.
- decode(FORMAT) Takes a format and decodes a byte string into a normal string.
- socket.AF_INET AF_INET is an address family that is used to designate the type of addresses that your socket can communicate with (in this case, Internet Protocol v4 addresses).
- socket.SOCK_STREAM SOCK_STREAM. Provides sequenced, two-way byte streams with a transmission mechanism for stream data.
Follow the reference image
.
In server.py
and client.py
we got port address by creating a tuple of ip address and port number.
In code SOCKET_ADDR = (IP, PORT)
where we got the ip address using two functions of socket module IP = socket.gethostbyname(socket.gethostname())
In these python project I made a server server.py
and client.py
cli based programmes, but here we have two client programmes tom.py
and jerry.py
to communicate with each other first run the server.py
file when it's running run both client programmes one after another in different command prompt window.
congratulations, you got it now you are ready to communicate between clients.