application to listen and send

CCrawshaw

Member
Joined
May 19, 2011
Messages
8
Programming Experience
Beginner
Hi,
I'm trying to create an application to send and receive strings to an ip address.
I have the send part working okay, but as soon as I try to receive strings, it locks the application until it receives one.

Currently, the receiving part is done on a button click, but ideally I would like to have it going on permanently in the background, and still be able to send strings on a button click.

The local ip address is 10.0.0.14, remote is 10.0.0.25 on port 4000. The code I currently have:

VB.NET:
Imports System.Net.Sockets
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Sub startListener()
        Dim f As Integer = 0
        Dim temp As Integer
        Dim ipaddress(3) As Byte
        ipaddress(0) = 10
        ipaddress(1) = 0
        ipaddress(2) = 0
        ipaddress(3) = 14
        Dim opensock As New Net.Sockets.TcpListener(New Net.IPAddress(ipaddress), 4000)
        Try
            opensock.Start()
        Catch ex As System.Net.Sockets.SocketException
            TextBox1.Text = ex.NativeErrorCode
        End Try
        TextBox1.Text = "Connected"

        Dim tcpClient As TcpClient = opensock.AcceptTcpClient()

        'While f < 5
        TextBox1.Text = "Inside loop"
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        TextBox1.Text = "Server Waiting..."
        TextBox1.Text = "String received: " & returndata

        temp = 40 - (40 * CInt("&H" & (returndata.Substring(12, 2))) / 255)
        templabel.Text = temp

        TextBox1.Text = "temp is: " & temp
        'End While
        TextBox1.Text = "temp is: " & temp & "Loop Closed"
        'opensock.EndAcceptSocket()
        'opensock.EndAcceptTcpClient()

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim opensock As New System.Net.Sockets.TcpClient
        Dim swon As String = "A55A6B0550000000FFFBDE0030C8" 'switch on
        Dim swoff As String = "A55A6B0570000000FFFBDE0030E8" 'switch off
        Dim but0 As String = "A55A6B0500000000FFFBDE002066" 'button release

        opensock.Connect("10.0.0.25", 4000)
        Console.WriteLine("connected")

        Dim networkStream As NetworkStream = opensock.GetStream()

        If networkStream.CanWrite And networkStream.CanRead Then
            'SWITCH ON:
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(swon)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            Dim bytes(opensock.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(opensock.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)

            'BUTTON RELEASE:
            sendBytes = Encoding.ASCII.GetBytes(but0)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            networkStream.Read(bytes, 0, CInt(opensock.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            returndata = Encoding.ASCII.GetString(bytes)
            opensock.Close()
        End If
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim opensock As New System.Net.Sockets.TcpClient
        Dim swon As String = "A55A6B0550000000FFFBDE0030C8" 'switch on
        Dim swoff As String = "A55A6B0570000000FFFBDE0030E8" 'switch off
        Dim but0 As String = "A55A6B0500000000FFFBDE002066" 'button release

        opensock.Connect("10.0.0.25", 4000)
        Console.WriteLine("connected")

        Dim networkStream As NetworkStream = opensock.GetStream()

        If networkStream.CanWrite And networkStream.CanRead Then
            'SWITCH OFF:
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(swoff)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            Dim bytes(opensock.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(opensock.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)

            'BUTTON RELEASE:
            sendBytes = Encoding.ASCII.GetBytes(but0)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            networkStream.Read(bytes, 0, CInt(opensock.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            returndata = Encoding.ASCII.GetString(bytes)
            opensock.Close()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.startListener()
    End Sub
End Class

Any help or a pointer in the right direction would be greatly appreciated.

Cheers,
Chris
 
listening application

Hi, this is a slight repost, but as I can get no further with my previous question, I'll re-ask it having gone back a few steps.

I'm trying to create an application to listen on a certain ip address and port. Every so often a string will be sent to the ip address which I need to work with. I have got this working so dar, but only in that the application locks up until the string is received, where as I need it listening in the background so I can get the application to do other things.

I think I have a lack of understanding somewhere, if someone could point me in the right direction to look I'd be very grateful.

I have searched the net a lot, but only seem to find the standard listeners which I'm recreating.

Cheers,
Chris

VB.NET:
Imports System.Net.Sockets
Imports System.Text

Public Class templistener
    Dim ipaddress(3) As Byte
        

    Dim msg As String = "0000000000000000000000000000"
    Dim t As Integer = "00"
    Dim returndata As String = msg
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        createListener()
    End Sub
    Public Sub createListener()
        ipaddress(0) = 10
        ipaddress(1) = 0
        ipaddress(2) = 0
        ipaddress(3) = 14
        Dim opensock As New Net.Sockets.TcpListener(New Net.IPAddress(ipaddress), 4000)


        Try
            opensock.Start()
        Catch ex As System.Net.Sockets.SocketException
            mess.Text = "cannot start connection"
        End Try

             Dim tcpClient As TcpClient = opensock.AcceptTcpClient()


            mess.Text = "connected"
            comm.Text = msg


            Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                mess.Text = "Can read and write"
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                returndata = Encoding.ASCII.GetString(bytes)

                comm.Text = returndata
                mess.Text = "data received"
                'Console.WriteLine(("TCP Server returned: " + returndata))
            Else
                returndata = msg
            End If

        t = 40 - (40 * CInt("&H" & (returndata.Substring(12, 2))) / 255)
        temp.Text = t
    End Sub


End Class
 
You need to research multi-threading, to avoid tying up the UI thread long-running/blocking calls must be performed on a secondary thread.
 
Hi,
Many thanks for that. I now have my application listening in the background, and also it doesn't stop listening as I have added a while true.
My next question is, I now need to add the ability to send strings to the remote ip address on a button click as well as receiving them.
To do this, I assume that when the button is clicked, I need to stop the background worker, open the tcpclient, send the string then restart the background_worker.
Is this correct?

Cheers,
Chris

P.S. I've attached a text file of my code rather than fill the page with 150 lines of code!
 

Attachments

  • codelistener.txt
    5.8 KB · Views: 66
How is the communication protocol defined?
 
Do you mean whether it's TCP/IP or UDP?
The remote unit is currently set to TCP, but I can change it to UDP if it would make things simpler. I left it as TCP as I have never used UDP before.
 
No, I mean how is the communication you engage supposed to take place? The protocol defines how you are supposed to behave during socket connection.
 
Ok, the application needs to be able to send and receive 28 characters string to and from the remote unit defined by an ip address and port.
 
I don't understand what that is supposed to mean. Anyway, you can write to the NetworkStream from your button handler while at the same time having a blocking Read receive operation on 'listen' thread, you just need to provide access to the TcpClient/NetworkStream, as in assign it a class field.
 
Back
Top