| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2413 人关注过本帖
标题:求助,如何在成员类函数中访问大类中的friend成员?
只看楼主 加入收藏
zx2015
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-8-11
收藏
 问题点数:0 回复次数:0 
求助,如何在成员类函数中访问大类中的friend成员?
问题描述如下:

在应用窗体Form1内拖放了一个friend的listbox成员,另外申明了一个ChartServer 的成员类,当ChartServer的某个方法触发了某个事件之后,
希望改变应用窗体中的listbox 成员,本想用这个方法,但测试无效,不知何故?编译无错。

 Chart.Form1.ListBox1.Items.Add(Connect.RemoteEndPoint.ToString())

其中 Chart 为程序集名称,调试工具为 2010。试过对lable控件进行赋值,取代Listbox,结果一样。

这是一个网上收集到的TCP的demo源码,现列出供大虾参考。谢谢!




Imports Chart
Imports System.Threading

Public Class Form1

    Private list As New ArrayList
    Private CClient As ChartClient
    Private CServer As ChartServer

    Private CS As Short = 0          '0无任何对象,1服务器,2客户端

    Public Sub New()
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
        CheckForIllegalCrossThreadCalls = False '//关闭跨线程调用抛出异常
    End Sub

    '启动服务器
    Private Sub cmdStartServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartServer.Click
        '初始化一个服务器类,相同同一个端口号只能创建一个服务器 (()).AddressList.ToString()
        'CServer = New ChartServer(, 5000, "server1")
        CServer = New ChartServer("192.168.0.112", 5000, "server1")

        '服务器启动
        CServer.StartServer()
        CS = 1
        cmdSent.Enabled = True
        cmdStartServer.Enabled = False
        cmdStartClient.Enabled = False
        RTxtMsgRecord.Text += "服务器已启动" & vbCrLf
        TxtMsg.Focus()

        '创建一个新的线程启动消息接收消息
        Dim SThread = New Thread(New ThreadStart(AddressOf SReciveMsg))
        SThread.Start()
    End Sub

    Private Sub cmdStartClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartClient.Click
        '定义并初始化一个客户端对象
        CClient = New ChartClient("192.168.0.112", 5000, "client1")
        '启动客户端
        CClient.StartClient()
        CS = 2
        cmdSent.Enabled = True
        cmdStartServer.Enabled = False
        cmdStartClient.Enabled = False
        '创建一个新的线程启动消息接收消息
        Dim CThread = New Thread(New ThreadStart(AddressOf CReciveMsg))
        CThread.Start()
    End Sub

    Private Sub SReciveMsg()
        Do
            Me.RTxtMsgRecord.Text += CServer.RecvieMsg
        Loop
    End Sub

    Private Sub CReciveMsg()
        Do
            Me.RTxtMsgRecord.Text += CClient.RecvieMsg
        Loop
    End Sub

    Private Sub cmdSent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSent.Click
        '判断正在使用的是服务器还是客户端
        If CS = 1 Then
            '服务器
            CServer.SentMsg(TxtMsg.Text)
            Me.RTxtMsgRecord.Text += "Server>>>"
        ElseIf CS = 2 Then
            '客户端
            CClient.SentMsg(TxtMsg.Text)
            Me.RTxtMsgRecord.Text += "我说>>>"
        End If
        Me.RTxtMsgRecord.Text += TxtMsg.Text & vbCrLf
        TxtMsg.Text = ""
        TxtMsg.Focus()
    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        System.Environment.Exit(System.Environment.ExitCode)
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RTxtMsgRecord.Text += TxtMsg.Text
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Label3.Text = "fdsaf"
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        '    Me.ListBox1.Items.Add(Connect.RemoteEndPoint.ToString())
    End Sub

    Private Sub ListAdd(str As String)
        Me.ListBox1.Items.Add(str)
    End Sub

End Class




///////////////////////////////////////////////////////////////////////////////////////////////////////////
ChartServer 类代码:

Imports
Imports
Imports System
Imports System.Threading
Imports

