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
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
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.
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.
ReplyDeleteThanks Salim, stay connected.
DeleteNicely explained, it will be really helpful for the first time socket user as well as for the coder want to transfer data like images
ReplyDeletethank you Jeevan :)
DeleteI'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.
ReplyDeletedata science in malaysia
Thanks for sharing this Information. Machine Learning Training in Gurgaon
ReplyDeleteHappy 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.
ReplyDeletedata analytics course in yelahanka
thank you for your feedback..
DeleteNice article. I liked very much. All the information given by you are really helpful for my research. keep on posting your views.
ReplyDeletedata scientist course
Thank you..Do follow my blog for more updates..
DeleteThanks for Sharing this Information. Machine Learning Training in Gurgaon
ReplyDeleteThank you follow my blog for more update.
DeleteThank you
ReplyDeleteThank you
ReplyDeleteThank you follow my blog for more update.
ReplyDeleteAlways so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning
ReplyDeletebusiness analytics courses in aurangabad
Thank you ...
DeleteEven 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.
ReplyDeleteai development company
Thank you for posting this blog really appreciate the efforts you have taken into curating this blog if possible please check out,
ReplyDeletedata science course in bangalore they have a lot to offer with regards to data science in terms of training and live projects
Thanks
DeleteI feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
ReplyDeletecyber security certification malaysia
Thank you
DeleteImpressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeletecyber security training malaysia