Board index » Visual Studio » Socket.Receive hangs

Socket.Receive hangs

Visual Studio40
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.


-
 

Re:Socket.Receive hangs

On 2008-03-21, Joe Blauth <catabouche.removethis@web.de>wrote:

Quote
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 :)



--

Tom Shelton

-

Re:Socket.Receive hangs

"Tom Shelton" <tom_shelton@YOUKNOWTHEDRILLcomcast.net>wrote in message

Quote
On 2008-03-21, Joe Blauth <catabouche.removethis@web.de>wrote:

>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 :)



--

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.





-

Re:Socket.Receive hangs

On Mar 22, 9:16=A0am, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam.>

wrote:

Quote
"Tom Shelton" <tom_shel...@YOUKNOWTHEDRILLcomcast.net>wrote in message



news:OAkI3B4iIHA.1188@TK2MSFTNGP04.phx.gbl...











>On 2008-03-21, Joe Blauth <catabouche.removet...@web.de>wrote:

>>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 a=

t

>>all.

>>the communication itself (sending message from client to server, server=



>>listens on port and receives message, server sends message) is doing it=

s

>>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 =3D New Socket(AddressFamily.InterNetwork, SocketType.Stream,

>>ProtocolType.Tcp)

>>Dim oServerIP As System.Net.IPAddress =3D

>>System.Net.IPAddress.Parse(sServerIP)

>>Dim oRemoteEndPoint As New System.Net.IPEndPoint(oServerIP, iPort)

>>_Socket.Connect(oRemoteEndPoint)



>>sending message:

>>Dim oData As Object =3D sMessage

>>Dim bytData() As Byte =3D

>>System.Text.Encoding.ASCII.GetBytes(oData.ToString)

>>_Socket.Send(bytData)



>>receiving message:

>>Dim bytBuffer(1024) As Byte

>>=3D=3D=3D>Dim iReceived As Integer =3D _Socket.Receive(bytBuffer)

>>Dim charChars(iReceived) As Char

>>Dim oDecoder As System.Text.Decoder =3D

>>System.Text.Encoding.UTF8.GetDecoder

>>Dim iCharLength As Integer =3D oDecoder.GetChars(bytBuffer, 0, iReceive=

d,

>>charChars, 0)

>>Dim sData As New String(charChars)



>>SERVER

>>on client connect:

>>_SocketWorker =3D _SocketListener.EndAccept(AsyncResult)

>>'_SocketListener is bound to local IP and listens to same port as clien=

t



>>sending message to client:

>>Dim oData As Object =3D sMessage

>>Dim bytData() As Byte =3D

>>System.Text.Encoding.ASCII.GetBytes(oData.ToString)

>>_SocketWorker.Send(bytData)



>>Has anyone an idea why the Socket.Receive on the client side is not doi=

ng

>>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? =A0Are your client and server on different bo=

xes?

>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. =A0There is=



>another possiblity... =A0A 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. =A0If it isn't a firewall issue=

,

>you might want

>to tack a dummy character on the end of your message and see if that fix=

es

>the problem.



>There are other issues with your code - such as how do you know you have=



>received the whole transmission? =A0You 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. =A0If your data is small, you may not=



>ever run into this - but, it is a good idea to think it through now :)



>--

>Tom Shelton



I believe the bug you are referring to is fixed, at least in dotNet 2.0 an=

d

later. =A0I send/receive variable length packets over TCP all the time wit=

h 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...



--

Tom Shelton

-