Netduino home hardware projects downloads community

Jump to content


Photo

Socket


  • Please log in to reply
4 replies to this topic

#1 Stari

Stari

    Member

  • Members
  • PipPip
  • 26 posts

Posted 14 February 2014 - 04:59 PM

Hi,

i have a small automation program, that comunicate with WiFly. Ok, if WiFly is connected, if not, my program hangs.Connection i make with timer. as i say, all things work perfectly, if WiFly is ON. If not, program "hangs" without any message.

Thank you

 

 

 

Attached Files



#2 jrlyman3

jrlyman3

    Advanced Member

  • Members
  • PipPipPip
  • 67 posts
  • LocationNorth St Paul. MN

Posted 16 February 2014 - 03:54 PM

It seems like this should be easier ...

 

In C# I would set Socket.Blocking = false (which causes the Connect to throw an exception that I ignore), then call Socket.Poll to wait for the connect to finish, and then check if the socket is connected.

 

The following link shows a VB example that's not quite what you're looking for, but might get you headed in the right direction :).

 

  http://msdn.microsof...#code-snippet-2

 

Hope that this helps - John



#3 Stari

Stari

    Member

  • Members
  • PipPip
  • 26 posts

Posted 16 February 2014 - 06:23 PM

jrlyman3, thank you. Yes, this is the right direction. I'm not programmer, so i will need more time to finish.



#4 JoopC

JoopC

    Advanced Member

  • Members
  • PipPipPip
  • 148 posts

Posted 17 February 2014 - 09:27 AM

You program it wrong, you'll have to do it like this as example:

 

 

 

Const cnstHostThingSpeak As String = "thingspeak.com"

 

Dim IPEndPoint As New IPEndPoint(Dns.GetHostEntry(cnstHostThingSpeak).AddressList(0), 80)

Dim Host As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
Host.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, True)
 
sending.......
Host.SendTo( Encoding.UTF8.GetBytes("blabla"), IPEndPoint)
 
 
reading:......................
Host.Poll(500000, SelectMode.SelectRead)
Dim bBytes As Byte() = New Byte(Host.Available - 1) {}
If Host.Receive(bBytes) > 0 Then
   Debug.Print(New String(Encoding.UTF8.GetChars(bBytes)))
End If
 
 
If you have a lot to read, you'll have to loop like this:
 
 
Dim strRequest as string = ""
Do While Host.Poll(50000, SelectMode.SelectRead)
    Dim bBytes As Byte() = New Byte(Host.Available - 1) {}
 
    If Host.Receive(bBytes) > 0 Then
        strRequest &= New String(Encoding.UTF8.GetChars(bBytes))
    Else
         Exit Do
    End If
 
   Thread.Sleep(200)
Loop
 
Debug.Print(strRequest)


#5 Stari

Stari

    Member

  • Members
  • PipPip
  • 26 posts

Posted 17 February 2014 - 05:53 PM

JoopC, thank you. Very usefull. I have make my program like this, found on Internet : http://netmftoolbox....ailable classes

OK, now things ar working. 

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.