Socket.Receive hangs

J

Joe Blauth

Hi all,

I am currently working on a small application that sends messages from a
client to a server and receives messages in return. Basically the
functionality is made with sockets which is working just fine except of one
little thing. whenever the client tries to receive a message the server has
sent it goes into a hang. unfortunately without an error or exception at
all.
the communication itself (sending message from client to server, server
listens on port and receives message, server sends message) is doing its job
so I cannot explain to myself why the socket.receive won't work for the
client (it does on the server side obviously).

but lets dig into some code:

CLIENT
connecting the server:
_Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
Dim oServerIP As System.Net.IPAddress =
System.Net.IPAddress.Parse(sServerIP)
Dim oRemoteEndPoint As New System.Net.IPEndPoint(oServerIP, iPort)
_Socket.Connect(oRemoteEndPoint)

sending message:
Dim oData As Object = sMessage
Dim bytData() As Byte = System.Text.Encoding.ASCII.GetBytes(oData.ToString)
_Socket.Send(bytData)

receiving message:
Dim bytBuffer(1024) As Byte
===> Dim iReceived As Integer = _Socket.Receive(bytBuffer)
Dim charChars(iReceived) As Char
Dim oDecoder As System.Text.Decoder = System.Text.Encoding.UTF8.GetDecoder
Dim iCharLength As Integer = oDecoder.GetChars(bytBuffer, 0, iReceived,
charChars, 0)
Dim sData As New String(charChars)

SERVER
on client connect:
_SocketWorker = _SocketListener.EndAccept(AsyncResult)
'_SocketListener is bound to local IP and listens to same port as client

sending message to client:
Dim oData As Object = sMessage
Dim bytData() As Byte = System.Text.Encoding.ASCII.GetBytes(oData.ToString)
_SocketWorker.Send(bytData)

Has anyone an idea why the Socket.Receive on the client side is not doing
the expected job ? it simply runs into a hang and the program itself is not
responding anymore.

thanks a lot for your hints!

Joe.
 
T

Tom Shelton

Hi all,

I am currently working on a small application that sends messages from a
client to a server and receives messages in return. Basically the
functionality is made with sockets which is working just fine except of one
little thing. whenever the client tries to receive a message the server has
sent it goes into a hang. unfortunately without an error or exception at
all.
the communication itself (sending message from client to server, server
listens on port and receives message, server sends message) is doing its job
so I cannot explain to myself why the socket.receive won't work for the
client (it does on the server side obviously).

but lets dig into some code:

CLIENT
connecting the server:
_Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
Dim oServerIP As System.Net.IPAddress =
System.Net.IPAddress.Parse(sServerIP)
Dim oRemoteEndPoint As New System.Net.IPEndPoint(oServerIP, iPort)
_Socket.Connect(oRemoteEndPoint)

sending message:
Dim oData As Object = sMessage
Dim bytData() As Byte = System.Text.Encoding.ASCII.GetBytes(oData.ToString)
_Socket.Send(bytData)

receiving message:
Dim bytBuffer(1024) As Byte
===> Dim iReceived As Integer = _Socket.Receive(bytBuffer)
Dim charChars(iReceived) As Char
Dim oDecoder As System.Text.Decoder = System.Text.Encoding.UTF8.GetDecoder
Dim iCharLength As Integer = oDecoder.GetChars(bytBuffer, 0, iReceived,
charChars, 0)
Dim sData As New String(charChars)

SERVER
on client connect:
_SocketWorker = _SocketListener.EndAccept(AsyncResult)
'_SocketListener is bound to local IP and listens to same port as client

sending message to client:
Dim oData As Object = sMessage
Dim bytData() As Byte = System.Text.Encoding.ASCII.GetBytes(oData.ToString)
_SocketWorker.Send(bytData)

Has anyone an idea why the Socket.Receive on the client side is not doing
the expected job ? it simply runs into a hang and the program itself is not
responding anymore.

thanks a lot for your hints!

Joe.

Receive blocks when there is no data available on the socket - so, you
need to ask your self why? Are your client and server on different boxes? Have you
checked to make sure that your app is not being blocked by the windows firewall?
The firewall will allow out going data, but block incomming. There is
another possiblity... A while ago, there was a bug in the framework,
that would cause a hang if the data was aligned on specific size
boundries - and to be honest, I'm not sure if it was fixed, it was a couple
of years ago that I ran into this issue. If it isn't a firewall issue, you might want
to tack a dummy character on the end of your message and see if that fixes the problem.

There are other issues with your code - such as how do you know you have
received the whole transmission? You can't always guarentee that you
will have one receive for every send - in other words, you may have to
do multiple reads to get one send. If your data is small, you may not
ever run into this - but, it is a good idea to think it through now :)
 
T

Tom Shelton

I believe the bug you are referring to is fixed, at least in dotNet 2.0 and
later.  I send/receive variable length packets over TCP all the time with no
problems.

Mike.- Hide quoted text -

- Show quoted text -

Probably :) It was a while ago. I can't even remember the size
boundries that caused the issue...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top