HELP:Weird string problem

M

mspeed

I have written a TCP/IP server. The first thing a client does is send
login info in the form of FirstName LastName|mypassword@ I read this
in using the .GetByte method of a networkstream. I add the input as
entered into a byte array which upon receiving the "@" character I
convert into a string using the .GetChars method of an encoding
object. I then parse out the username and password into string
variables for use elsewhere. When I step through the code I find
something strange. In the Autos window at the bottom of the screen
the username will show as "FirstName LastName" (with the " characters)
but the password part shows as "mypassword with no closing "
character. Additionally, the unclosed string shows in red text and I
cannot use it in any other string. If I build a string and append the
password value anything that comes after the password will not show.
It is almost as though there is some special character at the end of
the string that will not allow anything to come after the last letter
entered before the "@"

Any help would be greatly appreciated.
 
M

Matthew Speed

On Thu, 10 Jul 2003 00:35:03 GMT, (e-mail address removed) wrote:

I figured this out. I was reading the input into a byte array of size
200. After the .GetChars method I needed to do a string =
left(string,numberofcharsreadin) to cut off the null space in the byte
array that didn't get filled by input.
 
M

Matthew Speed

The value you have read is Null terminated, and in VB (like most other
languages) a Null indicates the end of a string (even if there actually IS
more data after the null). Just curious, what encoding object are you using?
I ended up figuring it out. I am using encoding.UTF8.

If there is a better way to do this I'd love to hear it. I have never
written a TCP/IP server before and while I mostly understand what
sockets do I had never actually created one in a program. I am doing
what I was able to get to work, inefficient or not.

My code looks like this:

dim enc as encoding = encoding.UTF8

dim i as int16 = 0
dim cmdin(200) as byte
dim cmdstr as string
dim strm as networkstream = new networkstream(mysocket)
while true
try
cmdin(i) = strm.readbyte
'Check for @ end-of-command indicator
if cmdin(i) = "64" then
exit while
end if
i+=1
if i > 200 then
exit while
end if
catch
mysocket.close()
end try
end while

cmdstr = enc.getchars(cmdin)
cmdstr = left(cmdstr,i)

now I have a string I can work with
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top