I'm trying to send bytes to a server, but I always get an exception at the send command saying: the socket should not be connected (line 26 in the code below).
This is weird right? cause I don't see how I can send without a connection...
I work in visual basic.net
This is weird right? cause I don't see how I can send without a connection...
I work in visual basic.net
Code:
Function sendCommand(ByVal command As String)
Dim dpr2() As Byte
Dim sender() As Byte = (prepCommand(command))
'//find a useable port
While (True)
Try
datagramSocket = New UdpClient(port)
Console.WriteLine("success, using port " + datagramSocket.Client.LocalEndPoint.ToString)
Exit While
Catch ex As Exception
Console.WriteLine("port " + port + " unusable, trying next port")
port = port + 1
End Try
End While
Try
'//Create new UDP connection and connect to specified ip address + port
datagramSocket.Connect(socketAddress.Address, socketAddress.Port)
datagramSocket.ExclusiveAddressUse = False
'//Initialize and send command as UDP packet to specified ip address and port
Dim dps(sender.Length) As Byte
datagramSocket.Send(sender, sender.Length, socketAddress)
Console.WriteLine("Sending command to " & socketAddress.Address.ToString & ":" & socketAddress.Port)
Catch Exc As Exception
'//Catch failed command send
Console.WriteLine("error: could not send UDP packet to server")
Console.WriteLine("details: " + Exc.Message)
datagramSocket.Close()
Return Nothing
End Try
Comment