473,395 Members | 1,915 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,395 software developers and data experts.

Using socket class program hangs?


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 again it never hangs .
I want to have an infinite loop so my socket keeps on listening until i
press another button.
private void button1_Click(object sender, System.EventArgs e)
{

bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp);
int generalPort=myport;
IPAddress mcastIP = IPAddress.Parse(myip);

IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(localIP,generalPort);
udpsock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembershi p,
new MulticastOption(mcastIP,localIP));

textBox1.AppendText("Begin datagram:"+"\r\n");
int n = mysock.Receive(data,mydata.Length,SocketFlags.None );

String s =System.BitConverter.ToString(data,0);
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
textBox1.AppendText("End of Datagram"+"\r\n");
mysock.Close();

}

It seems tome that the socket buffer is full so hangs , but. the socket is
closed every time the loop ends..well ,any help will be appreciated.
THANKS.
Nov 16 '05 #1
10 5267
Why are you using SocketOptionName.ReuseAddress? This smells like it could
be the issue.
Also, why are you creating the socket over and over again in the loop? If
you want a listener, create the socket once and loop on receive.

--
William Stacey, MVP

"feel52" <fe*********************@europeonline.com> wrote in message
news:Bq*********************@news3.tin.it...

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 again it never hangs .
I want to have an infinite loop so my socket keeps on listening until i
press another button.
private void button1_Click(object sender, System.EventArgs e)
{

bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
int generalPort=myport;
IPAddress mcastIP = IPAddress.Parse(myip);

IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(localIP,generalPort);
udpsock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembershi p,
new MulticastOption(mcastIP,localIP));

textBox1.AppendText("Begin datagram:"+"\r\n");
int n = mysock.Receive(data,mydata.Length,SocketFlags.None );

String s =System.BitConverter.ToString(data,0);
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
textBox1.AppendText("End of Datagram"+"\r\n");
mysock.Close();

}

It seems tome that the socket buffer is full so hangs , but. the socket is closed every time the loop ends..well ,any help will be appreciated.
THANKS.


Nov 16 '05 #2

"William Stacey [MVP]" <st***********@mvps.org> ha scritto nel messaggio
news:eB**************@TK2MSFTNGP12.phx.gbl...
Why are you using SocketOptionName.ReuseAddress? This smells like it could be the issue.
Also, why are you creating the socket over and over again in the loop? If
you want a listener, create the socket once and loop on receive.

--
William Stacey, MVP

Thanks,
i'm using Reuse Address because another program receiving from same port,
without it mine raises exception.
Now i will try your second hint.Bye.
Nov 16 '05 #3
Hi,

I would change the code a little, in addition to Stacey's opinion I
strongly suggest you two things:

1- Do ALL sockets operations in a worker thread, do not use the UI thread.
2- Do you really need to ReUseAddress and multicast?

What is the intention of the program?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"feel52" <fe*********************@europeonline.com> wrote in message
news:Bq*********************@news3.tin.it...

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 again it never hangs .
I want to have an infinite loop so my socket keeps on listening until i
press another button.
private void button1_Click(object sender, System.EventArgs e)
{

bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
int generalPort=myport;
IPAddress mcastIP = IPAddress.Parse(myip);

IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(localIP,generalPort);
udpsock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembershi p,
new MulticastOption(mcastIP,localIP));

textBox1.AppendText("Begin datagram:"+"\r\n");
int n = mysock.Receive(data,mydata.Length,SocketFlags.None );

String s =System.BitConverter.ToString(data,0);
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
textBox1.AppendText("End of Datagram"+"\r\n");
mysock.Close();

}

It seems tome that the socket buffer is full so hangs , but. the socket is closed every time the loop ends..well ,any help will be appreciated.
THANKS.

Nov 16 '05 #4
Are you sure you want that behavior. Sometime you will get the data, other
times the other app will get the bytes. Is this what you want?

--
William Stacey, MVP

"feel52" <fe*********************@europeonline.com> wrote in message
news:5S*********************@news3.tin.it...

"William Stacey [MVP]" <st***********@mvps.org> ha scritto nel messaggio
news:eB**************@TK2MSFTNGP12.phx.gbl...
Why are you using SocketOptionName.ReuseAddress? This smells like it

could
be the issue.
Also, why are you creating the socket over and over again in the loop? If you want a listener, create the socket once and loop on receive.

--
William Stacey, MVP

Thanks,
i'm using Reuse Address because another program receiving from same port,
without it mine raises exception.
Now i will try your second hint.Bye.


Nov 16 '05 #5
BTW, if you intention is to listen on one thread and send on another, this
reuse will not work as expected. Don't create two sockets, share the same
socket and send on one only and receive on the other only. HTH

