So I have got it to work but it only does it once then throws exception target machine is actively refusing it .I guess now I need to set up a loop to continuously send and listen between both programs , but how would I get that done. Kind of a newb exploring waters for something bigger
Netduino App
Imports System.NetImports System.Net.SocketsImports Microsoft.SPOTImports Microsoft.SPOT.HardwareImports SecretLabs.NETMF.HardwareImports SecretLabs.NETMF.Hardware.NetduinoImports System.TextModule Module1 Dim rawData As String = Nothing Dim cdat As String = "done" Dim dataco As Byte() = Encoding.UTF8.GetBytes(cdat) Sub Main() Try Using socket As System.Net.Sockets.Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) socket.Bind(New IPEndPoint(IPAddress.Any, 8080)) socket.Listen(10) Using commSocket As Socket = socket.Accept() If commSocket.Poll(-1, SelectMode.SelectRead) Then Dim bytes As Byte() = New Byte(commSocket.Available - 1) {} Dim count As Integer = commSocket.Receive(bytes) rawData = (New String(Encoding.UTF8.GetChars(bytes))) End If End Using End Using Debug.Print(rawData) Catch ex As SocketException Debug.Print(ex.ToString) End Try Try Using socket2 As System.Net.Sockets.Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim ipSelect As String = "192.168.1.231" Dim portSelect As Integer = 8080 Dim remoteIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(ipSelect) Dim remoteEndPoint As New System.Net.IPEndPoint(remoteIPAddress, portSelect) socket2.Connect(remoteEndPoint) socket2.Send(dataco) socket2.Close() End Using Catch e As SocketException Debug.Print(e.Message) End Try Thread.Sleep(Timeout.Infinite) End SubEnd Module
Windows Form App
Imports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports System.LinqImports System.TextImports System.Windows.FormsImports System.Net.SocketsImports System.NetPublic Class Form1 Private Sub sendbtn_Click(sender As Object, e As EventArgs) Handles sendbtn.Click Dim cdat As String = Nothing cdat = value1.Text Dim Client As Socket Dim data As Byte() = Encoding.ASCII.GetBytes(cdat) Dim data2 As String = Nothing Try Client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim ipSelect As String = ipadd.Text Dim portSelect As Integer = Convert.ToInt16(port.Text) Dim remoteIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(ipSelect) Dim remoteEndPoint As New System.Net.IPEndPoint(remoteIPAddress, portSelect) Client.Connect(remoteEndPoint) Client.Send(data) Catch [error] As SocketException MessageBox.Show([error].Message) End Try Try Using Client2 = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Client2.Bind(New IPEndPoint(IPAddress.Any, 8080)) Client2.Listen(1) Using commSocket As Socket = Client2.Accept() If commSocket.Poll(-1, SelectMode.SelectRead) Then Dim bytes2 As Byte() = New Byte(commSocket.Available - 1) {} Dim count As Integer = commSocket.Receive(bytes2) data2 = (New String(Encoding.UTF8.GetChars(bytes2))) End If MessageBox.Show(data2) End Using End Using Catch ex As SocketException MessageBox.Show(ex.SocketErrorCode) End Try End SubEnd Class
Attached Files
Edited by twinnaz, 04 February 2014 - 03:06 AM.