473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket.Receive hangs

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(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p)
Dim oServerIP As System.Net.IPAd dress =
System.Net.IPAd dress.Parse(sSe rverIP)
Dim oRemoteEndPoint As New System.Net.IPEn dPoint(oServerI P, iPort)
_Socket.Connect (oRemoteEndPoin t)

sending message:
Dim oData As Object = sMessage
Dim bytData() As Byte = System.Text.Enc oding.ASCII.Get Bytes(oData.ToS tring)
_Socket.Send(by tData)

receiving message:
Dim bytBuffer(1024) As Byte
===Dim iReceived As Integer = _Socket.Receive (bytBuffer)
Dim charChars(iRece ived) As Char
Dim oDecoder As System.Text.Dec oder = System.Text.Enc oding.UTF8.GetD ecoder
Dim iCharLength As Integer = oDecoder.GetCha rs(bytBuffer, 0, iReceived,
charChars, 0)
Dim sData As New String(charChar s)

SERVER
on client connect:
_SocketWorker = _SocketListener .EndAccept(Asyn cResult)
'_SocketListene r 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.Enc oding.ASCII.Get Bytes(oData.ToS tring)
_SocketWorker.S end(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.
Mar 21 '08 #1
3 3606
On 2008-03-21, Joe Blauth <ca************ *******@web.dew rote:
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(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p)
Dim oServerIP As System.Net.IPAd dress =
System.Net.IPAd dress.Parse(sSe rverIP)
Dim oRemoteEndPoint As New System.Net.IPEn dPoint(oServerI P, iPort)
_Socket.Connect (oRemoteEndPoin t)

sending message:
Dim oData As Object = sMessage
Dim bytData() As Byte = System.Text.Enc oding.ASCII.Get Bytes(oData.ToS tring)
_Socket.Send(by tData)

receiving message:
Dim bytBuffer(1024) As Byte
===Dim iReceived As Integer = _Socket.Receive (bytBuffer)
Dim charChars(iRece ived) As Char
Dim oDecoder As System.Text.Dec oder = System.Text.Enc oding.UTF8.GetD ecoder
Dim iCharLength As Integer = oDecoder.GetCha rs(bytBuffer, 0, iReceived,
charChars, 0)
Dim sData As New String(charChar s)

SERVER
on client connect:
_SocketWorker = _SocketListener .EndAccept(Asyn cResult)
'_SocketListene r 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.Enc oding.ASCII.Get Bytes(oData.ToS tring)
_SocketWorker.S end(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
Mar 21 '08 #2
"Tom Shelton" <to*********@YO UKNOWTHEDRILLco mcast.netwrote in message
news:OA******** ******@TK2MSFTN GP04.phx.gbl...
On 2008-03-21, Joe Blauth <ca************ *******@web.dew rote:
>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
functionalit y 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(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.T cp)
Dim oServerIP As System.Net.IPAd dress =
System.Net.IPA ddress.Parse(sS erverIP)
Dim oRemoteEndPoint As New System.Net.IPEn dPoint(oServerI P, iPort)
_Socket.Connec t(oRemoteEndPoi nt)

sending message:
Dim oData As Object = sMessage
Dim bytData() As Byte =
System.Text.En coding.ASCII.Ge tBytes(oData.To String)
_Socket.Send(b ytData)

receiving message:
Dim bytBuffer(1024) As Byte
===Dim iReceived As Integer = _Socket.Receive (bytBuffer)
Dim charChars(iRece ived) As Char
Dim oDecoder As System.Text.Dec oder =
System.Text.En coding.UTF8.Get Decoder
Dim iCharLength As Integer = oDecoder.GetCha rs(bytBuffer, 0, iReceived,
charChars, 0)
Dim sData As New String(charChar s)

SERVER
on client connect:
_SocketWorke r = _SocketListener .EndAccept(Asyn cResult)
'_SocketListen er 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.En coding.ASCII.Ge tBytes(oData.To String)
_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.
Mar 22 '08 #3
On Mar 22, 9:16*am, "Michael D. Ober" <obermd.@.alum. mit.edu.nospam. >
wrote:
"Tom Shelton" <tom_shel...@YO UKNOWTHEDRILLco mcast.netwrote in message

news:OA******** ******@TK2MSFTN GP04.phx.gbl...


On 2008-03-21, Joe Blauth <catabouche.rem ovet...@web.dew rote:
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(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p)
Dim oServerIP As System.Net.IPAd dress =
System.Net.IPAd dress.Parse(sSe rverIP)
Dim oRemoteEndPoint As New System.Net.IPEn dPoint(oServerI P, iPort)
_Socket.Connect (oRemoteEndPoin t)
sending message:
Dim oData As Object = sMessage
Dim bytData() As Byte =
System.Text.Enc oding.ASCII.Get Bytes(oData.ToS tring)
_Socket.Send(by tData)
receiving message:
Dim bytBuffer(1024) As Byte
===Dim iReceived As Integer = _Socket.Receive (bytBuffer)
Dim charChars(iRece ived) As Char
Dim oDecoder As System.Text.Dec oder =
System.Text.Enc oding.UTF8.GetD ecoder
Dim iCharLength As Integer = oDecoder.GetCha rs(bytBuffer, 0, iReceived,
charChars, 0)
Dim sData As New String(charChar s)
SERVER
on client connect:
_SocketWorker = _SocketListener .EndAccept(Asyn cResult)
'_SocketListene r 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.Enc oding.ASCII.Get Bytes(oData.ToS tring)
_SocketWorker.S end(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.- 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
Mar 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8494
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for my server program written in python it simply hangs.it does not show any error also.I've tried sample programs available .I don understand what...
3
2330
by: Alex | last post by:
Hi, I am programming asynchronous communication between client and server, with .net asynchronous sockets example from MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingnon-blockingserversocket.asp) The problem is that the application hangs on the client side, when the server sends a buffer. It...
6
4096
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: Connect Receive response Send Connect ACK
10
5293
by: feel52 | last post by:
Below you'll find the code i'm working on. It's in a button click routine and hangs after 3 or 4 sometimes 5 loops done, probably in sock.receive(....). Some code was found here( on google i mean) but the amazing( at least for me) is that if i run the program step by step or i cancel the loop and run it by pressing the button again and...
1
3821
by: Saso Zagoranski | last post by:
Hi! I have simple client/server game that uses sockets to transfer different messages. The server and the client are running on the same machine.
2
4142
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send I get the last chunk of data and am not able to retrieve it anymore cause the socket will be dead. Loop: { socket.Receive <----------- data...
0
1001
by: Faisal | last post by:
Hi, People I have written an ftpclient in .net now a strange thing is happening while debugging every thing is working all right but when i run the program normally the system hangs on socket.receive() can any body give me any clues why this would be happening This is my code snippet Socket clientSocket = new Socket(...
2
15308
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with...
10
7379
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out of 0.1 set. If however, you try to receive more than one char, (I tested with 3, did not try 2), then when you shut the remote end down you do not...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.