How to process network byte order in C
I have a question regarding C and byte order. I am programming a small TCP server which is receiving a message. I however have trouble receiving messages. I can only find TCP examples of transferring strings. However, my message includes single toggled bits.
recv(socketClient, buffer, strlen(buffer), 0);
I am specifically asking the following: What Type do I need to initialize the buffer and how do I then process it (split it into an array of different integers ie.)
My current method for Strings:
int max_size = 512;
char buffer[max_size];// buffer to read server message
memset(buffer, ' ', sizeof(buffer)); //fill buffer memory with an empty char
c networking byte
add a comment |
I have a question regarding C and byte order. I am programming a small TCP server which is receiving a message. I however have trouble receiving messages. I can only find TCP examples of transferring strings. However, my message includes single toggled bits.
recv(socketClient, buffer, strlen(buffer), 0);
I am specifically asking the following: What Type do I need to initialize the buffer and how do I then process it (split it into an array of different integers ie.)
My current method for Strings:
int max_size = 512;
char buffer[max_size];// buffer to read server message
memset(buffer, ' ', sizeof(buffer)); //fill buffer memory with an empty char
c networking byte
1
Welcome to Stack Overflow! Please take the tour and read How To Ask A Good Question and How To Create A Minimal, Complete, And Verifiable Example.
– Fiddling Bits
Nov 13 at 20:57
add a comment |
I have a question regarding C and byte order. I am programming a small TCP server which is receiving a message. I however have trouble receiving messages. I can only find TCP examples of transferring strings. However, my message includes single toggled bits.
recv(socketClient, buffer, strlen(buffer), 0);
I am specifically asking the following: What Type do I need to initialize the buffer and how do I then process it (split it into an array of different integers ie.)
My current method for Strings:
int max_size = 512;
char buffer[max_size];// buffer to read server message
memset(buffer, ' ', sizeof(buffer)); //fill buffer memory with an empty char
c networking byte
I have a question regarding C and byte order. I am programming a small TCP server which is receiving a message. I however have trouble receiving messages. I can only find TCP examples of transferring strings. However, my message includes single toggled bits.
recv(socketClient, buffer, strlen(buffer), 0);
I am specifically asking the following: What Type do I need to initialize the buffer and how do I then process it (split it into an array of different integers ie.)
My current method for Strings:
int max_size = 512;
char buffer[max_size];// buffer to read server message
memset(buffer, ' ', sizeof(buffer)); //fill buffer memory with an empty char
c networking byte
c networking byte
edited Nov 13 at 21:19
dchi
212
212
asked Nov 13 at 20:48
kleinerberg
1
1
1
Welcome to Stack Overflow! Please take the tour and read How To Ask A Good Question and How To Create A Minimal, Complete, And Verifiable Example.
– Fiddling Bits
Nov 13 at 20:57
add a comment |
1
Welcome to Stack Overflow! Please take the tour and read How To Ask A Good Question and How To Create A Minimal, Complete, And Verifiable Example.
– Fiddling Bits
Nov 13 at 20:57
1
1
Welcome to Stack Overflow! Please take the tour and read How To Ask A Good Question and How To Create A Minimal, Complete, And Verifiable Example.
– Fiddling Bits
Nov 13 at 20:57
Welcome to Stack Overflow! Please take the tour and read How To Ask A Good Question and How To Create A Minimal, Complete, And Verifiable Example.
– Fiddling Bits
Nov 13 at 20:57
add a comment |
1 Answer
1
active
oldest
votes
To answer your questions first:
With what Type do I need to initialize the buffer
By "type" I assume you actually need value, as in what you initialize the buffer with. To answer - you don't need to fill it with some kind of known value at all. The return value of recv function tells you how many bytes you've received. Everything within that amount is valid data, everything else you can treat as garbage and can use the remaining space to for example null-terminate the string. Assuming you transfer only strings of course.
how do I then process it (split it into an array of different integers ie)
That's what protocols and serialization methods deal with. Those tell you things like the oder in which data gets sent across, how to interpret it, as well as tell you how to convert the data your program holds into byte buffer (argument to send) and then how to convert it back to get the same data as before the conversion (reading from recv).
Now to further elaborate, given the limited data given in the question: Unless you exchange data with a host that you know uses certain protocol (HTTP for example) or transfers data in some known format (JSON for example) and therefore could use some kind of library to do this job for you, you implement serialization and deserialization on your own. In short - you must know how the remote end converted data it sent into a byte buffer and you do the same operations in reverse order. Assuming you're on the same platform as the connected host, for a very quick proof-of-concept you can go with memcpy but any further than that I do not recommend it. How you go about this is totally up to you. Personally I'd recommend looking into the topic of data serialization, starting from simple examples and building your way up. Such topic is too broad to explain even in a minimal form in a single answer.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289277%2fhow-to-process-network-byte-order-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To answer your questions first:
With what Type do I need to initialize the buffer
By "type" I assume you actually need value, as in what you initialize the buffer with. To answer - you don't need to fill it with some kind of known value at all. The return value of recv function tells you how many bytes you've received. Everything within that amount is valid data, everything else you can treat as garbage and can use the remaining space to for example null-terminate the string. Assuming you transfer only strings of course.
how do I then process it (split it into an array of different integers ie)
That's what protocols and serialization methods deal with. Those tell you things like the oder in which data gets sent across, how to interpret it, as well as tell you how to convert the data your program holds into byte buffer (argument to send) and then how to convert it back to get the same data as before the conversion (reading from recv).
Now to further elaborate, given the limited data given in the question: Unless you exchange data with a host that you know uses certain protocol (HTTP for example) or transfers data in some known format (JSON for example) and therefore could use some kind of library to do this job for you, you implement serialization and deserialization on your own. In short - you must know how the remote end converted data it sent into a byte buffer and you do the same operations in reverse order. Assuming you're on the same platform as the connected host, for a very quick proof-of-concept you can go with memcpy but any further than that I do not recommend it. How you go about this is totally up to you. Personally I'd recommend looking into the topic of data serialization, starting from simple examples and building your way up. Such topic is too broad to explain even in a minimal form in a single answer.
add a comment |
To answer your questions first:
With what Type do I need to initialize the buffer
By "type" I assume you actually need value, as in what you initialize the buffer with. To answer - you don't need to fill it with some kind of known value at all. The return value of recv function tells you how many bytes you've received. Everything within that amount is valid data, everything else you can treat as garbage and can use the remaining space to for example null-terminate the string. Assuming you transfer only strings of course.
how do I then process it (split it into an array of different integers ie)
That's what protocols and serialization methods deal with. Those tell you things like the oder in which data gets sent across, how to interpret it, as well as tell you how to convert the data your program holds into byte buffer (argument to send) and then how to convert it back to get the same data as before the conversion (reading from recv).
Now to further elaborate, given the limited data given in the question: Unless you exchange data with a host that you know uses certain protocol (HTTP for example) or transfers data in some known format (JSON for example) and therefore could use some kind of library to do this job for you, you implement serialization and deserialization on your own. In short - you must know how the remote end converted data it sent into a byte buffer and you do the same operations in reverse order. Assuming you're on the same platform as the connected host, for a very quick proof-of-concept you can go with memcpy but any further than that I do not recommend it. How you go about this is totally up to you. Personally I'd recommend looking into the topic of data serialization, starting from simple examples and building your way up. Such topic is too broad to explain even in a minimal form in a single answer.
add a comment |
To answer your questions first:
With what Type do I need to initialize the buffer
By "type" I assume you actually need value, as in what you initialize the buffer with. To answer - you don't need to fill it with some kind of known value at all. The return value of recv function tells you how many bytes you've received. Everything within that amount is valid data, everything else you can treat as garbage and can use the remaining space to for example null-terminate the string. Assuming you transfer only strings of course.
how do I then process it (split it into an array of different integers ie)
That's what protocols and serialization methods deal with. Those tell you things like the oder in which data gets sent across, how to interpret it, as well as tell you how to convert the data your program holds into byte buffer (argument to send) and then how to convert it back to get the same data as before the conversion (reading from recv).
Now to further elaborate, given the limited data given in the question: Unless you exchange data with a host that you know uses certain protocol (HTTP for example) or transfers data in some known format (JSON for example) and therefore could use some kind of library to do this job for you, you implement serialization and deserialization on your own. In short - you must know how the remote end converted data it sent into a byte buffer and you do the same operations in reverse order. Assuming you're on the same platform as the connected host, for a very quick proof-of-concept you can go with memcpy but any further than that I do not recommend it. How you go about this is totally up to you. Personally I'd recommend looking into the topic of data serialization, starting from simple examples and building your way up. Such topic is too broad to explain even in a minimal form in a single answer.
To answer your questions first:
With what Type do I need to initialize the buffer
By "type" I assume you actually need value, as in what you initialize the buffer with. To answer - you don't need to fill it with some kind of known value at all. The return value of recv function tells you how many bytes you've received. Everything within that amount is valid data, everything else you can treat as garbage and can use the remaining space to for example null-terminate the string. Assuming you transfer only strings of course.
how do I then process it (split it into an array of different integers ie)
That's what protocols and serialization methods deal with. Those tell you things like the oder in which data gets sent across, how to interpret it, as well as tell you how to convert the data your program holds into byte buffer (argument to send) and then how to convert it back to get the same data as before the conversion (reading from recv).
Now to further elaborate, given the limited data given in the question: Unless you exchange data with a host that you know uses certain protocol (HTTP for example) or transfers data in some known format (JSON for example) and therefore could use some kind of library to do this job for you, you implement serialization and deserialization on your own. In short - you must know how the remote end converted data it sent into a byte buffer and you do the same operations in reverse order. Assuming you're on the same platform as the connected host, for a very quick proof-of-concept you can go with memcpy but any further than that I do not recommend it. How you go about this is totally up to you. Personally I'd recommend looking into the topic of data serialization, starting from simple examples and building your way up. Such topic is too broad to explain even in a minimal form in a single answer.
answered Nov 13 at 21:31
Jacek Ślimok
1,2942623
1,2942623
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289277%2fhow-to-process-network-byte-order-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to Stack Overflow! Please take the tour and read How To Ask A Good Question and How To Create A Minimal, Complete, And Verifiable Example.
– Fiddling Bits
Nov 13 at 20:57