多客户端VB2008 Express [英] Multi Clients VB2008 Express

查看:59
本文介绍了多客户端VB2008 Express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个

http://msdn.microsoft.com/en-us/library/aa478452.aspx

http://msdn.microsoft.com/en-us/library/aa478452.aspx

我按照它创建了它,它可以运行但是有一些问题.

and I created it follow that, it can run but have some problem.

...客户端向服务器发送了数据并更新了服务器上的显示,但是服务器没有将数据回复给客户端.

...Client sent data to server and update display on server, but server don't reply data to client.

这是我的代码;

服务器

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

Public Class SocketServer

    Private mcolClients As New Hashtable()
    Private mobjListener As TcpListener
    Private mobjThread As Threading.Thread '***

    Public Delegate Sub StatusInvoker(ByVal t As String)
    Private Delegate Sub UpdateStatusCallBack(ByVal t As String) '***

    Private Sub UpdateStatus(ByVal t As String)
        If lstStatus.InvokeRequired Then
            lstStatus.Invoke(New UpdateStatusCallBack(AddressOf UpdateStatus), t)
        Else
            lstStatus.Items.Add(t)
            lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
        End If
    End Sub

    Private Sub DoListen()
        Try
            mobjListener = New TcpListener(IPAddress.Any, 15000)

            mobjListener.Start()
            Do
                Dim x As New Client(mobjListener.AcceptTcpClient)

                AddHandler x.Connected, AddressOf OnConnected
                AddHandler x.Disconnected, AddressOf OnDisconnected
                AddHandler x.LineReceived, AddressOf OnLineReceived
                mcolClients.Add(x.ID, x)

                Dim params() As Object = {"New connection"}
                Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
            Loop Until False
        Catch
        End Try
    End Sub

    Private Sub SocketServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mobjThread = New Threading.Thread(AddressOf DoListen)
        mobjThread.Start()
        UpdateStatus("Listener started")
    End Sub

    Private Sub SocketServer_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        mobjListener.Stop()
    End Sub

    Private Sub OnConnected(ByVal sender As Client)
        UpdateStatus("Connected")
    End Sub

    Private Sub OnDisconnected(ByVal sender As Client)
        UpdateStatus("Disconnected")
        mcolClients.Remove(sender.ID)
    End Sub

    Private Sub OnLineReceived(ByVal sender As Client, ByVal Data As String)
        UpdateStatus("Line: " & Data)

        Dim objClient As Client
        Dim d As DictionaryEntry

        For Each d In mcolClients
            objClient = d.Value
            objClient.Send(Data & vbCrLf)
        Next
    End Sub



End Class


'Client Class**************************************************************
Public Class Client

    Public Event Connected(ByVal sender As Client)
    Public Event Disconnected(ByVal sender As Client)
    Public Event LineReceived(ByVal sender As Client, ByVal Data As String)

    Private mgID As Guid = Guid.NewGuid
    Private mobjClient As TcpClient
    Private marData(1024) As Byte
    Private mobjText As New StringBuilder()

    Public ReadOnly Property ID() As String
        Get
            Return mgID.ToString
        End Get
    End Property


    Public Sub New(ByVal client As TcpClient)
        mobjClient = client
        RaiseEvent Connected(Me)
        mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoReceive, Nothing)
    End Sub


    Private Sub DoReceive(ByVal ar As IAsyncResult) 'DoStreamReceive
        Dim intCount As Integer

        Try
            SyncLock mobjClient.GetStream
                intCount = mobjClient.GetStream.EndRead(ar)
            End SyncLock
            If intCount < 1 Then
                RaiseEvent Disconnected(Me)
                Exit Sub
            End If

            BuildString(marData, 0, intCount)

            SyncLock mobjClient.GetStream
                mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoReceive, Nothing)
            End SyncLock
        Catch e As Exception
            RaiseEvent Disconnected(Me)
        End Try
    End Sub

    Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
        Dim intIndex As Integer

        For intIndex = offset To offset + count - 1
            If Bytes(intIndex) = 13 Then
                RaiseEvent LineReceived(Me, mobjText.ToString)
                mobjText = New StringBuilder()
            Else
                mobjText.Append(ChrW(Bytes(intIndex)))
            End If
        Next
    End Sub

    Public Sub Send(ByVal Data As String)
        SyncLock mobjClient.GetStream
            Dim w As New IO.StreamWriter(mobjClient.GetStream)
            w.Write(Data)
            w.Flush()
        End SyncLock
    End Sub


End Class

    

客户

    

CLIENT

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

Public Class SocketClient

    Private mobjClient As TcpClient
    Private marData(1024) As Byte
    Private mobjText As New StringBuilder()

    Public Delegate Sub DisplayInvoker(ByVal t As String)

    Private Sub DisplayText(ByVal t As String)
        txtDisplay.AppendText(t)
    End Sub

    Private Sub SocketClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mobjClient = New TcpClient("192.168.26.108", 15000) '("localhost", 15000) '("192.168.26.108", 15000)
        DisplayText("Connected to 192.168.26.108" & vbCrLf)
    End Sub

    Private Sub Send(ByVal Data As String)
        Dim w As New IO.StreamWriter(mobjClient.GetStream)
        w.Write(Data & vbCr)
        w.Flush()
    End Sub

    Private Sub btnSent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Send(txtSend.Text)
        'DisplayText(txtSend.Text & vbCrLf)
        txtSend.Text = ""
        txtSend.Focus()
    End Sub

    Private Sub DoRead(ByVal ar As IAsyncResult)
        Dim intCount As Integer

        Try
            intCount = mobjClient.GetStream.EndRead(ar)
            If intCount < 1 Then
                MarkAsDisconnected()
                Exit Sub
            End If

            BuildString(marData, 0, intCount)

            mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
        Catch e As Exception
            MarkAsDisconnected()
        End Try
    End Sub

    Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
        Dim intIndex As Integer

        For intIndex = offset To offset + count - 1
            If Bytes(intIndex) = 10 Then
                mobjText.Append(vbLf)

                Dim params() As Object = {mobjText.ToString}
                Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)

                mobjText = New StringBuilder()
            Else
                mobjText.Append(ChrW(Bytes(intIndex)))
            End If
        Next
    End Sub









    '*** When the server disconnects, prevent further chat messages from being sent.
    'Private Sub MarkAsDisconnected()
    '    txtSend.ReadOnly = True
    '    btnSend.Enabled = False
    'End Sub

    Private Sub MarkAsDisconnected()
        DisplayText("Connection failed!" & vbCrLf)
    End Sub
End Class

推荐答案

我们有一个论坛

We have a forum Windows Communication Foundation, Serialization, and Networking for system.net namespace issues. In order to provide better support, I'll move this thread.

感谢您的理解.

最好的问候


这篇关于多客户端VB2008 Express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