Python Program that Sends And Recieves Message from Client

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

Socket programming is started by importing the socket library and making a simple socket.

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the second one is SOCK_STREAM. AF_INET refers to the address family ipv4. The SOCK_STREAM means connection-oriented TCP protocol.



Note: For more information, refer to Socket Programming in Python

Now we can connect to a server using Server:

A Server is a program that provides service to other computers on the network or Internet. Similarly, a client is a program that receives services from the server. When a server wants to communicate with a client, there is a need for a socket. A socket is a point of connection between the server and the client.


TCP/IP server program that sends message to the client.

filter_none

edit
close

play_arrow

link
brightness_4
code

import socket
   
# take the server name and port name
host = 'local host'
port = 5000
   
# create a socket at server side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET, 
                  socket.SOCK_STREAM)
   
# bind the socket with server
# and port number
s.bind(('', port))
   
# allow maximum 1 connection to
# the socket
s.listen(1)
   
# wait till a client accept
# connection
c, addr = s.accept()
   
# display client address
print("CONNECTION FROM:", str(addr))
   
# send message to the client after 
# encoding into binary string
c.send(b"HELLO, How are you ? \
       Welcome to Akash hacking World")
  
msg = "Bye.............."
c.send(msg.encode())
   
# disconnect the server
c.close()

chevron_right



TCP/IP server program that receive message from server.

filter_none

edit
close

play_arrow

link
brightness_4
code

import socket
   
# take the server name and port name
   
host = 'local host'
port = 5000
   
# create a socket at client side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)
   
# connect it to server and port 
# number on local computer.
s.connect(('127.0.0.1', port))
   
# receive message string from
# server, at a time 1024 B
msg = s.recv(1024)
   
# repeat as long as message
# string are not empty
while msg:
    print('Recived:' + msg.decode())
    msg = s.recv(1024)
  
# disconnect the client
s.close()

chevron_right


Note: Open In Two Separate DOS Windows And First Execute server, then Execute client.

Output of Server:

Output of Client:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.