--
William Stacey, MVP

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Are you sure you want that behavior. Sometime you will get the data, other times the other app will get the bytes. Is this what you want?

--
William Stacey, MVP

"feel52" <fe*********************@europeonline.com> wrote in message
news:5S*********************@news3.tin.it...

"William Stacey [MVP]" <st***********@mvps.org> ha scritto nel messaggio
news:eB**************@TK2MSFTNGP12.phx.gbl...
Why are you using SocketOptionName.ReuseAddress? This smells like it

could
be the issue.
Also, why are you creating the socket over and over again in the loop? If you want a listener, create the socket once and loop on receive.

--
William Stacey, MVP

Thanks,
i'm using Reuse Address because another program receiving from same port, without it mine raises exception.
Now i will try your second hint.Bye.


Nov 16 '05 #6

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> ha
scritto nel messaggio news:eM**************@tk2msftngp13.phx.gbl...
Hi,

I would change the code a little, in addition to Stacey's opinion I
strongly suggest you two things:

1- Do ALL sockets operations in a worker thread, do not use the UI thread.
2- Do you really need to ReUseAddress and multicast?

What is the intention of the program?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Well it's hanging as before moving the loop to socket receive.
All I want is to read datagrams ( no sent at all ) incoming and sent to
me in multicast, parse them as the other application does another work
with them,so both must be running at same time. I wrote an application in
delphi7 doing well that , now i'm porting (better trying to..) to C# so i
know that it's a thing that can work without problem.

As i said before it's strange (to me) that if i run step by step everything
works well, i see data arriving on screen in correct way ( i compare them
with the other application), obviously i miss some datagrams as they arrive
more or less 4 or 5 per second and if i run using the loop hangs after
receving fews datagrams.
Thanks again.
Nov 16 '05 #7
I noticed the same thing once. Just has to do with timing when you
stepping. If you do it enouph, you should get the same behavior after many
tries while stepping. I still don't think you will get passed the ReUse
issue. If you have two sockets at same port/IP, you can't control which
socket will receive the data (AFAICT, both will not receive the same bytes.
One or the other will) It is hanging because the other socket gets the data
and probably drops the datagram and your thread is still blocking on a
receive as it never "sees" the bytes. Not sure how this may have worked in
Delphi as both use winsock (unless delphi has another socket
implementation.) You should see the same behavior using winsock api
natively. I have not worked with multicast, so maybe that is answer that
allows both sockets to receive the datagram, but don't think so. Maybe the
Delphi way was two sockets on same port but different IPs using multicast?
I would also try the ms winsock ng. Please let us know if you find the fix.
Cheers!

--
William Stacey, MVP

"feel52" <fe*********************@europeonline.com> wrote in message
news:lm*********************@news3.tin.it...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> ha
scritto nel messaggio news:eM**************@tk2msftngp13.phx.gbl...
Hi,

I would change the code a little, in addition to Stacey's opinion I
strongly suggest you two things:

1- Do ALL sockets operations in a worker thread, do not use the UI thread. 2- Do you really need to ReUseAddress and multicast?

What is the intention of the program?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Well it's hanging as before moving the loop to socket receive.
All I want is to read datagrams ( no sent at all ) incoming and sent

to me in multicast, parse them as the other application does another work
with them,so both must be running at same time. I wrote an application in
delphi7 doing well that , now i'm porting (better trying to..) to C# so i
know that it's a thing that can work without problem.

As i said before it's strange (to me) that if i run step by step everything works well, i see data arriving on screen in correct way ( i compare them
with the other application), obviously i miss some datagrams as they arrive more or less 4 or 5 per second and if i run using the loop hangs after
receving fews datagrams.
Thanks again.


Nov 16 '05 #8
Also, IMO, a cleaner approach would be to have one socket listening. Get
the datagram and put a copy ref into two queues. Have two other threads.
ThreadA grabs the datagram from the queue and does its thing. ThreadB grabs
the same ref (or a copy of the bytes) and does its thing. You can have
output queue that ThreadC blocks on. When it gets some output, it sends the
reply on ref to same socket.

--
William Stacey, MVP

Nov 16 '05 #9

"William Stacey [MVP]" <st***********@mvps.org> ha scritto nel messaggio
news:OD**************@TK2MSFTNGP09.phx.gbl...
I noticed the same thing once. Just has to do with timing when you
stepping. If you do it enouph, you should get the same behavior after many tries while stepping. I still don't think you will get passed the ReUse
issue. If you have two sockets at same port/IP, you can't control which
socket will receive the data (AFAICT, both will not receive the same bytes. One or the other will) It is hanging because the other socket gets the data and probably drops the datagram and your thread is still blocking on a
receive as it never "sees" the bytes. Not sure how this may have worked in Delphi as both use winsock (unless delphi has another socket
implementation.)
In effect i used some third part components...

