Build a chat application using socket programming with python
Is possible to establish a network to transfer data from system to another system through programming??
yeah it is possible we can establish a network to transfer data from system to another system through socket programming for that Build a chat application
In python can transfer the input data through to the program or process within local system only. because the program every program associated with only process id and does not have port num. So to transfer data to program in other system we have to bind the port num with the ip address because ip address helps to reach the system so to connect to any process port num will come and play a vitial role.
In the real world we have uniquely identified ip address and where port num uniquely assoicated with it to connect the unique process in the other system.
To transfer the data to the other system we have to follow some protocol/rule usually we have two types of protocols one is Transimission Control Protocol (TCP) and other one is User Datagrm Protocol (UDP).
While if you send any packet to another system how we will known that packet is reached to destination system for that destination system will send the received message and this is know as Acknowledgement.
Let’s consider we have two system A and B where in the UDP A sends the data to B then A will think that B received it incase if B not received the data also where UDP is works on both Acknowledgement and not Acknowledgement
But in TCP when A send the data to B does’t recieved the packets then the packets are on the hold until the B received the data where all packets are in hold if B received the data where TCP works only with Acknowledgement .
In the networking world where IPv4 have respective family address is AF_INET and UDP , TCP have SOCK_DGRAM , SOCK_STREAM have respective protocols
>>> import socket
>>> dir(socket)
You can find all things as disscuss above when you run the following things in the python repl or any python IDE.
socket is the module in the python where use can use the all the functions like address family and respective protocols for that we have to import the socket module.
import socket
i am using UDP protocol i.e SOCK_DGRAM and ipv4 address family i.e AF_INET where SOCK_DGRAM and AF_INET are the functions of the socket module.
s = socket.socket(socket.SOCK_DGRAM,socket.AF_INET)
As discussed earlier we have to bind the ip address with the port num for that we are using the bind function where bind function support tuple data type to pass as argument.
s.bind(("192.168.43.206",1234))
for Client to send the data someone have to received the data upto 1024 size only where 1024 is the buffer size. if size greater than it remaining will discarded. recvfrom is the function which will receive from udp protocol.
x= s.recvfrom(1024)#server side programimport socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind((“192.168.43.206”,1234))
x= s.recvfrom(1024)
print(x)
To receive the data someone have to send the data for that we have sendto function in the socket programming.
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto("hello".encode,("192.168.43.206",1234))
while sending the data it will support only byte datatype so we have to encode the data where change the string data type data to byte data type data and again to change the byte data type data to string data type data we have to decode it.
(b'hello',('192.168.43.41',34321))
Till now we don’t known the ip address of the client in the output of server after send the data we got ip address of the client.
By default the process has the capability to send one thread to the cpu only i.e process is executed only so if you want that multiple threads to be send to cpu where multi threading come and play role.
To use multi thread we have to import the threading module.
import threading
def a():
while True:
print("aa")
def b():
while True:
print("bb")
In the above code you can see a() function is the never ending one and we have limitation that we can send the only single thread to the cpu so send multiple thread to the cpu of one process. we have to create the threading we need to use the modules of threading.
x1=threading.Thread(target=a)
x2=threading.Thread(target=b)
here we created the threads of the two infinte functions
x1.start()
x2.start()
and here we started the threads
import threading
def a():
while True:
print("aa")
def b():
while True:
print("bb")
x1=threading.Thread(target=a)
x2=threading.Thread(target=b)
x1.start()
x2.start()
here is the completed python code to demonstrate the multithreading.
chat1 application program:
import socket
import threading
import oss = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)ip = input("\n\t\tEnter Your IP : ")
port = 5678s.bind( (ip,port) )server_ip = input("\n\t\tEnter Server IP : ")
server_port = 1234print()def send():while True:
os.system('tput setaf 5')
msg = input('\n').encode()
s.sendto(msg,(server_ip,server_port))
if msg.decode() == "exit":
os._exit(1)
def recv():
while True:
os.system('tput setaf 2')
msg = s.recvfrom(1024)
if msg[0].decode() == "exit":
os._exit(1)
print('\n\t\t\t\t\t\t\t'+ server_ip +':'+msg[0].decode())t1 = threading.Thread(target=send)
t2 = threading.Thread(target=recv)t1.start()
t2.start()
chat1 application program:
import socket
import threading
import oss = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)ip = input("\n\t\tEnter Your IP : ")
port = 1234s.bind( (ip,port) )server_ip = input("\n\t\tEnter Server IP : ")
server_port = 5678def send():while True:
os.system('tput setaf 5')
msg = input('\n').encode()
s.sendto(msg,(server_ip,server_port))
if msg.decode() == "exit":
os._exit(1)
def recv():
while True:
os.system('tput setaf 2')
msg = s.recvfrom(1024)
if msg[0].decode() == "exit":
os._exit(1)
print('\t\t\t\t\t\t\t'+server_ip +':'+ msg[0].decode())t1 = threading.Thread(target=send)
t2 = threading.Thread(target=recv)t1.start()
t2.start()
Run the two codes to establish the chat application
Finally we successfully the Builded a chat application using socket programming with python
source code link: