473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SocketException : Too many open files - can't understand why though?

Lee
Hey all, I'm using the following code to send stuff accross the
network, appologies for it being in full, but I've really no idea
exactly where this error is occuring.

=======network code=========== =

using System;
using System.Net;
using System.Net.Sock ets;
using System.Threadin g;
using System.Windows. Forms;
using System.Componen tModel;
using System.Text;
using System.IO;
//Class for actually sending data accross the network
//should be used by parser.cs and perhaps a few forms
namespace FINN
{
public delegate void DataRecivedHand ler(object sender, DataEventArgs
e);
public class DataEventArgs : EventArgs
{
public string data;
public bool corGuess;
public TcpClient client = new TcpClient();
public DataEventArgs(s tring data)
{
this.data = data;
this.corGuess = false;
}
public DataEventArgs(s tring data, bool corGuess)
{
this.data =data;
this.corGuess = corGuess;
}
public DataEventArgs(s tring data, TcpClient client)
{
this.data = data;
this.client = client;
}
}
public class tcpClient
{
public event DataRecivedHand ler DataReceived;
private string gameip;
//private userObject user;
private TcpClient Client;
private StreamWriter SW;
private StreamReader SR;
public bool connected = false;
public tcpClient(strin g gameip)
{
bool error =false;
this.gameip = gameip;
Console.WriteLi ne(gameip.ToStr ing());
//this.user = user;
Client = new TcpClient();
try
{
Client.Connect( gameip, 9090);
}
catch (Exception e)
{
Console.WriteLi ne(e.ToString() );
error =true;
}
if(!error)
{
connected = true;
Thread t = new Thread(new ThreadStart(rec Data));
t.Start();
}
SW = new StreamWriter(Cl ient.GetStream( ));
SR = new StreamReader(Cl ient.GetStream( ));
Console.WriteLi ne("New files created");
}
public void sendData(string data)
{
SW.AutoFlush = true;
SW.WriteLine(da ta);
SW.Flush();
//SW.Close()
}
private void recData()
{
//NetworkStream NR = Client.GetStrea m();

while(true)
{
try
{
string data = SR.ReadLine();
//onsole.WriteLin e("Client Recived: " + data);
DataEventArgs dea = new DataEventArgs(d ata);
if(data != null)
{
DataReceived(th is, dea);
}
}
catch
{
}

}
SR.Close();
}
public void Disconnect()
{
Client.Close();
}
}

}
=======network code=========== =
This works fine for the most part, but when sendData gets called
repeatedly it causes it to throw the following exception and crash out:

