5 Replies - 924 Views - Last Post: 01 June 2013 - 11:21 AM Rate Topic: -----

#1 rodit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 13

I cannot solve this problem. It keeps giving me the same error!

Posted 29 May 2013 - 11:22 AM

For some reason that I do not understand, I keep getting this error when I try to connect to my LAN messaging server. (console application - Module posted below) The error is - 'Specified argument was out of the range of valid values. Parameter name: size'

Code for console application that starts the server
Imports System.Net.Sockets

Imports System.Text

Module Module1

    Dim clientsList As New Hashtable

    Sub Main()

        Dim serverSocket As New TcpListener(8888)

        Dim clientSocket As TcpClient

        Dim infiniteCounter As Integer

        Dim counter As Integer

        serverSocket.Start()

        msg("Chat Server Started ....")

        counter = 0

        infiniteCounter = 0

        For infiniteCounter = 1 To 2

            infiniteCounter = 1

            counter += 1

            clientSocket = serverSocket.AcceptTcpClient()

            Dim bytesFrom(10024) As Byte

            Dim dataFromClient As String

            Dim networkStream As NetworkStream = _
            clientSocket.GetStream()

            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize)) 'HERE IS WHERE IT GIVES THE ERROR

            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)

            dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))

            clientsList(dataFromClient) = clientSocket

            broadcast(dataFromClient + " Joined ", dataFromClient, False)

            msg(dataFromClient + " Joined chat room ")

            Dim client As New handleClinet

            client.startClient(clientSocket, dataFromClient, clientsList)

        Next

        clientSocket.Close()

        serverSocket.Stop()

        msg("exit")

        Console.ReadLine()

    End Sub

    Sub msg(ByVal mesg As String)

        mesg.Trim()

        Console.WriteLine(" >> " + mesg)

    End Sub

    Private Sub broadcast(ByVal msg As String, _
    ByVal uName As String, ByVal flag As Boolean)

        Dim Item As DictionaryEntry

        For Each Item In clientsList

            Dim broadcastSocket As TcpClient

            broadcastSocket = CType(Item.Value, TcpClient)

            Dim broadcastStream As NetworkStream = _
            broadcastSocket.GetStream()

            Dim broadcastBytes As [Byte]()

            If flag = True Then
                broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)

            Else

                broadcastBytes = Encoding.ASCII.GetBytes(msg)

            End If

            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)

            broadcastStream.Flush()

        Next

    End Sub

    Public Class handleClinet

        Dim clientSocket As TcpClient

        Dim clNo As String

        Dim clientsList As Hashtable

        Public Sub startClient(ByVal inClientSocket As TcpClient, _
        ByVal clineNo As String, ByVal cList As Hashtable)

            Me.clientSocket = inClientSocket

            Me.clNo = clineNo

            Me.clientsList = cList

            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)

            ctThread.Start()

        End Sub

        Private Sub doChat()

            Dim infiniteCounter As Integer

            Dim requestCount As Integer

            Dim bytesFrom(10024) As Byte

            Dim dataFromClient As String

            Dim sendBytes As [Byte]()

            Dim serverResponse As String

            Dim rCount As String

            requestCount = 0

            For infiniteCounter = 1 To 2

                infiniteCounter = 1

                Try

                    requestCount = requestCount + 1

                    Dim networkStream As NetworkStream = _
                    clientSocket.GetStream()

                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))

                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)

                    dataFromClient = _
                    dataFromClient.Substring(0, dataFromClient.IndexOf("$"))

                    msg("From client - " + clNo + " : " + dataFromClient)

                    rCount = Convert.ToString(requestCount)

                    broadcast(dataFromClient, clNo, True)

                Catch ex As Exception

                    MsgBox(ex.ToString)

                End Try

            Next

        End Sub

    End Class

End Module



This is the corresponding form application code that has the same error on the line given below:
Imports System.Net.Sockets

Imports System.Text

Public Class Form1

    Dim clientSocket As New System.Net.Sockets.TcpClient()

    Dim serverStream As NetworkStream

    Dim readData As String

    Dim infiniteCounter As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)

        serverStream.Flush()

    End Sub

    Private Sub msg()

        If Me.InvokeRequired Then

            Me.Invoke(New MethodInvoker(AddressOf msg))

        Else

            TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        readData = "Conected to Chat Server ..."

        msg()

        clientSocket.Connect(textbox4.text, 8888)

        'Label1.Text = "Client Socket Program - Server Connected ..."

        serverStream = clientSocket.GetStream()

        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")

        serverStream.Write(outStream, 0, outStream.Length)

        serverStream.Flush()

        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)

        ctThread.Start()

    End Sub

    Private Sub getMessage()

        For infiniteCounter = 1 To 2

            infiniteCounter = 1

            serverStream = clientSocket.GetStream()

            Dim buffSize As Integer

            Dim inStream(10024) As Byte

            buffSize = clientSocket.ReceiveBufferSize

            serverStream.Read(inStream, 0, buffSize) 'HERE IS WHERE I GET THE ERROR

            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)

            readData = "" + returnData

            msg()

        Next

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class



I am baffled by this error and do not know how to fix it. I would appreciate if anyone could help!

Please download my project code from the attached file to see the error. (you must first launch the console application and then the form application. Press connect on the form application and the error will come up. Do not bother filling in any of the text boxes, they are not important.

Attatchment:
Attached File  msgProj.zip (153.62K)
Number of downloads: 19

Is This A Good Question/Topic? 0
  • +

Replies To: I cannot solve this problem. It keeps giving me the same error!

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8928
  • View blog
  • Posts: 33,453
  • Joined: 12-June 08

Re: I cannot solve this problem. It keeps giving me the same error!

Posted 29 May 2013 - 11:26 AM

79	            serverStream.Read(inStream, 0, buffSize) 'HERE IS WHERE I GET THE ERROR


Have you put a breakpoint on that line and inspected what the values of, say, buffSize is?
Was This Post Helpful? 0
  • +
  • -

#3 rodit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 13

Re: I cannot solve this problem. It keeps giving me the same error!

Posted 29 May 2013 - 11:31 AM

There is no break point and the buffSize is 65536
Was This Post Helpful? 0
  • +
  • -

#4 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: I cannot solve this problem. It keeps giving me the same error!

Posted 30 May 2013 - 12:28 AM

you can put break point by pressing (f9 in VS10) key to check the values for buffsize.

setting breakpoints

This post has been edited by kai_itz me: 30 May 2013 - 12:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 rodit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 13

Re: I cannot solve this problem. It keeps giving me the same error!

Posted 01 June 2013 - 10:52 AM

I have tried that. the buffsize is 65536
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3191
  • View blog
  • Posts: 10,685
  • Joined: 12-December 12

Re: I cannot solve this problem. It keeps giving me the same error!

Posted 01 June 2013 - 11:21 AM

I'm all in favour of a little experimentation ;). Try reducing buffSize (by 1 ?!). If it works, then you can spend time finding out why it didn't work previously.

Programming (and, to an extent, debugging) can be a precise science, but there is always room for a little experimenting.

This post has been edited by andrewsw: 01 June 2013 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1