Send multiple variables at once socket

Asked

Viewed 106 times

0

Using the method Socket.Send would have some way to send a package with for example an integer and a string at once?

I saw such a Packet which apparently was all I needed, because I can turn it into an array of bytes and then turn it back into Packet but I couldn’t use the namespace Microsoft.SmartDevice.Connectivity, It is as if it did not exist in the vineyard version of .NET. But yet, someone has a better solution than this?

1 answer

1


Icarus, according to the answer given in this question here:

Yes, it is possible. Just know the size in bytes of your content. So imagine that you sent a 32-bit integer and a string containing 10 bytes respectively through a socket.

Edited (Adding method for transforming objects into array of bytes):

Who sends the content must generate the bytes to be sent in the following way (as an example):

Int32 codigo = 123;
String conteudo = "teste123";

const int buffer_length = 1024;
byte[] dados = new byte[buffer_length];

dados = BitConverter.GetBytes(codigo);
Buffer.BlockCopy(conteudo.ToCharArray(), 0, dados, dados.Length, conteudo.Length);

Who receives should read as follows (considering the absence of spaces or any other characters among the bytes sent):

//Declara-se o buffer de recebimento de dados
const int buffer_length = 1024;
byte[] dados = new byte[buffer_length];

//Recebe-se os dados da socket cliente
int recv_length = cliente.Receive(dados, buffer_length, SocketFlags.None);

//Contador para controle de "Em qual byte paramos a leitura?"
int byte_inicial = 0;

//Recebemos o inteiro através de um array de bytes e convertemos ele.
Int32 inteiro_recebido = BitConverter.ToInt32(dados, 0);
//Logo acrescemos o seu tamanho em bytes ao total de bytes lido
byte_inicial += sizeof(Int32);

//Declaramos um buffer isolado para a string que vem a seguir
byte[] bytes_str_recebida = new byte[buffer_length];
//E copiamos ela no array de bytes recém declarado passando os parâmetros que 
// indicam onde no array original a string está e qual seu tamanho
Buffer.BlockCopy(dados, byte_inicial, bytes_str_recebida, 0, dados.Length - byte_inicial);

//E finalmente convertemos os bytes correspondentes ao texto recebido para o tipo String.
string str_recebida = bytes_str_recebida.ToString();

I made it well commented the code (complete in the linked question) to make it easier to understand.

To add other types to the content is the same scheme, just know where the content is and what its size in bytes.

I hope I was able to help.

  • I had done something very similar yesterday, but it was giving a very crude error, I will post here in the comments for you to see

  • I tried as follows: http://pastebin.com/2iwdkqjF, but it is giving error in the line that I send the amount of bytes of the message

  • Dude, the parameters of your bitconverter uses are incorrect, so the problem. Get a better read on the code up there, Getbytes receives by parameter the object it will read and transform into bytes.

  • But logically, on the error line I send the bytes representing the size of the string bytes, understood?

  • Solved... I was using bufferOfClient = BitConverter.GetBytes(0) in this case the size of the array was automatically limited to the size of 0 in bytes :/

  • 1

    Great, good that you did. Seemingly insignificant changes at the beginning of the code usually impact code that follows negatively...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.