All pastes #277626 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-12-13 13:02 UTC
rendered paste body
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());
		}
		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();
		}
	}
	
}