473,324 Members | 2,196 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,324 software developers and data experts.

socket operation could not be performed - system lacked buffer space

I'm trying to write a Silverlight client that communicates with a server on a virtual machine over sockets. The service works as expected for several minutes before throwing the following exception:

"An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full"

I can't figure out what it is I'm doing that depleting resources.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. while (true)
  3.             {
  4.                 try
  5.                 {
  6.                     SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
  7.                     socketArgs.Completed += onConnectComplete;
  8.                     socket.AcceptAsync(socketArgs);
  9.                 }
  10.                 catch (Exception ex)
  11.                 {
  12.                     Console.WriteLine(ex.Message);
  13.                 }
  14.             }
  15. ...
  16.  
  17. public void  onConnectComplete(object sender, SocketAsyncEventArgs e)
  18.         {
  19.             if (e.SocketError == SocketError.Success)
  20.             {
  21.                 // should only happen if a new socket has been connected
  22.  
  23.                 e.Completed -= onConnectComplete;
  24.  
  25.                 myClient = e.AcceptSocket;
  26.  
  27.                 Console.WriteLine("Connected!");
  28.  
  29.                 myTimer.Start();
  30.  
  31.                 SocketAsyncEventArgs sockArgs = new SocketAsyncEventArgs();
  32.                 sockArgs.Completed += onReceiveComplete;
  33.                 byte[] response = new byte[1024];
  34.                 sockArgs.SetBuffer(response, 0, response.Length);
  35.                 myClient.ReceiveAsync(sockArgs);
  36.             }
  37.         }
  38.  
Thanks
Nov 10 '08 #1
4 6711
Plater
7,872 Expert 4TB
That sounds like somewhere there is a "busy loop" (which would affect async sockets) that is preventing the data from being reported to you as arrived so you could read it.

I'm not able to have a close look at your async logic, but it also looks suspect
Nov 10 '08 #2
tlhintoq
3,525 Expert 2GB
That sounds like somewhere there is a "busy loop" (which would affect async sockets) that is preventing the data from being reported to you as arrived so you could read it.

I'm not able to have a close look at your async logic, but it also looks suspect
I'm not a socket guy, so I could be off base here but...
I think its the "while (true)" part.

Since true will always be true this while loop never stops so it just keeps making new arguments and wiring events for it, as fast as it can loop.

Perhaps it should become 'while (bVariable)' and once you have a connection flip that boolean to false so it quits looping???
Nov 11 '08 #3
Plater, I am able to get the data trasmitted for several minutes before the exception is hit, so I'm not caught in an infinite loop somewhere that I don't know about.

The reason for the while(true) is to continue to accept incoming sockets on this server (I'm not getting that functionality either at this point, but I suspect these two problems are related to some degree) I agree that it would be nice to declare the new SocketAsyncEventArgs object outside of the while loop... this is likely where I'm eating up lots of memory very quickly (considering how expensive 'new' objects can be). But this object cannot be reused so that's out.

What I decided to do was to force pseudo-synchronous behavior by implementing a simple locking mechanism on the logic in the while loop:

...
while (true)
{
if (!blocked)
{
try
{
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
socketArgs.Completed += onConnectComplete;
socket.AcceptAsync(socketArgs);
blocked = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
...

public void onConnectComplete(object sender, SocketAsyncEventArgs e)
{
blocked = false;

if (e.SocketError == SocketError.Success)
{
// should only happen if a new socket has been connected

e.Completed -= onConnectComplete;
...
Nov 11 '08 #4
Plater
7,872 Expert 4TB
I would suggest, inside your while(true) loop (perferably before the accept async call) put an Application.DoEvents(). See if that clears up some problems.

Also, if AcceptAysnc() is a non blocking call (pretty sure it blocks, but it might not) you could be eating up memory by making worthless objects for false connections.

Something like this maybe?
Expand|Select|Wrap|Line Numbers
  1. //
  2. //
  3. bool KeepLooping=true;
  4. while (KeepLooping) 
  5.     try 
  6.     {//the .Poll() call in this case only works for sockets in "listen mode"
  7.         bool pending=socket.Poll(200, System.Net.Sockets.SelectMode.SelectRead);
  8.         if(pending)
  9.         {
  10.             SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs(); 
  11.             socketArgs.Completed += onConnectComplete; 
  12.             socket.AcceptAsync(socketArgs); 
  13.         }
  14.       Application.DoEvents();
  15.     } 
  16.     catch (Exception ex) 
  17.     { 
  18.       Console.WriteLine(ex.Message); 
  19.     } 
  20.  
  21. //
  22. //
  23.  
Nov 11 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bryan Olson | last post by:
Here's the problem: Suppose we use: import socket f = some_socket.makefile() Then: f.read() is efficient, but verbose, and incorrect (or at least does not play will with others);
0
by: Jack | last post by:
I am trying to develop a simple telnet client in MS.NET I have a small wrapper around the System.Net.Sockets.Socket class which takes care of sending and receiving data and handing it off to a...
1
by: Frank Jones | last post by:
When I call Dns.GetHostByName(Dns.GetHostName()) on one of our lab boxes I get the below exception. I've seen posts saying others in these newsgroups have seen similar issues since moving to .Net....
4
by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
6
by: Aero | last post by:
Hi, My window application written in C# is throwing following exception while connecting to one FTP location The type initializer for System.Net.Sockets.Socket threw an exception This exe is...
9
by: Irmen de Jong | last post by:
Hi, Recently I was bitten by an apparent bug in the BSD socket layer on Open VMS. Specifically, it appears that VMS defines MSG_WAITALL in socket.h but does not implement it (it is not in the...
0
by: irfanullah | last post by:
I have written an application for Receiving Stock Market Feed over a 2 Mbps Connection with Exchange with average incoming data of 800kbps. The program works fine for sometime i.e. for an hour or...
2
by: kodart | last post by:
Introduction Performance is the main concern to most server application developers. That’s why many of them anticipate using .NET platform to develop high performance server application regardless...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.