You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thomas Branyon edited this page Jan 28, 2016
·
8 revisions
#Data Serialization
Data serialization, for our purposes, simply refers to taking a block of data and transmitting it one byte at a time over some interface. This tutorial will give an example using sockets and the C programming language, but the concepts are the same for Java.
Loading a buffer and transmitting
Many types of data can be dealt with as simply a stream of bytes. In this C example, a .png image is read into a program as its individual bytes and then transmitted in order over a TCP socket.
FILE* fp = fopen("image.png","rb"); //open image.png for binary reading
char buf[32768]; //allocate 32K buffer for serializing image data (watch out for RAM usage)
int bytes_read = 0;
while(!feof(fp)) //keep going until we reach the end of the file
{
bytes_read = fread(buf, 1, sizeof(buf)-1, fp); //read as many bytes as possible from the image
write(socketfd, buf, bytes_read); //serially write as many bytes as we have in the buffer to the socket
}