如何在子例程中打开tcpClient并在单独的子例程中写入/接收 [英] How to Open a tcpClient in a subroutine and write/ receive in a separate subroutine

查看:83
本文介绍了如何在子例程中打开tcpClient并在单独的子例程中写入/接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过tcpClient / server程序控制继电器的数量。我想在应用程序启动时使用EthConnectionInitlize()子例程打开一个连接,并仅在按下某个按钮时发送一些代码字。我不希望每次按下按钮时都创建一个客户端。我正在尝试如下操作。

 公共  Sub  EthConnectionInitlize( ByVal  IPAddress  As  字符串 ByVal  PortNo  As  整数
尝试
client = TcpClient(IPAddress,PortNo)
lblEthLinkStatus.Text = 字符串 .Concat( 链接状态:已建立链接
lblEthLinkStatus.ForeColor = Color.Navy
EthConnectionStatus = True
Catch ex As 异常
Console.WriteLine(ex)
Dim errorResult 作为 字符串 = ex.Message
lblEthLinkStatus.ForeColor = Color.Red
lblEthLinkStatus .Text = 字符串 .Concat( 错误:,errorResult, 无法建立链接
EthConnectionStatus = 错误
结束 尝试
结束 Sub
私人 < span class =code-keyword> Sub WriteToEth( ByVal ContrlWord 作为 字符串
Dim tcpWriter 作为
如果 EthConnectionStatus = < span class =code-keyword> True 然后
tcpWriter.WriteLine(strControlword.ToUpper)
tcpWriter.Flush()
txtControlWord.Text = strControlword.ToUpper
lblEthLinkStatus.ForeColor = Color.Black
Else
MessageBox.Show(< span class =code-string> 以太网连接未建立
结束 如果
结束 Sub





但写入时无法访问客户端ToEth()。

Pl帮助。怎么去???

解决方案

请看我对这个问题的评论。你抱怨但客户无法访问...是荒谬的。你需要记住,你是那个定义什么是可接受的人。例如,您的客户端可以是同一个类的实例成员(最常见的是私有成员),以及实例方法的声明类 WriteToEth ;那么这个方法可以访问客户端



初始化客户端可以使用 lazy 模式完成,也就是说,只有当你第一次使用它时,或者在它的声明类的生命周期的最开始时才完成,比如说,构造函数。另见:

https://en.wikipedia.org/wiki/Lazy_initialization [< a href =https://en.wikipedia.org/wiki/Lazy_initializationtarget =_ blanktitle =New Window> ^ ],

https://msdn.microsoft.com/en-us/library/dd642331%28v=vs .110%29.aspx [ ^ ](您不需要使用此课程,只是为了您的方便)。



此外,如果你开发一些UI,使用TCP客户端(或其他任何东西)的所有网络通信应该在一个单独的线程中完成,所以你还需要学习线程和UI线程之间的线程和通信。



等等......你不应该把我的建议作为一些解决方案。相反,这是对软件开发应该真正设计代码的想法的说明,自由地组合了不同选项的所有组成部分的不同方面以及平台和语言的表达能力。你抱怨的那些问题不应该被视为问题;它应该都在你手指的尖端。 (确实存在问题,其中很多。)如何实现?学习理论和实践,更重要的是,在更简单的项目上提高你的技能,最初可能是纯粹的学习项目。



-SA

I am controlling number of relays through a tcpClient / server program. I want to open a connection at the start of application using EthConnectionInitlize () subroutine and send some codeword only when some button is pressed. I don't want to create a client everytime when a button a pressed .Ii am trying to do with as below.

Public Sub EthConnectionInitlize(ByVal IPAddress As String, ByVal PortNo As Integer)
        Try
            client = New TcpClient(IPAddress, PortNo)
            lblEthLinkStatus.Text = String.Concat("Link Status : Link Established")
            lblEthLinkStatus.ForeColor = Color.Navy
            EthConnectionStatus = True
        Catch ex As Exception
            Console.WriteLine(ex)
            Dim errorResult As String = ex.Message
            lblEthLinkStatus.ForeColor = Color.Red
            lblEthLinkStatus.Text = String.Concat("Error : ", errorResult, " Unable to establish Link")
            EthConnectionStatus = False
        End Try
    End Sub
    Private Sub WriteToEth(ByVal ContrlWord As String)
        Dim tcpWriter As New
        If EthConnectionStatus = True Then
            tcpWriter.WriteLine(strControlword.ToUpper)
            tcpWriter.Flush()
            txtControlWord.Text = strControlword.ToUpper
            lblEthLinkStatus.ForeColor = Color.Black
        Else
            MessageBox.Show("Ethernet Connection Not Established")
        End If
    End Sub



But client is not accessible in WriteToEth().
Pl Help. how to go about???

解决方案

Please see my comment to the question. Your complain "but client is not accessible…" is absurd. You need to remember that you are the one who define what is acceptable where. For example, your client could be the instance member (most typically, a private member) of the same class and the declaring class of the instance method WriteToEth; then this method can access client.

Initialization of client could be done using the lazy pattern, that is, only when you use it for the very first time, or be done at the very beginning of the lifetime of its declaring class, say, in its constructor. See also:
https://en.wikipedia.org/wiki/Lazy_initialization[^],
https://msdn.microsoft.com/en-us/library/dd642331%28v=vs.110%29.aspx[^] (you don't need to use this class, it's only for your convenience).

Besides, if you develop some UI, all the network communications using the TCP client (or anything else) should rather be done in a separate thread, so you also need to learn threading and communication between threads and the UI thread.

And so on… You should not take my suggestions as some solution. Rather, this is the illustration of the idea that a software development should really design the code, freely combining different aspects of all the repertoire of different options and expressive capabilities of the platform and the languages. Such "problems" as the one you complain about should not be considered as problems; it should be all on the tips of your finger. (There are real problem, a lot of them.) How to achieve that? Learning of theory and practice, and, importantly, sharpening your skills on much simpler projects, which could be purely study projects at first.

—SA


这篇关于如何在子例程中打开tcpClient并在单独的子例程中写入/接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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