卡在套接字消息接收上

时间:2012-02-19 23:34:11

标签: vb.net

我的客户端程序卡在NetworkStream.Read上,即使服务器正在报告它正在发送消息。客户端和服务器在同一台计算机上运行。

完整服务器代码(控制台应用程序)

Imports System.Net.Sockets
Imports System.Text

Module Module1
    Sub Main()
        'Dim address As New System.Net.IPAddress("127.0.0.1")
        'Dim serverSocket As New TcpListener(address, 8888)
        Dim serverSocket As New TcpListener(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 handleClient
            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 handleClient
        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 client(" + 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

完整客户端代码(表单应用程序)

Public Class CSocketClient
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream

    'Connect
    Public Sub init(ByVal address As String, ByVal port As Integer)
        clientSocket.Connect(address, port)
    End Sub

    'Run once
    Public Sub runOnce()
        sendMSG("Message1 from Client$")
        receiveMSG()
        sendMSG("Message2 from Client$")
        receiveMSG()
    End Sub

    'Send msg
    Public Sub sendMSG(ByVal msg As String)
        Dim serverStream As NetworkStream = clientSocket.GetStream()
        Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(msg)
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
    End Sub

    'Receive msg
    Public Sub receiveMSG()
        Dim inStream(10024) As Byte
        Dim buffSize As Integer = clientSocket.ReceiveBufferSize
        MsgBox("X")
        serverStream.Read(inStream, 0, buffSize)
        MsgBox("Y")
        Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
        MsgBox("Data from Server : " & returndata)
    End Sub
End Class

使用客户端:

Dim client As New CSocketClient
client.init("127.0.0.1", 8888)
client.runOnce()

我添加了MsgBox("X") and MsgBox("Y")来验证它没有超过serverStream.Read。 “X”弹出正常,它永远不会达到Y.

有人能说出为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

修正了它。发送和接收需要共享同一个NetworkStream ..愚蠢的错误!

Public Sub runOnce()
    Dim serverStream As NetworkStream = clientSocket.GetStream()
    sendMSG("Message1 from Client$", serverStream)
    receiveMSG(serverStream)
End Sub