Public Class ChartServer

    Private ReadOnly ServerName As String
    Private ReadOnly ServerIP As IPAddress
    Private ReadOnly PortNum As Integer = 0
    Private ReadOnly ServerTag As String

    Private Conlist As New ArrayList
    Private Connect As Socket = Nothing

    Private SocketStream As NetworkStream
    Private Writer As BinaryWriter
    Private Reader As BinaryReader
    Private NewThread As Thread = Nothing

    Private ServerStarted As Boolean = False  '服务器状态为未启动
    Private ClientCount As Integer = 1
    Private ClientIndex As Integer = 0
    Private str As String
    Const CMAXCOUNT = 30 '容许连接的最大客户端数目

    '初始化函数
    Sub New(ByVal SerIP As IPAddress, ByVal Port As Integer, ByVal SerTag As String)
        ServerIP = SerIP
        PortNum = Port
        ServerTag = SerTag
    End Sub

    Sub New(ByVal SerIP As String, ByVal Port As Integer, ByVal SerTag As String)
        ServerIP = IPAddress.Parse(SerIP)
        PortNum = Port
        ServerTag = SerTag
    End Sub

    '创建服务器
    Private Sub RunServer()
        Dim listener As TcpListener
        Try
            '创建一个侦听对象
            listener = New TcpListener(ServerIP, PortNum)             '启动服务器侦听端口
            listener.Start()

            While True
                '连接客户端,返回一个 套接字
                If ClientCount <= CMAXCOUNT Then
                    Connect = listener.AcceptSocket()
                    '添加到连接列表中
                    Conlist.Add(Connect)
                    Connect.RemoteEndPoint.ToString()
                    Chart.Form1.ListBox1.Items.Add(Connect.RemoteEndPoint.ToString()) 此语句为什么无效?本想在建立连接之后,在应用类中的列表中添加一个IP信息,编译妹报错,但无法刷新 listbox
                    SocketStream = New NetworkStream(Connect)
                    Writer = New BinaryWriter(SocketStream)
                    '通知客户端,连接成功
                    Writer.Write("Server>>>与服务器成功连接" & vbCrLf) '应该在什么时候断开该连接

                    ClientCount += 1
                Else
                    Connect = listener.AcceptSocket()
                    SocketStream = New NetworkStream(Connect)
                    Writer = New BinaryWriter(SocketStream)
                    '通知客户端,连接成功
                    Writer.Write("Server>>> 服务器的连接数已满,连接已经断开")
                    Connect.Disconnect(False) '断开连接
                End If

            End While
        Catch ex As IOException
            MsgBox(ex.Message)
        Finally
            SocketStream = Nothing
            Writer = Nothing
            Connect = Nothing
        End Try
    End Sub

    '该方法在每个实例中仅容许使用一次
    Public Sub StartServer()
        If ServerStarted = False Then
            Try
                '创建新线程,用新的线程启动服务器
                NewThread = New Thread(New ThreadStart(AddressOf RunServer))
                NewThread.Start()

            Catch ex As ThreadStartException
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "线程启动错误")
                '线程创建失败时服务器启动失败
                ServerStarted = False
            End Try

            ServerStarted = True
        Else
            MsgBox("该实例中已经有一个服务器进程在运行,若要创建新的服务器请使用其它实例", _
             MsgBoxStyle.OkOnly, "警告")
        End If

    End Sub

    '发消息给指定客户端
    'Public Sub SentMsg(ByVal ClientIndex As Integer, ByVal str_Msg As String)
    '    Dim Con As Socket
    '    Dim SocketStream As NetworkStream
    '    Dim SentMsg As BinaryWriter
    '    Dim Msg As String = str_Msg
    '    Try
    '        Con = Connect(ClientIndex)
    '        SocketStream = New NetworkStream(Con)
    '        SentMsg = New BinaryWriter(SocketStream)
    '        SentMsg.Write("Server>>>" & Msg)         '发送信息
    '    Catch ex As IndexOutOfRangeException
    '        MsgBox(ex.Message, MsgBoxStyle.OkOnly, "越界异常")
    '    Finally
    '        Con = Nothing
    '        SocketStream = Nothing
    '        SentMsg = Nothing
    '    End Try
    'End Sub
    '发消息给所有客户端

    Public Sub SentMsg(ByVal str_Msg As String)
        Dim Con As Socket
        Dim SocketStream As NetworkStream
        Dim SentMsg As BinaryWriter
        Dim Msg As String = str_Msg

        Dim index As Short
        If Conlist.Count > 0 Then
            For index = 0 To Conlist.Count - 1
                Try
                    Con = Conlist(index)
                    If Con Is Nothing Then
                        Conlist.RemoveAt(index)
                        Continue For
                    End If
                    SocketStream = New NetworkStream(Con)
                    SentMsg = New BinaryWriter(SocketStream)
                    SentMsg.Write("Server>>>" & Msg & vbCrLf)         '发送信息
                Catch ex As IOException
                    MsgBox(ex.Message)
                Finally
                    Con = Nothing
                    SocketStream = Nothing
                    SentMsg = Nothing
                End Try
            Next
        End If

    End Sub

    '接收客户的客户端消息
    Public Function RecvieMsg() As String
        Dim strMsg As String = ""
        Dim Con As Socket
        Dim SStream As NetworkStream

        Dim i As Short

        For i = 0 To Conlist.Count - 1
            Con = Conlist.Item(i)
            If Con Is Nothing Then
                Conlist.RemoveAt(i)
                Continue For
            End If
            SStream = New NetworkStream(Con)
            Reader = New BinaryReader(SStream)
            Try
                strMsg = Reader.ReadString
                Return strMsg
            Catch ex As SocketException
                MsgBox(ex.Message)
            Finally
                Reader = Nothing
                SStream = Nothing
                Con = Nothing
            End Try
        Next

        Return strMsg
    End Function


