tags:

views:

181

answers:

1

Here is an issue with a socket I am having:

Public Sub Connect(ByVal server As String, ByVal port As Integer)
        Dim IP As IPAddress = IPAddress.Parse(server)
        Dim EP As IPEndPoint = New IPEndPoint(IP, port)
        sock.Bind(EP)
        sock.Connect(server, port)
    End Sub

The socket is declared like this, at the top of the code:

Dim sock As Socket

What is wrong here? It says Object reference not set to an instance of an object, which is a NullReferenceException.

Thanks for your help! This is in vb.net by the way, and I have imported the proper namespaces.

+2  A: 

Does the code ever assign a new instance of Socket to the Sock variable? sounds like the object is null because no assignment has been made to the variable.

EDIT
Initializing the socket could be done like this:

 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp)

Assuming that you are using IPV4 and that you want to read and write bytes from the socket like a stream. As for the protocol... I've specified UDP here but that will depend on what you are trying to talk to.

Hamish Smith
K, what is the proper syntax? I didn't know what AddressFamily or SocketType or ProtocolType to specify.
Cyclone