Hello, I am trying to create a "stock Price Program" for an assignment that will allow you to type in a stock symbol such as "goog" in the client side and the server side will respond with a price for that stock symbol, this price can either be hard coded in or read from a text file. I have done a lot of research and have a good understanding of how sockets work.

My issue is that I do not understand how to get the server to reply based on what the client sends it. I have a working program that allows you to send text to the server and it should reply with the same text and date but i think the client is just re-typing the text instead of receiving it from the server and I can't figure out how to get the client to receive back from the server

Here is my code for the client:

Imports System.IO

Imports System.Net.Sockets

Module Module1

    Sub Main()


            Console.WriteLine("Connecting to 8585 Local Host")

            Dim serverListener As New TcpClient("localhost", 8585)

            Console.WriteLine("Input Lines:")

            Dim str As String = Console.ReadLine()
            Dim Bytes(1024) As Byte
            Dim datas As String = Nothing

            While True

                Dim sendBuff As Byte() = System.Text.Encoding.ASCII.GetBytes(str)
                Dim readStream As NetworkStream = serverListener.GetStream

                readStream.Write(sendBuff, 0, sendBuff.Length)

                If str.StartsWith(".") Then

                    GoTo Done

                End If

                datas = Nothing

                Dim i As Int32

                ' Loop to receive all the data sent by the client.
                i = readStream.Read(Bytes, 0, Bytes.Length)
                While (i <> 0)
                    ' Translate data bytes to a ASCII string.
                    datas = System.Text.Encoding.ASCII.GetString(Bytes, 0, i)
                    Console.WriteLine("Received: {0}", datas)

                    ' Process the data sent by the client.
                    datas = datas.ToUpper()
                    Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(datas)

                    str = Console.ReadLine()

                End While
            End While

Done:       Console.WriteLine("Done")

        Catch exp As Exception

            Console.WriteLine("Exception: " + exp.ToString())

        End Try

    End Sub

End Module

And the code for the server :

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic

Class MyTcpListener

    Public Shared Sub Main()

        Dim server As TcpListener
        server = Nothing
        ' Set the TcpListener on port 8585.
            Dim port As Int32 = 8585
            Dim localAddr As IPAddress = IPAddress.Parse("")

            server = New TcpListener(localAddr, port)

            ' Start listening for client requests.

            ' Buffer for reading data
            Dim bytes(1024) As Byte
            Dim data As String = Nothing

            ' Enter the listening loop.
            While True
                Console.Write("Waiting for a connection... ")

                ' Perform a blocking call to accept requests.
                ' You could also user server.AcceptSocket() here.
                Dim client As TcpClient = server.AcceptTcpClient()

                data = Nothing

                ' Get a stream object for reading and writing
                Dim stream As NetworkStream = client.GetStream()

                Dim i As Int32

                ' Loop to receive all the data sent by the client.
                i = stream.Read(bytes, 0, bytes.Length)
                While (i <> 0)

                    ' Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    Console.WriteLine("Received: {0}", data)

                    ' Process the data sent by the client.
                    data = data.ToUpper()
                    Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)

                    ' Send back a response.
                    stream.Write(msg, 0, msg.Length)
                    Console.WriteLine("Sent: {0}", data & Format(#5/31/1993#, "dddd, d MMM yyyy"))

                    i = stream.Read(bytes, 0, bytes.Length)

                End While

                ' Shutdown and end connection
            End While
        Catch e As SocketException
            Console.WriteLine("SocketException: {0}", e)

        End Try

        Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
    End Sub 'Main

End Class 'MyTcpListener

I need some direction, it's been quite awhile since i've done any coding so any help is appreciated! Also if you have any insight on creating different threads that would be helpful, we are supposed to have it allow only 5 connections on different threads.

Thanks, please ask if you need any more information.

Any help would be great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.