End Class



///////////////////////////////////////////////////////////////////////////////////////////////////////////
ChartClient 类代码:

Imports
Imports
Imports System
Imports System.Threading
Imports

Public Class ChartClient

    Private ServerName As String
    Private ServerIP As IPAddress
    Private clientTag As String
    Private PortNum As Integer = 0
    Private Connect As Socket = Nothing

    Private SocketStream As NetworkStream
    Private Writer As BinaryWriter
    Private Reader As BinaryReader
    Private NewThread As Thread = Nothing
    '初始化函数
    Sub New(ByVal SerIP As IPAddress, ByVal Port As Integer, ByVal Tag As String)
        ServerIP = SerIP
        PortNum = Port
        clientTag = Tag
    End Sub
    Sub New(ByVal SerIP As String, ByVal Port As Integer, ByVal Tag As String)
        ServerIP = IPAddress.Parse(SerIP)
        PortNum = Port
        clientTag = Tag
    End Sub
    Public Sub StartClient()
        ' 初始化一个客户端对象
        Dim Client As New TcpClient
        Try
            '连接服务器
            Client.Connect(ServerIP, 5000)
            SocketStream = Client.GetStream
            Reader = New BinaryReader(SocketStream)
            Writer = New BinaryWriter(SocketStream)
            Writer.Write(clientTag & "连接成功" & vbCrLf)
            MsgBox(Reader.ReadString())
        Catch ex As Exception

        End Try
    End Sub

    Public Function RecvieMsg() As String
        Dim strMsg As String = ""
        If SocketStream Is Nothing Then
            Return "与服务器的连接已断开"
            Exit Function
        End If
        Reader = New BinaryReader(SocketStream)
        Try
            strMsg = Reader.ReadString
            Return strMsg
        Catch ex As IOException
            MsgBox(ex.Message)
        Finally
            Reader = Nothing
        End Try
        Return strMsg
    End Function
    '向服务器发送消息
    Public Sub SentMsg(ByVal str_Msg As String)
        Dim Msg As String = str_Msg
        If Not SocketStream Is Nothing Then
            'Writer = New BinaryWriter(SocketStream)
            Writer.Write(clientTag & ">>>" & vbCrLf & Msg & vbCrLf)
        End If
    End Sub
End Class

///////////////////////////////////////////////////////////////////////////////////////////////////////////
搜索更多相关主题的帖子: friend 如何 网上 
2015-08-11 17:15
快速回复:求助,如何在成员类函数中访问大类中的friend成员?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034767 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved