473,748 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject sender, System.EventArg s e)
{

bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily. InterNetwork,So cketType.Dgram,
ProtocolType.Ud p);
int generalPort=myp ort;
IPAddress mcastIP = IPAddress.Parse (myip);

IPAddress localIP = IPAddress.Parse (myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(loca lIP,generalPort );
udpsock.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
mysock.Bind(loc alEnd);
mysock.SetSocke tOption(
SocketOptionLev el.IP,SocketOpt ionName.AddMemb ership,
new MulticastOption (mcastIP,localI P));

textBox1.Append Text("Begin datagram:"+"\r\ n");
int n = mysock.Receive( data,mydata.Len gth,SocketFlags .None);

String s =System.BitConv erter.ToString( data,0);
textBox1.Append Text(s);
textBox1.Append Text("\r\n");
textBox1.Append Text("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 5306
Why are you using SocketOptionNam e.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************ *********@europ eonline.com> wrote in message
news:Bq******** *************@n ews3.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(o bject sender, System.EventArg s e)
{

bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily. InterNetwork,So cketType.Dgram, ProtocolType.Ud p);
int generalPort=myp ort;
IPAddress mcastIP = IPAddress.Parse (myip);

IPAddress localIP = IPAddress.Parse (myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(loca lIP,generalPort );
udpsock.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
mysock.Bind(loc alEnd);
mysock.SetSocke tOption(
SocketOptionLev el.IP,SocketOpt ionName.AddMemb ership,
new MulticastOption (mcastIP,localI P));

textBox1.Append Text("Begin datagram:"+"\r\ n");
int n = mysock.Receive( data,mydata.Len gth,SocketFlags .None);

String s =System.BitConv erter.ToString( data,0);
textBox1.Append Text(s);
textBox1.Append Text("\r\n");
textBox1.Append Text("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******** ******@TK2MSFTN GP12.phx.gbl...
Why are you using SocketOptionNam e.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************ *********@europ eonline.com> wrote in message
news:Bq******** *************@n ews3.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(o bject sender, System.EventArg s e)
{

bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily. InterNetwork,So cketType.Dgram, ProtocolType.Ud p);
int generalPort=myp ort;
IPAddress mcastIP = IPAddress.Parse (myip);

IPAddress localIP = IPAddress.Parse (myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(loca lIP,generalPort );
udpsock.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
mysock.Bind(loc alEnd);
mysock.SetSocke tOption(
SocketOptionLev el.IP,SocketOpt ionName.AddMemb ership,
new MulticastOption (mcastIP,localI P));

textBox1.Append Text("Begin datagram:"+"\r\ n");
int n = mysock.Receive( data,mydata.Len gth,SocketFlags .None);

String s =System.BitConv erter.ToString( data,0);
textBox1.Append Text(s);
textBox1.Append Text("\r\n");
textBox1.Append Text("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************ *********@europ eonline.com> wrote in message
news:5S******** *************@n ews3.tin.it...

"William Stacey [MVP]" <st***********@ mvps.org> ha scritto nel messaggio
news:eB******** ******@TK2MSFTN GP12.phx.gbl...
Why are you using SocketOptionNam e.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******** ******@TK2MSFTN GP12.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************ *********@europ eonline.com> wrote in message
news:5S******** *************@n ews3.tin.it...

"William Stacey [MVP]" <st***********@ mvps.org> ha scritto nel messaggio
news:eB******** ******@TK2MSFTN GP12.phx.gbl...
Why are you using SocketOptionNam e.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.mach in AT dot.state.fl.us > ha
scritto nel messaggio news:eM******** ******@tk2msftn gp13.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************ *********@europ eonline.com> wrote in message
news:lm******** *************@n ews3.tin.it...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > ha
scritto nel messaggio news:eM******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP09.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

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

Similar topics

11
8525
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 the reason is as i am quite new to it. here is teh server side program: ///////////////////////
0
1552
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: --- import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3
2346
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 doesn't reach the read-callback (that is assigned right after successful "Connect(server,port)") on...
4
9449
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 pass the socket to a class which just reads from the socket (in a thread). class Reader(Thread): def run(self): while 1:
5
12802
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
0
1401
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 for the socket.send(Data) method to complete. I've tried all of these socket options to overcome this event: 'Set send timeout to 2 seconds:
0
1123
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, # ------------------------------------ use IO::Socket; $host = "www.cs.cf.ac.uk" ; ## Use any site here
2
3434
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 failing to asynchronously connect sockets on closed or filtered ports, but I'm quite unsure if this is a PHP issue or my misunderstanding, as it seems that socket streams only wrap around <sys/socket.h>.
9
18555
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, for testing. When looking at netstat, the socket is in the SYN_SENT state, like this: $netstat -a -tcp tcp 0 0 *:www *:* LISTEN 7635/apache2 tcp 0 1 bukzor:38234 adsl-75-61-84-249.d:www
1
9319
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.