Wednesday, November 4, 2020

Socket programming with python(sending text messages and image files)

          Recently I was working with socket programming and I was amazed to learn how we can communicate between 2 machines placed remotely. With sockets, not only communicating we can send/receive any kind of data including images. So I thought of sharing this knowledge with you all. 


          We will directly go to the implementation as there are many descriptions online about the socket programming. Today I will share two implementation, first one is sending text messages between sockets and second one is sending image from server to client machine via sockets. So Let’s get started:

    1.Sending text messages between machines via sockets:
    Server Side: In the server we have a bind() method which binds it to a specific ip(or local if connected via LAN) and port so that it can listen to incoming requests on that ip and port. The server also has a listen() method which puts the server into listen mode. This allows the server to listen to incoming connections. And last a server has an accept() and close(). Please see the below code for example. You can download the code from github
           Client side: The client code will connect with the server machine with the specified ip/local and print the received massage
You need to run the server code first, once you run it, it will print “Server started listening” , then run the client code which will print “Hey! Welcome” . Here is the output from the terminals

 
2.Sending images from server machine to client via sockets:
    We will use opencv to read image data at the server and then pack the data with pickle to send it to the client machine. In the client machine we will unpack the data with pickle and then display it.

    Server code:
            Server code would be similar with the above mentioned code, except the part of reading the image with opencv and dumping it with pickle.
    Then with a while loop we will send the data as long as it takes
        After the whole data is send we will shutdown and close connection
    Client Code:
        In the client side we will get the data size first, then retrieve the data
   
Then we will convert the data for visualization and show it with opencv

    Note:
While packing and unpacking data for sockets be careful about the format. For example if it is linux -> linux , then in the ‘struct.pack’ function we need to specify it as 'L', whereas communicating with R-pi or windows it should be '=L' .

         You can download the whole code from github. Do share your comments and feedback below. Stay inside, stay safe and keep learning cheers.

23 comments:

  1. Sockets are really interesting and getting into the fine details of how it works and seeing the communication happen seems like magic. And being able to send entire python objects using the pickle library is a great feature. Really appreciate the post as it allows for coders to get acquainted with sockets.

    ReplyDelete
  2. Nicely explained, it will be really helpful for the first time socket user as well as for the coder want to transfer data like images

    ReplyDelete
  3. I'm cheerful I found this blog! Every now and then, understudies need to psychologically the keys of beneficial artistic articles forming. Your information about this great post can turn into a reason for such individuals.
    data science in malaysia

    ReplyDelete
  4. Happy to visit your blog, I am by all accounts forward to more solid articles and I figure we as a whole wish to thank such huge numbers of good articles, blog to impart to us.
    data analytics course in yelahanka

    ReplyDelete
  5. Nice article. I liked very much. All the information given by you are really helpful for my research. keep on posting your views.
    data scientist course

    ReplyDelete
  6. Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning
    business analytics courses in aurangabad

    ReplyDelete
  7. Even though differences are stated, Artificial Intelligence and the Internet of Things do have a clear intersection between them, which is why AI becomes the lead to the future of IoT.

    ai development company

    ReplyDelete
  8. Thank you for posting this blog really appreciate the efforts you have taken into curating this blog if possible please check out,
    data science course in bangalore they have a lot to offer with regards to data science in terms of training and live projects

    ReplyDelete
  9. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    cyber security certification malaysia

    ReplyDelete
  10. Impressive. Your story always bring hope and new energy. Keep up the good work.
    cyber security training malaysia

    ReplyDelete