StealthRT
StealthRT

Reputation: 10552

Winsock in VB.net not working

Hey all i am trying to get this code that worked in VB6 just fine to work in VB.net 2008. It doesnt seem to want to connect (but has no error after it goes past the sockMain.Connect().

sockMain.RemoteHost = "192.168.1.77"
sockMain.RemotePort = 77
sockMain.Connect()

Now when i do this:

On Error GoTo oops
    sockMain.SendData(txtSend.Text)

oops: 
    If Err.Number = 40006 Then
        MsgBox("It doesnt seem that the server is running. Please check it and try again")
    End If

I get the It doesnt seem that the server is running. Please check it and try again. error.

What am i missing??

David

Upvotes: 1

Views: 6890

Answers (3)

Fredou
Fredou

Reputation: 20140

if you want the feeling of the vb6 winsock in .net world try this, beware it was not updated since 2008 and there is a few bug, look at the comment at the end of the acticle for more information

Upvotes: 1

Ciaran Keating
Ciaran Keating

Reputation: 2883

The Connect call can take a little while to complete. Even if your client has made the physical connection to the server, you have to give it a wee while to establish the TCP virtual circuit. If you put a breakpoint on the call to SendData and wait just a second or two, then continue, you'll probably find that it works OK.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 245001

As I explained in a comment, VB.NET and VB 6 are almost entirely different programming languages. You're not doing yourself any favors by trying to write VB 6 code in VB.NET. There's no reason to migrate at all if you're not going to take advantage of the new features provided by the .NET platform.

Beyond the structured exception handling that I mentioned already, you should ditch the old WinSock control in favor of the classes found in the System.Net.Sockets namespace.

Try replacing what you have with something like the following code:

Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("192.168.1.77", 77)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
    ' Do a simple write.
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
    networkStream.Write(sendBytes, 0, sendBytes.Length)

    ' Read the NetworkStream into a byte buffer.
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

    ' Output the data received from the host to the console.
    Dim returnData As String = Encoding.ASCII.GetString(bytes)
    Console.WriteLine(("Host returned: " + returnData))
Else
    If Not networkStream.CanRead Then
        Console.WriteLine("Cannot not write data to this stream. " &
                          "Please check the server and try again.")
        tcpClient.Close()
    Else
        If Not networkStream.CanWrite Then
            Console.WriteLine("Cannot read data from this stream. " &
                              "Please check the server and try again.")
            tcpClient.Close()
        End If
    End If
End If

Upvotes: 4

Related Questions