Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Problems

Status
Not open for further replies.

WJProctor

Programmer
Jun 20, 2003
151
GB
Hi, im having a couple of problems with strings. When i
use this code.

Dim Data(numBytes) As Byte
_Socket.Receive(Data)
TempData = System.Text.ASCIIEncoding.ASCII.GetString(Data)

The string it returns is always in this format "String
and i cant understand why it is not in the
format "String" could someone explain what is going on? I should say this is when i am debuging the code and putting my mouse over varible at run time to see their value.
Its a bit annoying because i can compare strings.

Thanks

James


James Proctor

 
The return value of the .Receive() method tells you how many bytes you actually received (which may be less than you were expecting due to the network breaking your data up across packets). You can use that value to trim your string to the correct length.

If you're receiving data of a known length (i.e. record-based communications), you should loop around the call to .Receive() until you get at least as much as your record size, at which time you can copy your bytes into a record object, and save any remainder for the next record you receive.

If you're receiving varible-length data, you'll want to send the length of the data as the first few bytes so the receiving side knows how much data to expect. The good news is with .NET, you don't have to fool around with calls to htonl() and ntohl() like you used to have to do -- it does that for you.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top