473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(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.
Mar 21 '08 #1
3 3594
On 2008-03-21, Joe Blauth <ca*******************@web.dewrote:
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
Mar 21 '08 #2
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
On 2008-03-21, Joe Blauth <ca*******************@web.dewrote:
>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.ToStrin g)
_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.ToStrin g)
_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...@YOUKNOWTHEDRILLcomcast.netwrote in message

news:OA**************@TK2MSFTNGP04.phx.gbl...


On 2008-03-21, Joe Blauth <catabouche.removet...@web.dewrote:
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.- 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
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...
3
by: Alex | last post by:
Hi, I am programming asynchronous communication between client and server, with .net asynchronous sockets example from MSDN...
6
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: ...
10
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)...
1
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
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...
0
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...
2
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...
10
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.