System.Net.Sock ets.SocketExcep tion: Too many open files
at System.Net.Sock ets.Socket..cto r (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sock ets.TcpClient.I nit (AddressFamily family) [0x00000]
at System.Net.Sock ets.TcpClient.. ctor () [0x00000]
at FINN.DataEventA rgs..ctor (System.String data) [0x00000]
at FINN.frmGame.pi cMainDrawArea_m ouseMove (System.Object sender,
System.Windows. Forms.MouseEven tArgs e) [0x00000]
at (wrapper delegate-invoke)
System.Multicas tDelegate:invok e_void_object_M ouseEventArgs
(object,System. Windows.Forms.M ouseEventArgs)
at System.Windows. Forms.Control.O nMouseMove
(System.Windows .Forms.MouseEve ntArgs e) [0x00000]
at System.Windows. Forms.Control.W ndProc
(System.Windows .Forms.Message& m) [0x00000]
at System.Windows. Forms.Control+C ontrolNativeWin dow.WndProc
(System.Windows .Forms.Message& m) [0x00000]
at System.Windows. Forms.NativeWin dow.WndProc (IntPtr hWnd, Msg msg,
IntPtr wParam, IntPtr lParam) [0x00000]
I really can't figure out why this is occuring and was wondering if
anyone was able to help me improve my code and show me where it's wrong
(and why!) so that I can stop this from occuring.
Cheers all
Lee

Dec 13 '06 #1
7 5605
Lee,

It's impossible to tell. You aren't even showing how you are using the
class.

Also, why not just use the TcpClient class that the framework provides?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Lee" <lv******@gmail .comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.com...
Hey all, I'm using the following code to send stuff accross the
network, appologies for it being in full, but I've really no idea
exactly where this error is occuring.

=======network code=========== =

using System;
using System.Net;
using System.Net.Sock ets;
using System.Threadin g;
using System.Windows. Forms;
using System.Componen tModel;
using System.Text;
using System.IO;
//Class for actually sending data accross the network
//should be used by parser.cs and perhaps a few forms
namespace FINN
{
public delegate void DataRecivedHand ler(object sender, DataEventArgs
e);
public class DataEventArgs : EventArgs
{
public string data;
public bool corGuess;
public TcpClient client = new TcpClient();
public DataEventArgs(s tring data)
{
this.data = data;
this.corGuess = false;
}
public DataEventArgs(s tring data, bool corGuess)
{
this.data =data;
this.corGuess = corGuess;
}
public DataEventArgs(s tring data, TcpClient client)
{
this.data = data;
this.client = client;
}
}
public class tcpClient
{
public event DataRecivedHand ler DataReceived;
private string gameip;
//private userObject user;
private TcpClient Client;
private StreamWriter SW;
private StreamReader SR;
public bool connected = false;
public tcpClient(strin g gameip)
{
bool error =false;
this.gameip = gameip;
Console.WriteLi ne(gameip.ToStr ing());
//this.user = user;
Client = new TcpClient();
try
{
Client.Connect( gameip, 9090);
}
catch (Exception e)
{
Console.WriteLi ne(e.ToString() );
error =true;
}
if(!error)
{
connected = true;
Thread t = new Thread(new ThreadStart(rec Data));
t.Start();
}
SW = new StreamWriter(Cl ient.GetStream( ));
SR = new StreamReader(Cl ient.GetStream( ));
Console.WriteLi ne("New files created");
}
public void sendData(string data)
{
SW.AutoFlush = true;
SW.WriteLine(da ta);
SW.Flush();
//SW.Close()
}
private void recData()
{
//NetworkStream NR = Client.GetStrea m();

while(true)
{
try
{
string data = SR.ReadLine();
//onsole.WriteLin e("Client Recived: " + data);
DataEventArgs dea = new DataEventArgs(d ata);
if(data != null)
{
DataReceived(th is, dea);
}
}
catch
{
}

}
SR.Close();
}
public void Disconnect()
{
Client.Close();
}
}

}
=======network code=========== =
This works fine for the most part, but when sendData gets called
repeatedly it causes it to throw the following exception and crash out:

System.Net.Sock ets.SocketExcep tion: Too many open files
at System.Net.Sock ets.Socket..cto r (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sock ets.TcpClient.I nit (AddressFamily family) [0x00000]
at System.Net.Sock ets.TcpClient.. ctor () [0x00000]
at FINN.DataEventA rgs..ctor (System.String data) [0x00000]
at FINN.frmGame.pi cMainDrawArea_m ouseMove (System.Object sender,
System.Windows. Forms.MouseEven tArgs e) [0x00000]
at (wrapper delegate-invoke)
System.Multicas tDelegate:invok e_void_object_M ouseEventArgs
(object,System. Windows.Forms.M ouseEventArgs)
at System.Windows. Forms.Control.O nMouseMove
(System.Windows .Forms.MouseEve ntArgs e) [0x00000]
at System.Windows. Forms.Control.W ndProc
(System.Windows .Forms.Message& m) [0x00000]
at System.Windows. Forms.Control+C ontrolNativeWin dow.WndProc
(System.Windows .Forms.Message& m) [0x00000]
at System.Windows. Forms.NativeWin dow.WndProc (IntPtr hWnd, Msg msg,
IntPtr wParam, IntPtr lParam) [0x00000]
I really can't figure out why this is occuring and was wondering if
anyone was able to help me improve my code and show me where it's wrong
(and why!) so that I can stop this from occuring.
Cheers all
Lee

Dec 13 '06 #2
"Lee" <lv******@gmail .comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.com...
[...]
System.Net.Sock ets.SocketExcep tion: Too many open files
at System.Net.Sock ets.Socket..cto r (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sock ets.TcpClient.I nit (AddressFamily family) [0x00000]
at System.Net.Sock ets.TcpClient.. ctor () [0x00000]
at FINN.DataEventA rgs..ctor (System.String data) [0x00000]
at FINN.frmGame.pi cMainDrawArea_m ouseMove (System.Object sender,
System.Windows. Forms.MouseEven tArgs e) [0x00000]
at (wrapper delegate-invoke)
[...]
I really can't figure out why this is occuring and was wondering if
anyone was able to help me improve my code and show me where it's wrong
(and why!) so that I can stop this from occuring.
Just a guess, but I suspect that you have too many files open.

It's hard for me to read the code you posted, as there's no indentation at
all, and it's not clear that you've actually posted a good sample (e.g.
minimal code required to *fully* reproduce the issue). However, creating a
TcpClient inside your mouse-move event handler is a little odd. Mouse-moves
happen all the time...I don't see any legitimate reason to create a new
TcpClient each time one happens, even if one does assume that those
TcpClients are properly cleaned up (and given the error you're getting, it
seems they may not be).

So, rather than creating a new TcpClient every time you instantiate a
DataEventArgs, perhaps it would be better for you to create a TcpClient
once, and then supply that to the DataEventArgs constructor, or otherwise
gain access to it.

I suppose you could keep doing it the way you're doing it now, as long as
you added an explicit Close/Dispose method to the DataEventArgs class and
called that everywhere that you create a DataEventArgs and then are done
using it. But that's a pain, and it ignores the fact that creating a new
TcpClient every time you move the mouse is still a bad idea.

Pete
Dec 13 '06 #3
Lee
Humm, sorry it is tab-indented here... guess it got lost in the post
somehow.

To clear things up, this class isn't instantiated when mouse move is
called, only it's 'sendData' method is called then.

The reason I'm not using TcpClient class is because, well, this is
merely a wrapper to it, to encapsulate reciving data and have an
appropriate event triggered.

If required I could re-post this class onto a paste-bin and hopefully
tab indenting would be preserved?

Peter Duniho wrote:
"Lee" <lv******@gmail .comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.com...
[...]
System.Net.Sock ets.SocketExcep tion: Too many open files
at System.Net.Sock ets.Socket..cto r (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sock ets.TcpClient.I nit (AddressFamily family) [0x00000]
at System.Net.Sock ets.TcpClient.. ctor () [0x00000]
at FINN.DataEventA rgs..ctor (System.String data) [0x00000]
at FINN.frmGame.pi cMainDrawArea_m ouseMove (System.Object sender,
System.Windows. Forms.MouseEven tArgs e) [0x00000]
at (wrapper delegate-invoke)
[...]
I really can't figure out why this is occuring and was wondering if
anyone was able to help me improve my code and show me where it's wrong
(and why!) so that I can stop this from occuring.

Just a guess, but I suspect that you have too many files open.

It's hard for me to read the code you posted, as there's no indentation at
all, and it's not clear that you've actually posted a good sample (e.g.
minimal code required to *fully* reproduce the issue). However, creating a
TcpClient inside your mouse-move event handler is a little odd. Mouse-moves
happen all the time...I don't see any legitimate reason to create a new
TcpClient each time one happens, even if one does assume that those
TcpClients are properly cleaned up (and given the error you're getting, it
seems they may not be).

So, rather than creating a new TcpClient every time you instantiate a
DataEventArgs, perhaps it would be better for you to create a TcpClient
once, and then supply that to the DataEventArgs constructor, or otherwise
gain access to it.

I suppose you could keep doing it the way you're doing it now, as long as
you added an explicit Close/Dispose method to the DataEventArgs class and
called that everywhere that you create a DataEventArgs and then are done
using it. But that's a pain, and it ignores the fact that creating a new
TcpClient every time you move the mouse is still a bad idea.

Pete
Dec 13 '06 #4
Hey all, I'm using the following code to send stuff accross the
network, appologies for it being in full, but I've really no idea
exactly where this error is occuring.
while(true)
{
try
{
string data =
SR.ReadLine();

//onsole.WriteLin e("Client Recived: " + data);
DataEventArgs
dea = new DataEventArgs(d ata);
if(data !=
null)
{

DataReceived(th is, dea);
}
}
catch
{
}

}

Your while loop is infinite. Every time through the loop, you create a
new instance of DataEventArgs. Inside that class, you create a new
instance of the TcpClient class. I suspect that is why, but I'm not
sure.

Chris

Dec 14 '06 #5
"Lee" <lv******@gmail .comwrote in message
news:11******** *************@f 1g2000cwa.googl egroups.com...
Humm, sorry it is tab-indented here... guess it got lost in the post
somehow.

To clear things up, this class isn't instantiated when mouse move is
called, only it's 'sendData' method is called then.
Well, the stack trace you posted says otherwise.

You may think that you aren't instantiating a DataEventArgs object when your
picMainDrawArea _mouseMove method is called, but you clearly are. Since you
didn't post that code, I don't know for sure whether it's EVERY time that
method is called, but I suspect it is. Either way, it's a problem.

I also agree with Chris's comment about your while() loop in your recData
method. That is another place you are creating DataEventArgs objects, and
if you execute that loop frequently enough, those objects might not get
cleaned up quickly enough to avoid having too many files open.

That said, the exception occurs while creating a DataEventArgs object inside
your picMainDrawArea _mouseMove method. So that's the first place you need
to look.

Pete
Dec 14 '06 #6
Lee
Thanks all, I'll check it out when I'm back at work tomorrow and post
back how I get on.
Peter Duniho wrote:
"Lee" <lv******@gmail .comwrote in message
news:11******** *************@f 1g2000cwa.googl egroups.com...
Humm, sorry it is tab-indented here... guess it got lost in the post
somehow.

To clear things up, this class isn't instantiated when mouse move is
called, only it's 'sendData' method is called then.

Well, the stack trace you posted says otherwise.

You may think that you aren't instantiating a DataEventArgs object when your
picMainDrawArea _mouseMove method is called, but you clearly are. Since you
didn't post that code, I don't know for sure whether it's EVERY time that
method is called, but I suspect it is. Either way, it's a problem.

I also agree with Chris's comment about your while() loop in your recData
method. That is another place you are creating DataEventArgs objects, and
if you execute that loop frequently enough, those objects might not get
cleaned up quickly enough to avoid having too many files open.

That said, the exception occurs while creating a DataEventArgs object inside
your picMainDrawArea _mouseMove method. So that's the first place you need
to look.

Pete
Dec 14 '06 #7
Lee
Changing

public TcpClient client = new TcpClient();

to

public TcpClient client;

Fixed the problem, thanks all! :-)

(The rest of the code is probably still rubbish, but at least it works
now)

Dec 14 '06 #8

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

Similar topics

2
9977
by: MichaelB | last post by:
When I try logging into my web app using local host I am getting the "HTTP 403.9 - Access Forbidden: Too many users are connected Internet Information Services". The thing is I do not have 10 users connected! I have been running Microsoft Application Center Test for load testing purposes, and also another application called "ANTS Load" for the same purpose. I don't know if there's any direct relation between doing load testing with these...
3
6344
by: grzybek | last post by:
Hi, I upload files to ma Web Server and then I can view those files as link. I'd like to be able to open those file in proper application on client PC. I want achieve the same result as though these files were saved locally on the client disk. For example .exe files - open window ( open , save or cancel possibility ) or for .doc files to open in Word application or save on disk.
7
14622
by: Tyrone Showers | last post by:
I have a problem of getting the error "too many files open" and would like to trace my application. However, I have found nothing about how to display the current number of open files. Does anyone know what code is used to get the current number of open files? Also, is there a way to determine the number of open database connections?
0
3143
by: oferns | last post by:
Hi, apologies for the cluncky title.... Using WinXpSP2 & VS2005 Express editions..... I am writing an asynchronous socket client and I am getting a SocketException error on creating the socket: + EnableBroadcast 'this.client.EnableBroadcast' threw an exception of type 'System.Net.Sockets.SocketException' bool
7
2013
by: jonathandrott | last post by:
sorry newbie question probably. i'm trying to open an specific folder. open each file with in the folder individually and process each one. all the processing code has been written. i'm looking for sample code to open the folder and start grabbing files. thanks.
2
3038
by: John Donnell | last post by:
We have recently added another PC to the network and have run into difficulty when trying to open access files from the server using the new box. We get the error message 'MS Access cannot open this file. The file is located outside your intranet or on an untrusted site. MS Access will not open the file due to potential security problems. To open the file, copy it to your machine or an accessible network location.' Yes it can be...
7
8529
by: pike | last post by:
db2 8.1 FP11 on AIX 5.3.0.0 . The db2diag.log is intermittently reporting EMFILE (24) "Too many open files" errors. The culprit is always db2hmon. Sample db2diag.log output follows: 2007-03-20-07.42.35.269106+060 I14996239C505 LEVEL: Severe (OS) PID : 2289758 TID : 772 PROC : db2hmon 0 INSTANCE: defser_t NODE : 000 FUNCTION: DB2 UDB, SQO Memory Management, sqlocshr2, probe:200
4
5015
by: sd1978 | last post by:
Hi, I have placed a webservice in the webserver. When I access it from a webpage, default.aspx on a click of a button i get the following error: No connection could be made because the target machine actively refused it Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details:...
5
2631
by: PJ6 | last post by:
I can't figure this out. I have several references to style sheets and javascript files in my page header, the contents of which are served dynamically by my httphandler that serves *.res requests. The handler works most of the time, but occasionally the page load hangs on the loading of the last .res reference for a javascript file (Fiddler shows me this), and when I stop the load in the browser, I get this error message: A first chance...
0
10031
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
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,...
1
7242
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
6534
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
5140
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...
1
3805
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
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
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.