VB Express Socket help

I've never used VB.net but I was confident that it wouldn't be a problem.  However, I've had some trouble getting a simple socket program to work.  I have read numerous documentation on the subject but can't figure out what I'm doing wrong(the documentation seems to be targeted to the pro-programmer instead of a beginner).

Dim mySocket As System.Net.Sockets.Socket
Dim myEndpoint As System.Net.IPEndPoint
Dim myHost As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("localhost")
Dim myIP As System.Net.IPAddress = myHost.AddressList(0)
myEndpoint.Address = myIP
myEndpoint.Port = "2001"
mySocket.Bind(myEndpoint)
mySocket.Listen(25)

I get errors on the following two lines, saying that I am using the variable before I have assigned it a value.  myIP.toString returns "127.0.0.1", which is correct, but I can't figure out how toe send my endpoint.address correctly.


Answer this question

VB Express Socket help

  • JD Li

    There are a couple of ways that you could accomplish this using a TcpListener; you could add a timer to your project and poll (periodically check) the socket if there are any incoming connections/data available or you could fire up a background worker thread to do your work on.

    There are also a couple of examples on how to use "normal" asynchronous socket that may interest you:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconusingnon-blockingserversocket.asp

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconnon-blockingserversocketexample.asp

    Best regards,
    Johan Stenberg



  • Andrew Piper

    The reason I was using sockets is that this is a gui program that is listening to a port and displaying messages that is being sent to said port.  Using tcplistener and it's methods causes my gui to block.  I was under the impression that I could do this using sockets without the entire program blocking.
    Is their some way I can achieve the results I desire using tcplistener

  • Daniel Corbett

    The problem is that you never New up (create) an instance of the mySocket or myEndpoint variables, so they are both set to Nothing when you try to assign values to their members.

    While it certainly is possible to use the Socket class, I'd recommend that you try the TcpClient and TcpListener classes. The TcpClient and TcpListener represent the client and server side of a TCP connection respectively and expose more convenient methods for you to use.

    I have changed the code to use a TcpListener instead and ended up with something like this:



    Dim myHost As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("localhost")
    Dim myIP As System.Net.IPAddress = myHost.AddressList(0)
    Dim mySocket As New System.Net.Sockets.TcpListener(myIP, 2001)
    mySocket.Start(25)

     


    You can find some samples on networking here: http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpqstart/html/cpsmpnetsamples-howtonetworking.asp

    Best regards,
    Johan Stenberg



  • john cov

    hi, you can use tcpclient and tcplistener classes its easier to use

    but in your code you didn't instantiate the socket and didn't set which type of sockets is this tcp , udp , raw socket , the following is tcp socket if you want it udp replace stream with dgram , and replace tcp with udp

    mysocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);

    you can make your socket nonblocking, using theads , using Asynch as what Johan said

    more over there is a special forum for networking and communication  http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=40&SiteID=1

    hope this helps



  • VB Express Socket help