Miro said:
VB 2003
at the end of the code, this works great.
bytCommand = Encoding.ASCII.GetBytes("testing hello send text")
udpClient.Send(bytCommand, bytCommand.Length)
and this recieves it
Dim strReturnData As String = _
System.Text.Encoding.ASCII.GetString(receiveBytes)
Works super.
The original example i used was:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp
what I cant find is something like
System.Text.Encoding.ARRAY
Is it possible to send an array instead of a straight line of text? I know
i can make a huge string and send it and then
parse it out. But I just cant seem to find an example anywhere where anyone
has actually sent an array.
Im assuming i might need to convert an array to a byte or something and then
back. Im not really sure what to look for.
I dont know if it is possible or not.
A socket isn't concerned about what the data is, it is only a stream of
bytes. With that in mind, you should be able to simply serialize your
array and then send the results across the wire. The receiving
application, would then simply deserialize the stream of bytes back to
an array.
That sounds simple in theory, but there are several complications to
this. Here are some of them:
1) Is the receiving application a .NET application? If not, then your
going to have to serialize the array into something other then a binary
format. There has to be an agreed upon protocol for representing the
array. If the application is a .net application, then things are
greatly simplified, since you can serialize the array to a byte array
using a binary formatter and then send the resulting byte array.
2) How does the receiving application know that what you sent is an
array? Ok, if all your sending are serialized arrays, then this isn't
a problem. But, if you are sending mixed messages, then you need to
develop a protocol to tell the receiving application that the stream of
bytes you just sent contains an array.
3) How much data does the receiving application have to get before it
has the entire array? Sockets break data up into packets, and so it is
possilbe that you may need to do several reads before you have all the
data in one message. With string data, you don't usually have to know
how much to expect up front, because most protocols specify that string
data is terminated with a \r\n (cariage return, line feed), or some
other special character. That isn't usually possilbe with pure binary
data, so you have to usually send a pre-message of some sort that tells
the receiver that they need to expect a certain amount of data...
Again, you have to come up with a protocol to negotiate with this stuff
with the receiving application.
4) It appears your using UDP for your data transmission. Ok, but this
even complicates things even further. UDP packet sizes are normally
smaller on UDP, which has the affect of making my point 3 even more
relavent - but this is even further complicated by the fact that UDP
does not guarentee that the packets will be sent in a particular order,
or even that they will arrive at all. What does that mean? It means
that you need to manually break your data into chunks (smaller then the
UDP packet size) and append a packet number to the data so that the
receiver can put the data back together in the right order at the other
end, and know if it received all of the data. This also means that if
the receiver doesn't receive all the data, that they can re-request the
packets from the sender (so, you have to usually agree upon a timeout
so that the receiver doesn't wait forever for data that will never
arrive). If I were you, unless UDP was an absolute requirement, I
would seriously consider using TCP sockets for this - they at least
guarentee order and that the data will all be sent. It greatly
decreases the complexity of your send and receives. The client just
has to know up front how much data to expect, and then can read until
it receives that much data - and it doesn't have to reorder anything
since TCP guarentees the packets will be received in order. Of course,
TCP has it's own problems - it is a connected socket, so you can't just
broadcast data out to the network. You have to send it to a specific
endpoint (IP Address/Port).
Anyway, if you have specific questions about any of this - feel free to
ask.