VB.NET:System.ArgumentOutOfRangeException

时间:2017-01-15 08:12:12

标签: vb.net tcpclient outofrangeexception

我试图创建一个通过TCP进行通信的多线程套接字程序。我已关注this tutorial,但收到以下错误。

P.S我是网络编程新手

这是堆栈跟踪:

at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at Multi_TCP_Server.Module1.handleClinet.doChat() in \Multi-TCP_Server\Module1.vb:line 57
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

这是服务器代码:(Multi-TCP_Server.vb)

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Module Module1
Sub Main()
    Dim LocalAddr As IPAddress = IPAddress.Parse("192.168.1.10")
    Dim serverSocket As New TcpListener(LocalAddr, 8888)
    Dim clientSocket As TcpClient
    Dim counter As Integer

    serverSocket.Start()
    msg("Server Started")
    counter = 0
    While (True)
        counter += 1
        clientSocket = serverSocket.AcceptTcpClient()
        msg("Client No:" + Convert.ToString(counter) + " started!")
        Dim client As New handleClinet
        client.startClient(clientSocket, Convert.ToString(counter))
    End While

    clientSocket.Close()
    serverSocket.Stop()
    msg("exit")
    Console.ReadLine()
End Sub

Sub msg(ByVal mesg As String)
    mesg.Trim()
    Console.WriteLine(" >> " + mesg)
End Sub

Public Class handleClinet
    Dim clientSocket As TcpClient
    Dim clNo As String
    Public Sub startClient(ByVal inClientSocket As TcpClient,
    ByVal clineNo As String)
        Me.clientSocket = inClientSocket
        Me.clNo = clineNo
        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
        ctThread.Start()
    End Sub
    Private Sub doChat()
        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

        While (True)
            ' 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)
                serverResponse = "Server to clinet(" + clNo + ") " + rCount
                sendBytes = Encoding.ASCII.GetBytes(serverResponse)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                networkStream.Flush()
                msg(serverResponse)
            'Catch ex As Exception
            'MsgBox(ex.ToString)
            'End Try

        End While

    End Sub
End Class
End Module

以下是客户端代码:(Multi-TCP_Client.vb)

Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream


Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles  Button1.Click
    Dim serverStream As NetworkStream = clientSocket.GetStream()
    Dim buffSize As Integer
    Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("Message from Client")
    serverStream.Write(outStream, 32, outStream.Length)
    'serverStream.Flush()

    Dim inStream(10024) As Byte
    buffSize = inStream.Length
    serverStream.Read(inStream, 0, buffSize)
    Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
    msg("Data from Server : " + returndata)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    msg("Client Started")
    Try
        clientSocket.Connect("192.168.1.10", 8888)
        ConnectionStatusLbl.Text = "Server Connected..."
    Catch ex As Exception
        ConnectionStatusLbl.Text = "Unable to connect."
    End Try
End Sub

Sub msg(ByVal mesg As String)
    TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + mesg
End Sub
End Class

感谢所有帮助和想法。 谢谢:))

编辑:Here是来自Multi-TCP_Client.vb的堆栈跟踪

0 个答案:

没有答案