Perhaps since you're using TCP, the Nagle algorithm is buffering the stream for efficiency, and your message is too short so is stuck in the buffer waiting for more byte stream before sending.
After your creation of tempSocket, you can try adding
tempSocket.NoDelay = True
to disable Nagle, and the data should be transmitted without consolidating multiple send calls into one transmission.
Haven't tested it. I've only used TcpClient class (System.Net.Sockets.TcpClient), and haven't experience any issues with data not being sent with that. I'm just guessing since you say you don't see the data going out, that Nagle may be a possible reason.
I already had a program that would wait for a TCP connetion, and read bytes from the stream, initially 4 bytes to get the size of the data coming and then it would read the data. The waiting for connection, then reading data ran in a background thread for simplicity.
I just tried a quick test of sending four bytes over a TCP connection to that application, from a console application using your code as an example, and I received the four bytes. I didn't need to set NoDelay, so I couldn't verify there was a problem, and that setting NoDelay would get around it.
Code:
Imports System.Net.Sockets
Imports System.Net
Module Module1
Sub Main()
Dim status As String = testSocket()
Console.WriteLine(status)
Console.ReadKey()
End Sub
Private Function testSocket() As String
Dim s As Socket = ConnectSocket("127.0.0.1", 3030)
Dim bytesSent() As Byte = {49, 50, 51, 52}
If s Is Nothing Then
Return "Connection failed"
End If
' Send request to the server.
Dim numSent As Integer
numSent = s.Send(bytesSent)
Return ("Bytes Sent")
End Function
Private Function ConnectSocket(server As String, port As Integer) As Socket
Dim s As Socket = Nothing
Dim hostEntry As IPHostEntry = Nothing
' Get host related information.
' hostEntry = Dns.GetHostEntry(server)
' Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
' an exception that occurs when the host host IP Address is not compatible with the address family
' (typical in the IPv6 case).
' Dim address As IPAddress
' For Each address In hostEntry.AddressList
Dim endPoint As New IPEndPoint(IPAddress.Parse(server), port)
Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
tempSocket.Connect(endPoint)
If tempSocket.Connected Then
s = tempSocket
' Exit For
End If
' Next address
Return s
End Function
End Module
Perhaps because I was using the local loopback IP address, Nagle doesn't get used because the data wouldn't actually go out the NIC, so Network efficiency doesn't really apply. The test could be invalid. But, I don't feel like setting up another computer on the network to do more testing, as I should be heading to bed anyway.