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:
Any help or a pointer in the right direction would be greatly appreciated.
Cheers,
Chris
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