Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
VB
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???
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jun-15 15:12pm    
It sounds line an artificial problem, something which does not exist in real development, could only appear due to lack of familiarity with basics of general programming. A software engineer freely decide what code goes to what method.
Of course your TcpClient instance can and should be reused and possibly kept in connected state for prolonged time. It doesn't have to be created on some button press. (You can use the lazy design pattern.)

I don't see where you may need some help. I would rather advise that you may need to get more confident in general programming solving simpler problem that networking...

—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900