473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

single byte access in memblock

41
Hi!

I'm trying to learn some network programming and right now im designing the package layout.
I think ill do it like this:
- first two bytes (an int): the size of the package
- third byte: the type of the package
- the rest: the data

The problem is that i dont know how to access a singe byte in a memblock created by malloc.
I guess i could use an unsiged char, but I'd like my app to be cross platform, and im not sure that a char is one byte on every arch.

Something like this:
Expand|Select|Wrap|Line Numbers
  1. unsigned int packageSize = 2 + 1 + dataSize;
  2. unsiged char* package = (unsiged char*)malloc(packageSize);
  3. package[0] = (unsigned char)packageSize;
  4. package[2] = 2 // 2 being some predefined type
  5.  
  6. memcpy(package[3], data, dataSize);
  7.  
Thanks!
Aug 17 '08 #1
7 2148
weaknessforcats
9,208 Expert Mod 8TB
So where is package[3] allocated?

You can't copy to memory you don't own.

Also, shouldn't your package be a struct?
Aug 17 '08 #2
JonLT
41
yeah a struct might be a good idear, but im not sure that it takes the same amount of mem on all archs. So if i send some bytes from the server and cast them to a package struct in the client, im not sure ill get the right thing.

What i really want is a type that uses the same amount of bytes on every arch. Do i need to make my own type, and if so - how would one do that?

I know the above code doesnt work, its more like a pointer towards what i would like to do
Aug 17 '08 #3
Banfa
9,065 Expert Mod 8TB
When you say the package layout you mean the layout of the data block you are going to send across the network?

A lot depends on whether you intend this protocol to be transmitted over a TCP or UDP connection (or something else of course) but you may wish to consider also including

Package start indication: A TCP connection is a byte stream you may need to be able to pick out the start of a package in the stream.

Version Number: either message or protocol unless of course you only ever plan on having one version of the protocol that doesn't change once it is implemented to allow legacy and cutting edge devices to still effectively communicate.

A checksum of some sort: If you are using UDP then you have no guarantee of correct data delivery so a checksum might be useful in validating your packages.

When you define your package you will need to specify if the data carried is big endian or little endian (or uses some other byte ordering specification). That is if the package carries a 4 byte integer what order are the bytes of that integer transmitted in.

Are you sure that 1 byte (256 values) is enough for the package type? Remember no-one was ever going to need more than 640k of RAM in there PCs not so long ago.


sizeof(unsigned char) == sizeof(char) == 1

You are guaranteed this by the C or C++ (you haven't said which you are using) standard. Unfortunately what you are not guaranteed is the number of bits in a byte which is platform dependent.
Aug 18 '08 #4
JonLT
41
Thank you very much for that answer. It made me think :)
I didn't know anything about endianness, now i know a little bit. And i think a time-stamp in the header might be a good idear aswell.

Again: Thank you very much!
Aug 19 '08 #5
Banfa
9,065 Expert Mod 8TB
Oh I forgot identifier of the sending unit. In the protocol I use/wrote we have

Start Bytes
Size
Protocol Version
Unit ID
Time Stamp (to 1/100 second)
Package Type
Payload
CRC16

Of course our packages have to be transmittable via a number of different transport mediums including TCP/IP and a modem link (and SMS is on the cards too but only conceptually at the moment).

The is no sequence number in the header because the majority of our packages are complete in themselves. In the few (1) case where the data to be transferred large (an image for example) it is carried by multiple packages in sequence. Then part of the payload is sequence number and max-sequence number to ensure the received packages can be verified as correct, complete and put back together in the right order.

If you are only using TCP this may not be required, on the other hand if are not and most of your packages carrier payloads of bits of a data block you wish to put sequence and max-sequence into the header.
Aug 19 '08 #6
JonLT
41
Thank you once again :)

I'm planing on using TCP for starters. The application im developing is a IRC like chat server and client. But I'd like it to be able to send more than just strings.
I'm using C++.
If TCP proves to slow i'll try UDP with some custom checking on top.

To get back to my initial question:
The following is what i'm planning to do:

The package:
- size (2 bytes)
- type (2 bytes)
- time stamp ( 'not sure yet' bytes)
- payload ( 'size' bytes)

When the client wants to send something, i reserve a memblock large enough for the payload and the additional information (the rest of the package). After that i just memcpy the data into the block at the predefined locations, and convert to big-endian (if it isnt already) and then send.
To manifest the package in the server i first receive the size of the package, and if needed convert its endianness. I then reserve enough mem for the rest of the package, and receive it (putting it in to the reserved block), again convert endianness if needed. To get the data from the package, first memcpy the type and the time stamp, and cast to some known type, and to get the payload of the package memcpy the data and cast to the type defined by the type field.

But the problem is this: will the result of the memcpy be correct on all archs (im not aiming for any embedding). I know this app will proberply never leave my home network, but i would like it to work elsewhere (just for kicks).
From what i understand an (un)signed int is minimum 2 bytes. Does that mean that important data can be stored outside the first two bytes, or will it be enough to send the first two bytes?
Is it even possible to send a float/double, whitout converting it to some costum format (saving the two parts (significand/exponent) in some clever way)?

Thanks!
Aug 19 '08 #7
Banfa
9,065 Expert Mod 8TB
OK everything is bytes, it is up to you to define the size of the integers carried by your packages and it is up to the platforms to convert them to a form that is easy for them to handle.

Some protocols, MPEG2 for example, use the minimum number of bits required to carry the required range of values specified which leads to only sized fields 13 bits for example.

If you decide that for the purposes of your package an unsigned int is a 2 byte big endian value then that is how it is, it becomes the receiving software's problem to convert this to a appropriate data type for the platform in question.

The same with floating point numbers, you specify what standard you package will use and what order the bytes are in and it becomes the software on the platforms responsibility to read those bytes and convert them to a format the platform can use.

Remember what you are sending and receiving is not some integers and floating point values but an array of bytes that conform to a specification. It is the software's job to convert between that byte stream and useful data types.

You may have a structure that is the data for a given package type. If you want to produce portable, best practice code then what you can not do is use memcpy or casting to get the data from the array of bytes to the structure or vice-versa you must process the bytes individually to fill in your structure from the byte array.

That is you have to go the long way round using the bit operators (& | << >>).
Aug 19 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
3
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
2
by: Sisnaz | last post by:
I'm reading byte information from a C++ program via TCP sockets. At one time I thought I came across away to convert C Float data type into a VB single data type. I'm reading the C data type in...
13
DOSrelic
by: DOSrelic | last post by:
I have an assignment program that uses an array of images that are created from a supporting resource folder but I wish to incorporate them into the final exe file so how do I make an array from the...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.