TCP Socket Programming in Python (server/client)
Socket Programming in Python
What is Socket :
A socket is one end of a two-way communication link between two programs/software running on the network. A socket is bound to a port (also known as process id) number so that the TCP/UDP layer can identify the application that data to be transferred to.
What to do?
in order to create a server first, we need to create a socket in the program and put this socket into the listen mode.
to that in the python is very easy, as you all know python is a very easy and powerful language.
Socket Creation
Create a New python file named as the server.py keep in mind that to work with socket we need to import socket into the file.
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 15710
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((TCP_IP,TCP_PORT))
TCP_IP is the IP of this machine you are working on, and TCP_PORT is the port to be connected.
socket.AF_INET is the protocol to work with the TCP protocol.
Listening to connection and accepting the connection.
Now we have to put this socket into the listen mode so that it can listen to the different client requests.s.listen(1)
print("Socket is listening")
Let's create a buffer to store data on that is received by the client.
also, accept the connection from the client.
BUFFER = 1024
c , addr = s.accept()
print("Connection Addr : {}".format(addr))
Sending a string to the client
So now we have accepted a connection from the client let's send a string to client and say you are connected to the client.
seq = "You are connected to the server"
c.sendall(seq.encode("utf-8"))
As of now you have connected to the client and sent a string of verification.
Let's move to the receiving of the message.
Receiving of Message
to receive a message we have to wait for the client using the while loop.
while True:
data = c.recv(BUFFER)
if not data:
break
x = data.decode("utf-8")
print("client replied " + x)
break
c.close()
Lastly, close the connection.
Program for the client.py
Client program is somewhat same as the server program so I am copying and pasting most of the code.
in client program, we do not bind or listen for the connection we directly connect to the socket.
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
TCP_IP = '127.0.0.1'
port = 15710
s.connect((TCP_IP,port))
BUFFER = 1024
in client first, we listen from the server then sends a string and exit the program.
while True:
data = s.recv(BUFFER)
if data:
break
x = data.decode("utf-8")
print("Server message ".format(x))
n = "we are code in cafe"
s.send(n.encode("utf-8"))
s.close()
that's all for the client program.
Testing:
for the testing, I have some screenshot.
for the server
for the client
Source code for the Server.py
import socket
TCP_IP = "127.0.0.1"
TCP_PORT = 15710
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((TCP_IP,TCP_PORT))
s.listen(1)
print("Socket is listening")
BUFFER = 1024
c , addr = s.accept()
seq = "You are connected to the server"
c.sendall(seq.encode("utf-8"))
# now wait for the msg
while True:
data = c.recv(BUFFER)
if not data:
break
x = data.decode("utf-8")
print("client replied " + x)
break
c.close()
for the Client.py
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
TCP_IP = '127.0.0.1'
port = 15710
s.connect((TCP_IP,port))
BUFFER = 1024
#waiting for message
while True:
data = s.recv(BUFFER)
if data:
break
x = data.decode("utf-8")
print("Server message {}".format(x))
n = "we are code in cafe"
s.send(n.encode("utf-8"))
s.close()
if you have liked the post then please share this post to your friends.
for the more posts like this stay tuned.
Are you working on the Web development or trying to create a school project or working on the simple website, and having trouble with learning the basics of the PHP, Java, Python, C#, C++, C. You were at the right place...
Thanks for being here.
Comments
Post a Comment