![]() ![]() import zlib def checksum_calculator(data): checksum = zlib.crc32(data) return checksum First, let’s create a checksum calculator function. In this tutorial, we will use the Zlib library for calculating checksum and the struct library for making the UDP header. But wait, how are we going to do that? Well, we have so many modules to work with. Like we said earlier, we have to make a UDP header, put the checksum in the UDP header and finally attach it to the data. There are no UDP headers therefore we didn’t calculate for the checksum. In the above simple example, we are just sending the data. After that on the last line, we are finally sending the packet to a receiver. We are encoding(changing it to bytes) the data because we can only send bytes over a socket. As you see it there the data is “Hello world”. On the above 4 lines of code, we are trying to send data(packet) to a receiver(client). data = “Hello world” packet = data.encode() receiver_addr = (“127.0.0.1”, 1112) # For now, it is just an arbitary address socket_ndto(packet, receiver_addr) So to make things clear, SOCK_DGRAM is for UDP protocol and SOCK_STREAM is for TCP protocol. If we were to use a socket.SOCK_STREAM, it would have been a TCP protocol socket. The second parameter(socket.SOCK_DGRAM) is the only one making the socket a UDP protocol socket. It is used to designate the type of addresses that our socket can communicate with (in this case, Internet Protocol v4 addresses). The first argument(socket.AF_INET) is the address family. The thing that makes the socket_object a UDP protocol socket is the argument of the socket.socket() object. We bind on a specific address means, senders can send data to us by targeting the specified address. Well, on the above 3 lines of code, we imported the socket module, created the socket object(UDP socket object) that we are going to use for sending and receiving data, and bonded with a local host and port number. import socket socket_object = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) socket_object.bind(("127.0.0.1", 1111)) First, let’s create the socket object for the UDP protocol. You don’t have to worry if you never worked with this module. ![]() In this article, we are going to use python’s built-in module “socket”. So, in this article like it says from its title, we are going to experiment with UDP protocol, headers including checksum calculation.įirst of all, let’s create a simple script with UDP protocol excluding UDP headers and checksum calculation. And we said that the solution for data corruption is calculating the checksum of packets on both sender and receiver sides. In the previous article(UDP protocol and header explanation), we said that there might be data loss or data corruption while communicating through the UDP protocol. If you don’t fulfill this requirement, I will suggest you read my article on the explanation of UDP protocol and headers. Prerequisites- This article assumes a basic understanding of networking, UDP protocol, and python programming language.Īttention: To continue with this article(tutorial), you have to know how UDP protocol works and how we use UDP headers in order to calculate checksum and do some other stuff. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.UDP protocol with a header implementation in python Original Assignee SUZHOU PANXING OPTOELECTRONIC TECHNOLOGY Co Ltd Priority date (The priority date is an assumption and is not a legal conclusion. SUZHOU PANXING OPTOELECTRONIC TECHNOLOGY Co Ltd Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.) Inventor 许琨 杨峰 Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.) Pending Application number CN2012100874073A Other languages Chinese ( zh) Google Patents CN102638464A - UDP (User Datagram Protocol) checksum calculating method CN102638464A - UDP (User Datagram Protocol) checksum calculating method ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |