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
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