1

This may be a stupid question, i apologise if it is. But I'm rewriting some c code into c# to use as part of a GUI, the original c programs transmit data buffers backwards and forwards to a microcontroller via:

n = write(sockfd, buf, sizeof(buf));

In the transmitter program and

n = read(sockfd, (void *)buf[idx]+numread,sizeof(buf[0])-numread);

In the receiver program. I am trying to find the c# equivalent of these functions above, but the only one i have found only takes byte data.

The server on the microcontroller runs software i didn't design, hence why i can't simply serialise or convert to byte etc (as in previous similar questions: How to send integer array over a TCP connection in c# and then decompose at the other end, or at least id rather check if theres a solution before i get into trying to edit code i didn't write.

any help greatly appreciated !

6
  • What's wrong with "byte data"? Commented Sep 10, 2016 at 13:45
  • It's unclear what structure your buf data is. "Integer array" typically means a contiguous block of memory organized as integers of a certain fixed range stored in bytes with the CPU's native endianness with no padding between the integers. So, what are the particulars that you'll have to match in C#? Commented Sep 10, 2016 at 15:04
  • The lines in the server program which send and receive the data are if(send(sock_client, rxbuf, sizeof(rxbuf), MSG_NOSIGNAL) < 0) break; if(recv(sock_client, txbuf, sizeof(txbuf), MSG_WAITALL) <= 0) break; I think at least, its a very complicated program which i don't particularly understand. Both rxbuf and txbuf are declared as uint32_t arrays. Im not sure in this sense what send and recv actually do to the data, do they convert it to bytes and send it ? If so is there an easy way to convert it back on the c# end ? Commented Sep 11, 2016 at 16:05
  • @user2769075 why did you comment here and not interact with my answer? It solves your problem. Commented Sep 12, 2016 at 21:18
  • @usr Sorry, i have been trying to get it to work with Binary reader but haven't had any success so far, i don't understand how the conversions work so my data keeps coming out as garbage. I wasn't intentionally ignoring your answer, but i don't feel it has solved my issue either, i have however marked it as helpful which i should have done previously. Thank you for your help. Commented Sep 14, 2016 at 10:58

1 Answer 1

0

You can use NetworkStream.Read/Write to read and write byte arrays to a socket. BinaryReader/Writer are abstractions on top of that that make it easier to write in certain formats.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.