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 !

Community
  • 1
  • 1
  • What's wrong with "byte data"? – Amit Sep 10 '16 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#? – Tom Blodget Sep 10 '16 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 ? – user2769075 Sep 11 '16 at 16:05
  • @user2769075 why did you comment here and not interact with my answer? It solves your problem. – usr Sep 12 '16 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. – user2769075 Sep 14 '16 at 10:58
  • Maybe you need a big/little endian conversion? That C code is garbage because it transmits integers in an undefined format. Does http://stackoverflow.com/questions/8620885/c-sharp-binary-reader-in-big-endian help? In any case the question is not fully answerable because you need to investigate the format that is being sent. I'm stressing **you**. You need to find out and provide that info because nobody else can. – usr Sep 14 '16 at 19:51

1 Answers1

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.

usr
  • 168,620
  • 35
  • 240
  • 369