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

L

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_MouseEventArgs
(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.WndProc
(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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Lee said:
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_MouseEventArgs
(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.WndProc
(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
 
P

Peter Duniho

Lee said:
[...]
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
 
L

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 said:
Lee said:
[...]
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
 
C

Chris Dunaway

Hey all, I'm using the following code to send stuff accross the
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
 
P

Peter Duniho

Lee said:
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
 
L

Lee

Thanks all, I'll check it out when I'm back at work tomorrow and post
back how I get on.
 
L

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top