Question Sending string to Server

denjerlog

New member
Joined
Jan 24, 2009
Messages
1
Programming Experience
Beginner
First time using forum. In BS program learning to use VB. Let me know if this question should be in another forum, I will probably have others, as I progress through this program. Thanks.

Problem

I have set up a console version Client program and a server program in Visual Studio- VB.Net

The goal is to input one of five stock symbols, send it to the Server program, open a .txt document, match the symbol to the line of data in the txt document and send the company name and hardcoded stockprice for that stock symbol.

The stock symbol input is held in the variable stockInput on the Client side

stockInput = Console.ReadLine()

While debugging, I can see that the format is "CSC" in stockInput, with quotation marks at beginning and end

When the data is sent to the Server, it is held in the variable clientdata.

However, again while debugging, I can see that the format is "CSC in clientdata, with quotation marks only at the beginning.

I open the .txt file which pulls the txt data into separate variables. The string variables are in the format, "CSC"

I think what is happening is that we don't make it into the if statement because "CSC" does not equal "CSC

If symbol = clientdata Then

response1 = company

response2 = price


Is there another way to bring over the input data?

Should I think about trying to strip all the quotation marks?

The other thing I noticed on the server side in line 46,

networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

While debugging, if you scroll over bytes the length is 8193, if you scroll over tcpClient.ReceiveBufferSize, the length is 8192

Is that normal, or could that be the problem.
 
bytes the length is 8193, if you scroll over tcpClient.ReceiveBufferSize, the length is 8192
You have accidentally created an array one byte larger than the maximum number of bytes you intend to request to be read into that array. It has no consequence other than looking a bit silly since you only operate on bytes actually written to that array anyway. The Read method returns this number.

Why not instead use some kind of writer/reader to write/read the stream? It saves you some code converting and managing the bytes yourself. If you only need to handle single lines you can use the StreamWriter/Reader. This example show how to write and read a string on a NetworkStream:
VB.NET:
Dim writer As New IO.StreamWriter(tcp.GetStream)
writer.AutoFlush = True
writer.WriteLine("hello world")

Dim reader As New IO.StreamReader(tcp.GetStream)
Dim message As String = reader.ReadLine
Close the writer/reader when you close networkstream/client.
 
Back
Top