By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.Sockets;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
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 DataRecivedHandler(object sender, DataEventArgs
e);
public class DataEventArgs : EventArgs
{
public string data;
public bool corGuess;
public TcpClient client = new TcpClient();
public DataEventArgs(string data)
{
this.data = data;
this.corGuess = false;
}
public DataEventArgs(string data, bool corGuess)
{
this.data =data;
this.corGuess = corGuess;
}
public DataEventArgs(string data, TcpClient client)
{
this.data = data;
this.client = client;
}
}
public class tcpClient
{
public event DataRecivedHandler DataReceived;
private string gameip;
//private userObject user;
private TcpClient Client;
private StreamWriter SW;
private StreamReader SR;
public bool connected = false;
public tcpClient(string gameip)
{
bool error =false;
this.gameip = gameip;
Console.WriteLine(gameip.ToString());
//this.user = user;
Client = new TcpClient();
try
{
Client.Connect(gameip, 9090);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
error =true;
}
if(!error)
{
connected = true;
Thread t = new Thread(new ThreadStart(recData));
t.Start();
}
SW = new StreamWriter(Client.GetStream());
SR = new StreamReader(Client.GetStream());
Console.WriteLine("New files created");
}
public void sendData(string data)
{
SW.AutoFlush = true;
SW.WriteLine(data);
SW.Flush();
//SW.Close()
}
private void recData()
{
//NetworkStream NR = Client.GetStream();

while(true)
{
try
{
string data = SR.ReadLine();
//onsole.WriteLine("Client Recived: " + data);
DataEventArgs dea = new DataEventArgs(data);
if(data != null)
{
DataReceived(this, 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.Sockets.SocketException: Too many open files
at System.Net.Sockets.Socket..ctor (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sockets.TcpClient.Init (AddressFamily family) [0x00000]
at System.Net.Sockets.TcpClient..ctor () [0x00000]
at FINN.DataEventArgs..ctor (System.String data) [0x00000]
at FINN.frmGame.picMainDrawArea_mouseMove (System.Object sender,
System.Windows.Forms.MouseEventArgs e) [0x00000]
at (wrapper delegate-invoke)
System.MulticastDelegate:invoke_void_object_MouseE ventArgs
(object,System.Windows.Forms.MouseEventArgs)
at System.Windows.Forms.Control.OnMouseMove
(System.Windows.Forms.MouseEventArgs e) [0x00000]
at System.Windows.Forms.Control.WndProc
(System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.Control+ControlNativeWindow.W ndProc
(System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.NativeWindow.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
Share this Question
Share on Google+
7 Replies


P: n/a
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.com

"Lee" <lv******@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.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.Sockets;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
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 DataRecivedHandler(object sender, DataEventArgs
e);
public class DataEventArgs : EventArgs
{
public string data;
public bool corGuess;
public TcpClient client = new TcpClient();
public DataEventArgs(string data)
{
this.data = data;
this.corGuess = false;
}
public DataEventArgs(string data, bool corGuess)
{
this.data =data;
this.corGuess = corGuess;
}
public DataEventArgs(string data, TcpClient client)
{
this.data = data;
this.client = client;
}
}
public class tcpClient
{
public event DataRecivedHandler DataReceived;
private string gameip;
//private userObject user;
private TcpClient Client;
private StreamWriter SW;
private StreamReader SR;
public bool connected = false;
public tcpClient(string gameip)
{
bool error =false;
this.gameip = gameip;
Console.WriteLine(gameip.ToString());
//this.user = user;
Client = new TcpClient();
try
{
Client.Connect(gameip, 9090);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
error =true;
}
if(!error)
{
connected = true;
Thread t = new Thread(new ThreadStart(recData));
t.Start();
}
SW = new StreamWriter(Client.GetStream());
SR = new StreamReader(Client.GetStream());
Console.WriteLine("New files created");
}
public void sendData(string data)
{
SW.AutoFlush = true;
SW.WriteLine(data);
SW.Flush();
//SW.Close()
}
private void recData()
{
//NetworkStream NR = Client.GetStream();

while(true)
{
try
{
string data = SR.ReadLine();
//onsole.WriteLine("Client Recived: " + data);
DataEventArgs dea = new DataEventArgs(data);
if(data != null)
{
DataReceived(this, 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.Sockets.SocketException: Too many open files
at System.Net.Sockets.Socket..ctor (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sockets.TcpClient.Init (AddressFamily family) [0x00000]
at System.Net.Sockets.TcpClient..ctor () [0x00000]
at FINN.DataEventArgs..ctor (System.String data) [0x00000]
at FINN.frmGame.picMainDrawArea_mouseMove (System.Object sender,
System.Windows.Forms.MouseEventArgs e) [0x00000]
at (wrapper delegate-invoke)
System.MulticastDelegate:invoke_void_object_MouseE ventArgs
(object,System.Windows.Forms.MouseEventArgs)
at System.Windows.Forms.Control.OnMouseMove
(System.Windows.Forms.MouseEventArgs e) [0x00000]
at System.Windows.Forms.Control.WndProc
(System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.Control+ControlNativeWindow.W ndProc
(System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.NativeWindow.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

P: n/a
"Lee" <lv******@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
[...]
System.Net.Sockets.SocketException: Too many open files
at System.Net.Sockets.Socket..ctor (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sockets.TcpClient.Init (AddressFamily family) [0x00000]
at System.Net.Sockets.TcpClient..ctor () [0x00000]
at FINN.DataEventArgs..ctor (System.String data) [0x00000]
at FINN.frmGame.picMainDrawArea_mouseMove (System.Object sender,
System.Windows.Forms.MouseEventArgs 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

P: n/a
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.googlegro ups.com...
[...]
System.Net.Sockets.SocketException: Too many open files
at System.Net.Sockets.Socket..ctor (AddressFamily family, SocketType
type, ProtocolType proto) [0x00000]
at System.Net.Sockets.TcpClient.Init (AddressFamily family) [0x00000]
at System.Net.Sockets.TcpClient..ctor () [0x00000]
at FINN.DataEventArgs..ctor (System.String data) [0x00000]
at FINN.frmGame.picMainDrawArea_mouseMove (System.Object sender,
System.Windows.Forms.MouseEventArgs 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

P: n/a
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.WriteLine("Client Recived: " + data);
DataEventArgs
dea = new DataEventArgs(data);
if(data !=
null)
{

DataReceived(this, 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

P: n/a
"Lee" <lv******@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.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

P: n/a
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*********************@f1g2000cwa.googlegrou ps.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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.