You should see the same behavior using winsock api natively. I have not worked with multicast, so maybe that is answer that
allows both sockets to receive the datagram, but don't think so. Maybe the Delphi way was two sockets on same port but different IPs using multicast?
I would also try the ms winsock ng. Please let us know if you find the fix. Cheers!

--
William Stacey, MVP


One more thing,
i noticed now ( it' s two days 'only' i'm facing this problem) that if i
Pause the program, restart it by pressing Step Over ( i'm using Borland
Builder) the counter i put in my code ( i did not write it in my first post)
and that i can see in the Local Variables window kept growing so in effect
the program doesnt hang but , how can i say.... the form hangs and does not
respond to any click and the textbox isn't refreshed by incoming data
(appendtext doesnt work)but in background the socket is still receiving.May
be this can change the point of view of the problem.
Sorry , english is not my mothertongue as you understood , i hope you
understand.
Thanks anyway for your efforts.
Nov 16 '05 #10
You don't want blocking operations in your form. So you want to create
another thread that does the listening and processing. The button will just
start the thread and return so your UI remains active.
I would do something like:
1) create a Server class.
2) That class will contain a thread object. The entry point will be a
private method of your Server class.
3) Create a public Start() method on your server class. Call this to create
the thread and start it.
4) You can create your server object first and pass it udpclient ref or
create the updclient inside the server class. I would encapsulate as much
as possible.
5) Also create Stop() method, etc.

Now in your start button method, just create an instance of Server and start
it. Save this reference in a private form var so you have a ref to it after
the click method returns. This should get you started, you can add a lot
more function to server after you get the general steps working.

--
William Stacey, MVP

"feel52" <fe*********************@europeonline.com> wrote in message
news:TR*********************@news3.tin.it...

"William Stacey [MVP]" <st***********@mvps.org> ha scritto nel messaggio
news:OD**************@TK2MSFTNGP09.phx.gbl...
I noticed the same thing once. Just has to do with timing when you
stepping. If you do it enouph, you should get the same behavior after many
tries while stepping. I still don't think you will get passed the ReUse
issue. If you have two sockets at same port/IP, you can't control which
socket will receive the data (AFAICT, both will not receive the same

bytes.
One or the other will) It is hanging because the other socket gets the

data
and probably drops the datagram and your thread is still blocking on a
receive as it never "sees" the bytes. Not sure how this may have worked

in
Delphi as both use winsock (unless delphi has another socket
implementation.)


In effect i used some third part components...

You should see the same behavior using winsock api
natively. I have not worked with multicast, so maybe that is answer that allows both sockets to receive the datagram, but don't think so. Maybe

the
Delphi way was two sockets on same port but different IPs using multicast? I would also try the ms winsock ng. Please let us know if you find the

fix.
Cheers!

--
William Stacey, MVP


One more thing,
i noticed now ( it' s two days 'only' i'm facing this problem) that if i
Pause the program, restart it by pressing Step Over ( i'm using Borland
Builder) the counter i put in my code ( i did not write it in my first

post) and that i can see in the Local Variables window kept growing so in effect the program doesnt hang but , how can i say.... the form hangs and does not respond to any click and the textbox isn't refreshed by incoming data
(appendtext doesnt work)but in background the socket is still receiving.May be this can change the point of view of the problem.
Sorry , english is not my mothertongue as you understood , i hope you
understand.
Thanks anyway for your efforts.


Nov 16 '05 #11

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...
0
by: Michael Petroni | last post by:
hello *, i have a problem with python 2.2.3 under aix 4.3.3 compiled with gcc version 2.9-aix51-020209 (rpm package from ibm). the following code works fine under all other systems: ---...
3
by: Alex | last post by:
Hi, I am programming asynchronous communication between client and server, with .net asynchronous sockets example from MSDN...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
0
by: Blog the Haggis | last post by:
Hi all, I've written a program which distributes binary data to a number of clients via TCP. The program runs perfectly unless one of the client programs hangs and then it freezes while waiting...
0
by: cheguvera | last post by:
Hi All, I want to have a Perl script which will act as HTTP client. Basically, it should be able to get (read) a given HTML page from server. I have this code with me, #...
2
by: Nicolas Le Gland | last post by:
Hello everyone here. This is my first post in this newsgroup, I hope I won't be to much off-topic. Feel free to redirect me to any better group. I am getting strange timing issues when...
9
by: bukzor | last post by:
I'm having an issue where my program hangs while doing socket.connect() for a couple minutes, then times out the connection and crashes. I'm connecting to an apache2 process on the same machine,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.