TCP ZeroWindow Issue C# - c#

i have been writing a file server/client application in c# and it appeared to be working but then i realized the the stream was not being advanced and the client kept failing to receive from the server. so i checked wireshark and saw that my client was emitting TCPZeroWindow flagged packets. Any thoughts? Thanks.
Client :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace PortalClient
{
class Program
{
static string s1;
static byte[] data03;
static int int02;
static string str01;
static int length;
static IPEndPoint ipe01;
static TcpClient tcp01;
static FileStream s01;
static string ip01;
static string port01;
static string path01;
static byte[] data01;
static byte[] data02;
static byte[] data04;
static void Main(string[] args)
{
while (true)
{
label1:
{
try
{
string version = "V:1.1.4";
Console.Title = (" Portal Client " + version);
ConsoleColor ccolor1 = new ConsoleColor();
ccolor1 = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(" PORTAL CLIENT " + version);
Console.WriteLine();
Console.ForegroundColor = ccolor1;
data01 = new byte[20];
data03 = new byte[100];
data04 = new byte[100];
Console.Write(" Enter IPv4 address of server : ");
ip01 = Console.ReadLine();
Console.Write(" Enter port to connect on : ");
port01 = Console.ReadLine();
ipe01 = new IPEndPoint(IPAddress.Parse(ip01), Convert.ToInt32(port01));
tcp01 = new TcpClient();
tcp01.ReceiveTimeout = 2500;
tcp01.NoDelay = true;
Console.WriteLine(" Connecting...");
tcp01.Connect(ipe01);
Console.WriteLine(" Done.");
tcp01.Client.Receive(data04, SocketFlags.None);
System.Threading.Thread.Sleep(100);
Console.WriteLine(" Server message : " + Encoding.UTF8.GetString(data04));
tcp01.Client.Receive(data03, SocketFlags.None);
System.Threading.Thread.Sleep(100);
Console.WriteLine(" File on server : " + Encoding.UTF8.GetString(data03));
tcp01.Client.Receive(data01, SocketFlags.None);
System.Threading.Thread.Sleep(100);
str01 = Encoding.UTF8.GetString(data01);
Console.WriteLine();
Console.WriteLine(" file size : " + str01);
Console.WriteLine();
Console.Write(" Enter the number you see above : ");
length = Convert.ToInt32(Console.ReadLine());
Console.Write(" Save file as : ");
path01 = Console.ReadLine();
for (int i = 1; i <= 9000; i++)
{
if (length % i == 0) { int02 = i; }
}
if (length < 9000) { int02 = length; }
int int03 = length / int02;
s01 = File.OpenWrite(#path01);
Console.WriteLine(" Receiving file from " + tcp01.Client.RemoteEndPoint.ToString() + "...");
tcp01.Client.Send(new byte[1], SocketFlags.None);
System.Threading.Thread.Sleep(1000);
for (int i = 0; i <= int03; i++)
{
bool bool1 = false;
data02 = new byte[int02];
int n = tcp01.Client.Receive(data02, 0, int02, SocketFlags.None);
while (n < int02)
{
Console.WriteLine(" Entered Loop ");
int int04 = int02 - n;
tcp01.Client.Send(Encoding.UTF8.GetBytes("2"), 0, 1, SocketFlags.None);
int int05 = 0;
byte[] data05 = new byte[int04];
if (int04 >= 1 && int04 <= 9) { s1 = int04 + "xxx"; }
if (int04 >= 10 && int04 <= 99) { s1 = int04 + "xx"; }
if (int04 >= 100 && int04 <= 999) { s1 = int04 + "x"; }
if (int04 >= 1000 && int04 <= 9000) { s1 = int04.ToString(); }
tcp01.Client.Send(Encoding.UTF8.GetBytes(s1), 0, 4, SocketFlags.None);
int05 = tcp01.Client.Receive(data05, 0, int04, SocketFlags.None);
n = n + int05;
s01.Write(data05, 0, int05);
bool1 = true;
}
if (bool1 == false)
{
s01.Write(data02, 0, int02);
tcp01.Client.Send(new byte[1], 0, 1, SocketFlags.None);
}
}
System.Threading.Thread.Sleep(1000);
s01.Close();
Console.WriteLine(" Received all data.");
Console.WriteLine(" Press enter to disconnect...");
Console.ReadLine();
tcp01.Client.Send(new byte[1], SocketFlags.None);
System.Threading.Thread.Sleep(1000);
Console.WriteLine(" Disconnecting...");
tcp01.Close();
Console.WriteLine(" Done.");
}
catch (Exception e)
{
Console.WriteLine(" Error! : " + e.Message.ToString() + " - " + e.Data.ToString() + " - " + e.TargetSite.ToString()); if (!(tcp01 == null)) { tcp01.Close(); } if (!(s01 == null)) { s01.Close(); goto label1; }
}
}
}
}
}
}
Server :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace PortalServer
{
class Program
{
static long length;
static int int03;
static int int02;
static TcpListener tcp01;
static TcpClient tcp02;
static FileStream s01;
static byte[] data01;
static byte[] data03;
static byte[] data04;
static byte[] data05;
static string str01;
static string str02;
static IPEndPoint ipe01;
static string port01;
static void Main(string[] args)
{
while (true)
{
label1:
try
{
string version = "V:1.1.4";
Console.Title = (" Portal Server " + version);
ConsoleColor ccolor1 = new ConsoleColor();
ccolor1 = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(" PORTAL SERVER " + version);
Console.WriteLine();
Console.ForegroundColor = ccolor1;
Console.Write(" Enter port for connecting clients : ");
port01 = Console.ReadLine();
Console.Write(" Enter path of file to send : ");
str01 = Console.ReadLine();
ipe01 = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port01));
tcp01 = new TcpListener(ipe01);
Console.Write(" Enter server message : ");
str02 = Console.ReadLine();
s01 = File.OpenRead(#str01);
length = s01.Length;
for (int i = 1; i <= 9000; i++)
{
if (length % i == 0) { int02 = i; }
}
if (length < 9000) { int02 = (int)length;}
int03 = (int)length / int02;
tcp01.Start();
Console.WriteLine(" Server started. Waiting for clients...");
tcp02 = tcp01.AcceptTcpClient();
tcp02.Client.NoDelay = true;
Console.WriteLine(" Client " + tcp02.Client.RemoteEndPoint.ToString() + " connected.");
data05 = Encoding.UTF8.GetBytes(str02);
tcp02.Client.Send(data05, SocketFlags.None);
System.Threading.Thread.Sleep(500);
data04 = Encoding.UTF8.GetBytes(str01);
tcp02.Client.Send(data04, SocketFlags.None);
System.Threading.Thread.Sleep(500);
data03 = Encoding.UTF8.GetBytes(length.ToString());
tcp02.Client.Send(data03, SocketFlags.None);
System.Threading.Thread.Sleep(500);
Console.WriteLine(" Waiting for response...");
tcp02.Client.Receive(new byte[1], SocketFlags.None);
Console.WriteLine(" Received response...");
Console.WriteLine(" Sending file to " + tcp02.Client.RemoteEndPoint.ToString() + "...");
System.Threading.Thread.Sleep(1);
for (int i = 0; i <= int03;i++ )
{
System.Threading.Thread.Sleep(30);
data01 = new byte[int02];
s01.Read(data01, 0, data01.Length);
int n = tcp02.Client.Send(data01, 0 ,data01.Length, SocketFlags.None);
if (n != int02) { throw new Exception("unable to write bytes, insufficient memory."); }
byte[] data07 = new byte[1];
while (true)
{
tcp02.Client.Receive(data07,0,1,SocketFlags.None);
if (Encoding.UTF8.GetString(data07) == "2")
{
byte[] data06 = new byte[4];
int b = tcp02.Client.Receive(data06, 0, 4, SocketFlags.None);
if (b != 4) { throw new Exception("ex1"); }
string s1 = Encoding.UTF8.GetString(data06);
s1 = s1.Replace("x", "");
int int05 = Convert.ToInt32(s1);
int int06 = int02 - int05;
byte[] data08 = new byte[int05];
Buffer.BlockCopy(data01, int06, data08, 0, int05);
tcp02.Client.Send(data08, 0, data08.Length, SocketFlags.None);
System.Threading.Thread.Sleep(30);
}
break;
}
}
System.Threading.Thread.Sleep(1000);
s01.Close();
Console.WriteLine(" All data sent.");
Console.WriteLine(" Waiting for terminate message...");
tcp02.Client.Receive(new byte[1], SocketFlags.None);
Console.WriteLine(" Received terminate message.");
tcp02.Close();
Console.WriteLine(" Stopping client and server.");
tcp01.Stop();
Console.WriteLine(" Done. Restarting.");
}
catch (Exception e)
{
Console.WriteLine(" Error! : " + e.Message.ToString() + " - " + e.Data.ToString() + " - " + e.TargetSite.ToString());
if (!(tcp02 == null)) { tcp02.Close(); }
if (!(tcp01 == null)) { tcp01.Stop(); goto label1; }
}
}
}
}
}

I've seen this happen if you are not emptying the network buffer correctly at the client end and the TCP window size is reduced to zero as a consequence.
This could be happening with your use of
tcp01.Client.Receive(data04, SocketFlags.None);
which may not be clearing the buffer sufficiently, see MSDN. You want to call Receive continuously until no further data is available.
If you would like a working example as a comparison please see here.

I used separate threads for the heavy lifting send / receive methods, increased buffer size, increased time between sends, made sure fragmentation was on. works like a charm now.
client :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
namespace PortalClient
{
class Program
{
static int partloop;
static bool bool1;
static byte[] data05;
static int int03;
static int int04;
static int int05;
static int numbytesreceived;
static BackgroundWorker bw1;
static int bytestoreceive;
static string s1;
static byte[] data03;
static int int02;
static string str01;
static int length;
static IPEndPoint ipe01;
static TcpClient tcp01;
static FileStream s01;
static string ip01;
static string port01;
static string path01;
static byte[] data01;
static byte[] data02;
static byte[] data04;
static void Main(string[] args)
{
newvoid();
}
static void newvoid()
{
while (true)
{
try
{
Console.Clear();
Console.WindowWidth = 95;
string version = "V:1.1.4";
Console.Title = (" Portal Client " + version);
ConsoleColor ccolor1 = new ConsoleColor();
ccolor1 = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(#"
__________ __ .__ _________ .__ .__ __
\______ \____________/ |______ | | \_ ___ \| | |__| ____ _____/ |_
| ___/ _ \_ __ \ __\__ \ | | / \ \/| | | |/ __ \ / \ __\
| | ( <_> ) | \/| | / __ \| |__ \ \___| |_| \ ___/| | \ |
|____| \____/|__| |__| (____ /____/ \______ /____/__|\___ >___| /__|
\/ \/ \/ \/ " + version);
Console.WriteLine();
Console.ForegroundColor = ccolor1;
data01 = new byte[20];
data03 = new byte[100];
data04 = new byte[100];
Console.Write(" Enter IPv4 address of server : ");
ip01 = Console.ReadLine();
Console.Write(" Enter port to connect on : ");
port01 = Console.ReadLine();
ipe01 = new IPEndPoint(IPAddress.Parse(ip01), Convert.ToInt32(port01));
tcp01 = new TcpClient();
Console.WriteLine(" Connecting...");
tcp01.Connect(ipe01);
Console.WriteLine(" Done.");
tcp01.Client.Receive(data04, SocketFlags.None);
System.Threading.Thread.Sleep(100);
Console.WriteLine(" Server message : " + Encoding.UTF8.GetString(data04));
tcp01.Client.Receive(data03, SocketFlags.None);
System.Threading.Thread.Sleep(100);
Console.WriteLine(" File on server : " + Encoding.UTF8.GetString(data03));
tcp01.Client.Receive(data01, SocketFlags.None);
System.Threading.Thread.Sleep(100);
str01 = Encoding.UTF8.GetString(data01);
Console.WriteLine();
Console.WriteLine(" file size : " + str01);
Console.WriteLine();
Console.Write(" Enter the number you see above : ");
length = Convert.ToInt32(Console.ReadLine());
bytestoreceive = length;
Console.Write(" Save file as : ");
path01 = Console.ReadLine();
for (int i = 1; i <= 60000; i++)
{
if (length % i == 0) { int02 = i; }
}
if (length < 60000) { int02 = length; }
int03 = length / int02;
s01 = File.OpenWrite(#path01);
Console.WriteLine(" Receiving file from " + tcp01.Client.RemoteEndPoint.ToString() + "...");
tcp01.Client.Send(new byte[1], SocketFlags.None);
System.Threading.Thread.Sleep(1000);
while (bytestoreceive > 0)
{
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bool1 = false;
numbytesreceived = 0;
data02 = new byte[int02];
bw1.RunWorkerAsync();
while (numbytesreceived == 0) { }
while (numbytesreceived < int02)
{
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bool1 = true;
partloop += 1;
int04 = int02 - numbytesreceived;
tcp01.Client.Send(Encoding.UTF8.GetBytes("2"), 0, 1, SocketFlags.None);
int05 = 0;
data05 = new byte[int04];
if (int04 >= 1 && int04 <= 9) { s1 = int04 + "xxxx"; }
if (int04 >= 10 && int04 <= 99) { s1 = int04 + "xxx"; }
if (int04 >= 100 && int04 <= 999) { s1 = int04 + "xx"; }
if (int04 >= 1000 && int04 <= 9999) { s1 = int04 + "x"; }
if (int04 > 10000 && int04 <= 60000) { s1 = int04.ToString(); }
tcp01.Client.Send(Encoding.UTF8.GetBytes(s1), 0, 5, SocketFlags.None);
bw1.RunWorkerAsync();
while (int05 == 0) { }
if (int05 == 0) { throw new Exception("ex1"); }
numbytesreceived += int05;
bytestoreceive -= numbytesreceived;
s01.Write(data05, 0, data05.Length);
Console.WriteLine(" Received " + (length - bytestoreceive) + " / " + length + " bytes | P.B.R. loops: " + partloop.ToString());
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
if (bool1 == false)
{
bytestoreceive -= numbytesreceived;
s01.Write(data02, 0, int02);
tcp01.Client.Send(new byte[1], 0, 1, SocketFlags.None);
Console.WriteLine(" Received " + (length - bytestoreceive) + " / " + length + " bytes | P.B.R. loops: " + partloop.ToString());
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
}
Console.SetCursorPosition(0, Console.CursorTop + 1);
Console.WriteLine();
System.Threading.Thread.Sleep(500);
s01.Close();
Console.WriteLine(" Received all data.");
Console.WriteLine(" Press enter to disconnect...");
Console.ReadLine();
tcp01.Client.Send(new byte[1], SocketFlags.None);
System.Threading.Thread.Sleep(1000);
Console.WriteLine(" Disconnecting...");
tcp01.Close();
Console.WriteLine(" Done.");
}
catch (Exception e)
{
Console.WriteLine(" Error! : " + e.Message.ToString() + " - " + e.Data.ToString() + " - " + e.TargetSite.ToString());
if (!(tcp01 == null)) { tcp01.Close(); }
if (!(s01 == null)) { s01.Close(); }
Console.Write(" Press enter to continue..."); Console.ReadLine();
newvoid();
}
}
}
static void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
bw1.Dispose();
}
catch (Exception e1)
{
Console.WriteLine(" Error! : " + e1.Message.ToString() + " - " + e1.Data.ToString() + " - " + e1.TargetSite.ToString());
if (!(tcp01 == null)) { tcp01.Close(); }
if (!(s01 == null)) { s01.Close(); }
Console.Write(" Press enter to continue..."); Console.ReadLine();
newvoid();
}
}
static void bw1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
if (bool1 == false)
{
numbytesreceived = tcp01.Client.Receive(data02, 0, int02, SocketFlags.None);
}
if (bool1 == true)
{
int05 = tcp01.Client.Receive(data05, 0, int04, SocketFlags.None);
}
}
catch (Exception e2)
{
Console.WriteLine(" Error! : " + e2.Message.ToString() + " - " + e2.Data.ToString() + " - " + e2.TargetSite.ToString());
if (!(tcp01 == null)) { tcp01.Close(); }
if (!(s01 == null)) { s01.Close(); }
Console.Write(" Press enter to continue..."); Console.ReadLine();
newvoid();
}
}
}
}
server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
namespace PortalServer
{
class Program
{
static int bytesresent;
static BackgroundWorker bw1;
static int int06;
static int int05;
static string string01;
static int bytesCreceived;
static byte[] data06;
static byte[] data07;
static byte[] data08;
static int bytessent;
static bool bool1;
static int bytestosend;
static long length;
static int int03;
static int int02;
static TcpListener tcp01;
static TcpClient tcp02;
static FileStream s01;
static byte[] data01;
static byte[] data03;
static byte[] data04;
static byte[] data05;
static string str01;
static string str02;
static IPEndPoint ipe01;
static string port01;
static void Main(string[] args)
{
newvoid();
}
static void newvoid()
{
while (true)
{
try
{
Console.Clear();
Console.WindowWidth = 95;
string version = "V:1.1.4";
Console.Title = (" Portal Server " + version);
ConsoleColor ccolor1 = new ConsoleColor();
ccolor1 = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(#"
__________ __ .__ _________
\______ \____________/ |______ | | / _____/ ______________ __ ___________
| ___/ _ \_ __ \ __\__ \ | | \_____ \_/ __ \_ __ \ \/ // __ \_ __ \
| | ( <_> ) | \/| | / __ \| |__ / \ ___/| | \/\ /\ ___/| | \/
|____| \____/|__| |__| (____ /____/ /_______ /\___ >__| \_/ \___ >__|
\/ \/ \/ \/ " + version);
Console.WriteLine();
Console.ForegroundColor = ccolor1;
Console.Write(" Enter port for connecting clients : ");
port01 = Console.ReadLine();
Console.Write(" Enter path of file to send : ");
str01 = Console.ReadLine();
ipe01 = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port01));
tcp01 = new TcpListener(ipe01);
Console.Write(" Enter server message : ");
str02 = Console.ReadLine();
s01 = File.OpenRead(#str01);
length = s01.Length;
bytestosend = (int)length;
for (int i = 1; i <= 60000; i++)
{
if (length % i == 0) { int02 = i; }
}
if (length < 60000) { int02 = (int)length; }
int03 = (int)length / int02;
tcp01.Start();
Console.WriteLine(" Server started. Waiting for clients...");
tcp02 = tcp01.AcceptTcpClient();
Console.WriteLine(" Client " + tcp02.Client.RemoteEndPoint.ToString() + " connected.");
data05 = Encoding.UTF8.GetBytes(str02);
tcp02.Client.Send(data05, SocketFlags.None);
System.Threading.Thread.Sleep(500);
data04 = Encoding.UTF8.GetBytes(str01);
tcp02.Client.Send(data04, SocketFlags.None);
System.Threading.Thread.Sleep(500);
data03 = Encoding.UTF8.GetBytes(length.ToString());
tcp02.Client.Send(data03, SocketFlags.None);
System.Threading.Thread.Sleep(500);
Console.WriteLine(" Waiting for response...");
tcp02.Client.Receive(new byte[1], SocketFlags.None);
Console.WriteLine(" Received response...");
Console.WriteLine(" Sending file to " + tcp02.Client.RemoteEndPoint.ToString() + "...");
System.Threading.Thread.Sleep(1);
while (bytestosend > 0)
{
System.Threading.Thread.Sleep(50);
bool1 = false;
bytessent = 0;
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
data01 = new byte[int02];
s01.Read(data01, 0, data01.Length);
bw1.RunWorkerAsync();
while (bytessent == 0) { }
if (bytessent != int02) { throw new Exception("unable to write bytes, insufficient memory."); }
while (true)
{
data07 = new byte[1];
tcp02.Client.Receive(data07, 0, 1, SocketFlags.None);
if (Encoding.UTF8.GetString(data07) == "2")
{
bytesresent = 0;
bool1 = true;
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
data06 = new byte[5];
bytesCreceived = tcp02.Client.Receive(data06, 0, 5, SocketFlags.None);
if (bytesCreceived != 5) { throw new Exception("invalid client response."); }
string01 = Encoding.UTF8.GetString(data06);
string01 = string01.Replace("x", "");
int05 = Convert.ToInt32(string01);
int06 = int02 - int05;
bytestosend -= int05;
data08 = new byte[int05];
Buffer.BlockCopy(data01, int06, data08, 0, int05);
bw1.RunWorkerAsync();
while (bytesresent == 0) { }
Console.WriteLine(" Sent " + (length - bytestosend) + " / " + length + " bytes");
Console.SetCursorPosition(0, Console.CursorTop - 1);
System.Threading.Thread.Sleep(50);
}
if (Encoding.UTF8.GetString(data07) != "2")
{
bool1 = false;
bytestosend -= bytessent;
Console.WriteLine(" Sent " + (length - bytestosend) + " / " + length + " bytes");
Console.SetCursorPosition(0, Console.CursorTop - 1);
break;
}
}
}
Console.SetCursorPosition(0, Console.CursorTop + 1);
Console.WriteLine();
System.Threading.Thread.Sleep(500);
s01.Close();
Console.WriteLine(" All data sent.");
Console.WriteLine(" Waiting for terminate message...");
tcp02.Client.Receive(new byte[1], SocketFlags.None);
Console.WriteLine(" Received terminate message.");
tcp02.Close();
Console.WriteLine(" Stopping client and server.");
tcp01.Stop();
Console.WriteLine(" Done. Press enter to continue...");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(" Error! : " + e.Message.ToString() + " - " + e.Data.ToString() + " - " + e.TargetSite.ToString());
if (!(tcp02 == null)) { tcp02.Close(); }
if (!(tcp01 == null)) { tcp01.Stop(); }
if (!(s01 == null)) { s01.Close(); }
Console.Write(" Press enter to continue..."); Console.ReadLine();
newvoid();
}
}
}
static void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
bw1.Dispose();
}
catch (Exception e1)
{
Console.WriteLine(" Error! : " + e1.Message.ToString() + " - " + e1.Data.ToString() + " - " + e1.TargetSite.ToString());
if (!(tcp02 == null)) { tcp02.Close(); }
if (!(tcp01 == null)) { tcp01.Stop(); }
if (!(s01 == null)) { s01.Close(); }
Console.Write(" Press enter to continue..."); Console.ReadLine();
newvoid();
}
}
static void bw1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
if (bool1 == false)
{
bytessent = tcp02.Client.Send(data01, 0, data01.Length, SocketFlags.None);
}
if (bool1 == true)
{
bytesresent = tcp02.Client.Send(data08, 0, data08.Length, SocketFlags.None);
}
}
catch(Exception e2)
{
Console.WriteLine(" Error! : " + e2.Message.ToString() + " - " + e2.Data.ToString() + " - " + e2.TargetSite.ToString());
if (!(tcp02 == null)) { tcp02.Close(); }
if (!(tcp01 == null)) { tcp01.Stop(); }
Console.Write(" Press enter to continue..."); Console.ReadLine();
if (!(s01 == null)) { s01.Close(); }
newvoid();
}
}
}
}

Related

How can I check to see if the user won? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to check to see if user won with a conditional?
int score1 = 0;
int score2 = 0;
int score3 = 0;
for (int i = 1; i < 11; i++)
{ //open for
if (score1 > score2 && score1 > score3) //open if
Console.WriteLine(name1 + " takes the lead in lap " + i + "!");
else if (score2 > score1 && score2 > score3)
{
Console.WriteLine(name2 + " takes the lead in lap " + i + "!");
}
else if (score3 > score2 && score3 > score1)
{
Console.WriteLine(name3 + " takes the lead in lap " + i + "!");
} //close if
} // close for
} //close if
This is the piece of code I am referring to.
So, to solve the problem, you need 3 variables to which we add the various scores at each lap, this variables are then compared before the end of the while loop. The code posted shows only the comparison for the first option. I'm sure you can figure out how to compare for the remaining options.
class Program
{
public static void Main(string[] args)
{ //open main method
string name1 = "Buck";
int endurance1 = 6;
int speed1 = 4;
string name2 = "Daisy";
int endurance2 = 4;
int speed2 = 6;
string name3 = "Leo";
int endurance3 = 7;
int speed3 = 3;
int balance = 100;
Console.WriteLine("Welcome to the racetrack!\nToday's races will include 10 laps for reach race. There will be 3 races today. \nYou have $100 you can choose to bet on one of our 3 horses.\n1. Bet on a horse. \n2. Quit the game.");
string input = Console.ReadLine();
while (input == "1")
{ //open while1
Console.WriteLine("Would you like to bet on: \n1." + name1 + ": \nEndurance:" + endurance1 + "\nSpeed:" + speed1 + "\n2." + name2 + ": \nEndurance:" + endurance2 + "\nSpeed:" + speed2 + "\n3." + name3 + ":\nEndurance:" + endurance3 + "\nSpeed:" + speed3 + "\nPlease enter the name of the horse as it appears on the screen.");
string choice = Console.ReadLine();
if (choice == "Buck")
{
Console.WriteLine("You have chosen " + name1 + ".");
}
else if (choice == "Daisy")
{
Console.WriteLine("You have chosen " + name2 + ".");
}
else
{
Console.WriteLine("You have chosen " + name3 + ".");
}
Console.WriteLine("How much would you like to bet? Your current balance is $" + balance + ".");
string money = Console.ReadLine();
int bet = int.Parse(money);
Console.WriteLine("You are betting $" + bet + " on " + choice + ". Type \'start\' to start the race.");
string race = Console.ReadLine();
int totalScore1 = 0;
int totalScore2 = 0;
int totalScore3 = 0;
if (race == "start")
{ // open if
Console.WriteLine("Let the races begin!");
for (int i = 1; i < 11; i++)
{ //open for
System.Random r = new System.Random();
int score1 = speed1 + endurance1 + r.Next(1, 8);
int score2 = speed2 + endurance2 + r.Next(1, 8);
int score3 = speed3 + endurance3 + r.Next(1, 8);
totalScore1 += score1;
totalScore2 += score2;
totalScore3 += score3;
if (score1 > score2 && score1 > score3) //open if
Console.WriteLine(name1 + " takes the lead in lap " + i + "!");
else if (score2 > score1 && score2 > score3)
{
Console.WriteLine(name2 + " takes the lead in lap " + i + "!");
}
else if (score3 > score2 && score3 > score1)
{
Console.WriteLine(name3 + " takes the lead in lap " + i + "!");
} //close if
} // close for
} //close if
else
Console.WriteLine("Okay, take your time.");
if(totalScore1> totalScore2 && totalScore1 > totalScore3 && choice =="Buck")
{
Console.WriteLine("Buckl won the race");
}
} //close while
Console.WriteLine("Aw, too bad. Thanks for joining us!");
} //close main method
}
}
Instead of solving your exact issue, I'd like to show you the power of collections (arrays and Dictionary). They allow you not to manually type every single condition for a single horse (imagine if you had 15 horses)
Since you're a novice, you probably will have hard time understanding this code, but watch and learn!
using System;
using System.Collections.Generic;
struct Horse
{
public string name;
public int endurance;
public int speed;
};
class Program {
public static void Main(string[] args)
{
var horseByName = new Dictionary<string, Horse>
{
{"Buck", new Horse{ name = "Buck", endurance = 6, speed = 4 } },
{"Daisy", new Horse{ name = "Daisy", endurance = 6, speed = 4 } },
{"Leo", new Horse{ name = "Leo", endurance = 7, speed = 3 } },
{"Maya", new Horse{ name = "Maya", endurance = 5, speed = 4 } },
{"Richard", new Horse{ name = "Richard", endurance = 5, speed = 4 } },
};
int balance = 100;
int numberOfHorses = horseByName.Count;
Console.WriteLine("Welcome to the racetrack!");
Console.WriteLine("Today's races will include 10 laps for reach race. There will be 3 races today.");
Console.WriteLine("You have $100 you can choose to bet on one of our 3 horses.");
Console.WriteLine("1. Bet on a horse.");
Console.WriteLine("2. Quit the game.");
string input = Console.ReadLine();
while (input == "1")
{
Console.WriteLine("Would you like to bet on:");
int horseNumber = 1;
foreach (var horse in horseByName.Values)
{
Console.WriteLine($"{horseNumber}. {horse.name}\nEndurance: {horse.endurance}\nSpeed: {horse.speed}");
++horseNumber;
}
string choice = Console.ReadLine();
while (!horseByName.ContainsKey(choice))
{
Console.WriteLine("We don't have such horse, try again:");
choice = Console.ReadLine();
}
Console.WriteLine($"You have chosen {choice}.");
Console.WriteLine($"How much would you like to bet? Your current balance is ${balance}.");
int bet = int.Parse(Console.ReadLine());
Console.WriteLine($"You are betting ${bet} on {choice}. Type \'start\' to start the race.");
string race = Console.ReadLine();
if (race == "start")
{
Console.WriteLine("Let the races begin!");
int[] scores = new int[numberOfHorses];
int maxScorePerLap = 0;
string maxScoredHorseName = "";
for (int lap = 1; lap < 11; lap++)
{
System.Random r = new System.Random();
int horseId = 0;
foreach (var horse in horseByName.Values)
{
scores[horseId] = horse.speed + horse.endurance + r.Next(1, 8);
if (scores[horseId] > maxScorePerLap)
{
maxScorePerLap = scores[horseId];
maxScoredHorseName = horse.name;
}
++horseId;
}
Console.WriteLine($"{maxScoredHorseName} takes the lead in lap {lap}!");
}
if (maxScoredHorseName == choice)
{
Console.WriteLine($"Hurray! {choice} won!");
balance += bet;
}
else
{
Console.WriteLine($"Unfortunately {maxScoredHorseName} won, but you had bet on {choice}...");
balance -= bet;
}
}
else
{
Console.WriteLine("Okay, take your time.");
}
}
Console.WriteLine("Aw, too bad. Thanks for joining us!");
}
}

c# dice game implementation

I'm stuck on how I can implement the code so when it compiles to one counter it displays one counter not 'counters' as a plural for only one counter. I need help on how I can implement that piece of code in to my code that I have given below.
namespace ReferralDG
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Even Minus Odd Game: ");
Random rand = new Random();
bool PlayAgain = false;
do
{
int numberOfDice = 0;
int totalOfDiceRolled = 0;
while (numberOfDice > 10 || numberOfDice < 3)
{
Console.WriteLine("How many dice do you wish to play with? (Between 3 and 10?)");
numberOfDice = Convert.ToInt32(Console.ReadLine());
if (numberOfDice > 10 || numberOfDice < 3)
{
Console.WriteLine("Please enter the correct number
}
}
Console.Write("Dice rolls: ");
int evenTotal = 0;
int oddTotal = 0;
for (int i = 0; i < numberOfDice; i++)
{
int diceRoll = rand.Next(1, 7);
if (diceRoll % 2 == 0)
{
evenTotal += diceRoll;
}
else
{
oddTotal += diceRoll;
}
totalOfDiceRolled += diceRoll;
Console.Write(diceRoll + " ");
}
int counters = evenTotal - oddTotal;
Console.WriteLine();
if (counters > 0)
{
Console.WriteLine("You take " + counters + " counters.");
}
else if (counters < 0)
{
Console.WriteLine("You give " + Math.Abs(counters) + " counters.");
}
else if (evenTotal == oddTotal)
{
Console.WriteLine("Even total is equal to odd total");
}
else
{
Console.WriteLine("No counters this game");
}
Console.WriteLine("Would you like to play again? (Y/N): ");
string playAgain = Console.ReadLine().ToUpper();
if (playAgain == "Y")
{
PlayAgain = true;
}
else
{
PlayAgain = false;
}
} while (PlayAgain);
}
}
}
you could use the if shorthand statement ? valueIfTrue : valueIfFalse; to set the right string values. and put everything in a function like that
private static void printCounters(int counters,bool take) {
counters = Math.Abs(counters);
string msg1 = take ? "You take " : "You give ";
string msg2 = counters == 1 ? " counter." : " counters.";
Console.WriteLine( msg1 + counters + msg2);
}
this has the advantage that you can easily call printCounters(counters,true) if you take and printCounters(counters,give) if you give
You can just add additional if's to your block.
if (counters > 1) {
Console.WriteLine("You take " + counters + " counters.");
}
else if (counters > 0) // or if you rather counters == 1
{
Console.WriteLine("You take " + counters + " counter.");
}
else if (counters < -1)
{
Console.WriteLine("You give " + Math.Abs(counters) + " counters.");
}
else if (counters < 0)
{
Console.WriteLine("You give " + Math.Abs(counters) + " counter.");
}
else if (evenTotal == oddTotal)
{
Console.WriteLine("Even total is equal to odd total");
}
else
{
Console.WriteLine("No counters this game");
}
Another "simpler" solution is to just output it as counter(s)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
namespace ReferralDG
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Even Minus Odd Game: ");
Random rand = new Random();
bool PlayAgain = false;
do
{
int numberOfDice = 0;
int totalOfDiceRolled = 0;
while (numberOfDice > 10 || numberOfDice < 3)
{
Console.WriteLine("How many dice do you wish to play with? (Between 3 and 10?)");
numberOfDice = Convert.ToInt32(Console.ReadLine());
if (numberOfDice > 10 || numberOfDice < 3)
{
Console.WriteLine("Please enter the correct number");
}
}
Console.Write("Dice rolls: ");
int evenTotal = 0;
int oddTotal = 0;
for (int i = 0; i < numberOfDice; i++)
{
int diceRoll = rand.Next(1, 7);
if (diceRoll % 2 == 0)
{
evenTotal += diceRoll;
}
else
{
oddTotal += diceRoll;
}
totalOfDiceRolled += diceRoll;
Console.Write(diceRoll + " ");
}
int counters = evenTotal - oddTotal;
Console.WriteLine();
if (counters > 1)
{
Console.WriteLine("You take " + counters + " counters.");
}
else if (counters > 0) // or if you rather counters == 1
{
Console.WriteLine("You take " + counters + " counter.");
}
else if (counters < -1)
{
Console.WriteLine("You give " + Math.Abs(counters) + " counters.");
}
else if (counters < 0)
{
Console.WriteLine("You give " + Math.Abs(counters) + " counter.");
}
else if (evenTotal == oddTotal)
{
Console.WriteLine("Even total is equal to odd total");
}
else
{
Console.WriteLine("No counters this game");
}
Console.WriteLine("Would you like to play again? (Y/N): ");
string playAgain = Console.ReadLine().ToUpper();
if (playAgain == "Y")
{
PlayAgain = true;
}
else
{
PlayAgain = false;
}
} while (PlayAgain);
}
}
}
}
Hello and welcome to StackOverflow Sam!
So when you write the counters to the screen, you want it to write singular if their is a single counter, and plural if there are more than 1? I also assume we want this to work no matter if the counter is positive or negative.
Instead of adding a couple more branches to your if statement, we could use string.Format() and an Inline If statement.
In this case, it would look something like this:
// Console.WriteLine("You take " + counters + " counters.");
Console.WriteLine(string.Format("You take {0} counter{1}.", counters, Math.Abs(counters) == 1 ? "" : "s"));
This is really just a way of writing multiple "else ifs", as you still have added a branching statement. My way, it is in-line instead of taking up more space vertically.
If you wanted to get even more fancy, you could use an IIF to set the "give" or "take" as well:
if (counters != 0)
{
Console.WriteLine(string.Format("You {0} {1} counter{2}.",
counters > 0 ? "take" : "give",
counters,
Math.Abs(counters) == 1 ? "" : "s"
));
}
else if (evenTotal == oddTotal)
{
Console.WriteLine("Even total is equal to odd total");
}
else
{
Console.WriteLine("No counters this game");
}
Read up on the links, see what works best for you!

C# How to make an open while loop that stops when user wants

I'm writing a small loop in C# that I want to stay open until the user specifies.
public void ScoreCalc()
{
string goon = " ";
int counter = 1;
int score = 0;
while (goon == " ")
{
Console.WriteLine("Enter a score");
score += int.Parse(Console.ReadLine());
Console.WriteLine(score + " " + counter);
counter++;
}
}
I know this code is not correct.
One way would be to set goon to something other than " " if anything other than an integer is entered by the user.
The easiest way to check if an integer has been entered is by using the Int32.TryParse method.
public void ScoreCalc()
{
string goon = " ";
int counter = 1;
int score = 0;
int userInput = 0;
bool isInt = true;
while (goon == " ")
{
Console.WriteLine("Enter a score");
isInt = Int32.TryParse(Console.ReadLine(), out userInput);
if(isInt)
{
score += userInput;
Console.WriteLine(score + " " + counter);
counter++;
}
else
{
goon = "exit";
}
}
}
public void ScoreCalc()
{
int counter = 1;
int score = 0;
String input;
while (true)
{
Console.WriteLine("Enter a score");
input=Console.ReadLine();
if(input != "end"){
score += int.Parse(input);
Console.WriteLine(score + " " + counter);
counter++;
}else{
break;
}
}
}
I've updated your method assuming "quit" text as an exit signal from the user to break the while loop. Hope this helps!
public void ScoreCalc()
{
string goon = " ";
int counter = 1;
int score = 0;
var userInput = string.Empty;
var inputNumber = 0;
const string exitValue = "quit";
while (goon == " ")
{
Console.WriteLine("Enter a score or type quit to exit.");
userInput = Console.ReadLine();
if (userInput.ToLower() == exitValue)
{
break;
}
score += int.TryParse(userInput, out inputNumber) ? inputNumber : 0;
Console.WriteLine(score + " " + counter);
counter++;
}
}

C# - Sending a WebSocket Message to the client

I followed the following tutorial to setup a websocket server in C#:
https://developer.mozilla.org/pt-BR/docs/WebSockets/Writing_WebSocket_server
After this, i wrote a part where it would decode a normal message and display it to me in a console.
Now, i want to be able to send this message back to the client (exactly what ws://echo.websocket.org does)
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace sockecho
{
class Program
{
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
server.Start();
Console.WriteLine("Server has started on 127.0.0.1:80.{0}Waiting for a connection...", Environment.NewLine);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream();
//enter to an infinite cycle to be able to handle every change in stream
while (true)
{
while (!stream.DataAvailable) ;
Byte[] bytes = new Byte[client.Available];
stream.Read(bytes, 0, bytes.Length);
//translate bytes of request to string
String data = Encoding.UTF8.GetString(bytes);
if (new Regex("^GET").IsMatch(data))
{
#region startdata
Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine + "Upgrade: websocket" + Environment.NewLine + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
SHA1.Create().ComputeHash(
Encoding.UTF8.GetBytes(
new Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
)
)
) + Environment.NewLine
+ Environment.NewLine);
stream.Write(response, 0, response.Length);
string responsel = Encoding.UTF8.GetString(response);
Console.WriteLine(responsel);
#endregion
}
else
{
//stream.Write(bytes, 0, bytes.Length);
string decodedmessage = GetMessage(bytes);
Console.WriteLine(decodedmessage);
byte[] toBytes = Encoding.ASCII.GetBytes(decodedmessage);
stream.Write(toBytes, 0, toBytes.Length);
}
}
}
private static string GetMessage (Byte[] bytes)
{
string DETEXT = "";
if (bytes[0] == 129)
{
int position = 0;
int Type = 0;
ulong length = 0;
if (bytes[1] - 128 >= 0 && bytes[1] - 128 <= 125)
{
length = (ulong)bytes[1] - 128;
position = 2;
}
else if (bytes[1] - 128 == 126)
{
Type = 1;
length = (ulong)256 * bytes[2] + bytes[3];
position = 4;
}
else if (bytes[1] - 128 == 127)
{
Type = 2;
for (int i = 0; i < 8; i++)
{
ulong pow = Convert.ToUInt64(Math.Pow(256, (7 - i)));
length = length + bytes[2 + i] * pow;
position = 10;
}
}
else
{
Type = 3;
Console.WriteLine("error 1");
}
if (Type < 3)
{
Byte[] key = new Byte[4] { bytes[position], bytes[position + 1], bytes[position + 2], bytes[position + 3] };
Byte[] decoded = new Byte[bytes.Length - (4 + position)];
Byte[] encoded = new Byte[bytes.Length - (4 + position)];
for (long i = 0; i < bytes.Length - (4 + position); i++) encoded[i] = bytes[i + position + 4];
for (int i = 0; i < encoded.Length; i++) decoded[i] = (Byte)(encoded[i] ^ key[i % 4]);
DETEXT = Encoding.UTF8.GetString(decoded);
}
}
else
{
Console.WriteLine("error 2: " + bytes[0].ToString());
}
return DETEXT;
}
}
}

I can start threads with switches but can't end them

I want to start a number of threads based on the number of threads the computer running the program will have.
I tried doing switches but it seems that it can't end the threads.
This is my Main thread and doesn't work, it says the threads are out of context on the 2nd switch
Is there anything I can add or should I use a different method altogether?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Security.Principal;
namespace Yahtzee_DataMine
{
class Program
{
public static Random diceValue = new Random();
public static int numberOfDie = new int();
public static int numberOfSides = new int();
private static System.Object lockThis = new System.Object();
public static decimal percent = new decimal();
public static ConsoleColor oldColor = Console.ForegroundColor;
static void Main(string[] args)
{
while (true)
{
getInfo();
int processorCount = Environment.ProcessorCount;
Console.WriteLine(processorCount);
if (processorCount > 7) { processorCount = 7; }
switch (processorCount)
{
case 7:
Thread Rolls6 = new Thread(Rolling2);
Rolls6.Start();
goto case 6;
case 6:
Thread Rolls5 = new Thread(Rolling3);
Rolls5.Start();
goto case 5;
case 5:
Thread Rolls4 = new Thread(Rolling4);
Rolls4.Start();
goto case 4;
case 4:
Thread Rolls3 = new Thread(Rolling5);
Rolls3.Start();
goto case 3;
case 3:
Thread Rolls2 = new Thread(Rolling6);
Rolls2.Start();
goto case 1;
case 2:
case 1:
Thread Rolls1 = new Thread(Rolling7);
Rolls1.Start();
break;
}
while (true)
{
char quit = quit = Console.ReadKey().KeyChar;
if (quit == 'q')
{
Console.WriteLine("\rTerminated");
break;
}
}
switch (processorCount)
{
case 7:
Rolls6.abort();
goto case 6;
case 6:
Rolls5.abort();
goto case 5;
case 5:
Rolls4.abort();
goto case 4;
case 4:
Rolls3.abort();
goto case 3;
case 3:
Rolls2.abort();
goto case 1;
case 2:
case 1:
Rolls1.abort();
break;
}
}
}
public static void getInfo()
{
if (WindowsIdentity.GetCurrent().Owner.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) == false)
{
Console.WriteLine("You Are Not Running With Elevated Administrative Access.");
Console.WriteLine("Please Restart And Run Program With Administrative Access");
}
#region gettingNumberOfDie
while (true)
{
Console.WriteLine("How Many Die Would You Like To Roll? Type Q To Quit");
String howManyDie = Console.ReadLine();
try
{
int.TryParse(howManyDie, out numberOfDie);
if (numberOfDie < 2)
{
Console.WriteLine("Please Enter A Integer Greater Than 1");
continue;
}
break;
}
catch
{
Console.WriteLine("Please Enter A Number Or Press Q To Quit");
continue;
}
}
#endregion
#region gettingNumberOfSides
while (true)
{
Console.WriteLine("How Many Sides Do You Want Each Die To Have?");
String howManySides = Console.ReadLine();
try
{
int.TryParse(howManySides, out numberOfSides);
if (numberOfSides < 2)
{
Console.WriteLine("Please Enter A Integer Greater Than 1");
continue;
}
break;
}
catch
{
Console.WriteLine("Please Enter A Number Or Press Q To Quit");
continue;
}
}
#endregion
#region gettingPercent
int percentCounter = 1;
percent = (1m / (numberOfSides));
decimal percentMultiplier = percent;
while (percentCounter < numberOfDie)
{
percent = percent * percentMultiplier;
percentCounter++;
}
percent = percent * 100;
#endregion
Console.WriteLine("With " + numberOfDie + ", " + numberOfSides + " Sided Die, You Have An " + percent + '%' + " Chance of Getting A Yahtzee With Any Given Roll");
Console.WriteLine("Press Any Key To Commence");
Console.ReadKey();
}
static int seed = Environment.TickCount;
static readonly ThreadLocal<Random> random = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));
public static int Rand()
{
return random.Value.Next(numberOfSides);
}
private static void Rolling1()
{
Console.WriteLine("Thread1 Started");
while (true)
{
#region rollS
Stopwatch rollTime = new Stopwatch();
rollTime.Start();
UInt64 numberOfRolls = 0;
while (true)
{
numberOfRolls++;
int counter = 0;
int[] valuesOfRoll = new int[numberOfDie];
#region Roll
while (counter < numberOfDie)
{
valuesOfRoll.SetValue((Rand() + 1), counter);
counter++;
}
#region isItYahtzee?
Boolean isItYahtzee = true;
int counterYaht = 1;
while (counterYaht < numberOfDie)
{
if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
{
isItYahtzee = false;
counterYaht++;
break;
}
else
{
counterYaht++;
continue;
}
}
if ((numberOfRolls % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread1 has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
#endregion
#region ifYahtzee
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = (numberOfRolls).ToString();
lock (lockThis)
{
System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
fileA.WriteLine(linesA);
fileA.Close();
System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
fileB.WriteLine(linesB);
fileB.Close();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 1");
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
#endregion
#endregion
#endregion
}
}
private static void Rolling2()
{
Console.WriteLine("Thread2 Started");
while (true)
{
#region rollS
Stopwatch rollTime = new Stopwatch();
rollTime.Start();
UInt64 numberOfRolls = 0;
while (true)
{
numberOfRolls++;
int counter = 0;
int[] valuesOfRoll = new int[numberOfDie];
#region Roll
while (counter < numberOfDie)
{
valuesOfRoll.SetValue((Rand() + 1), counter);
counter++;
}
#region isItYahtzee?
Boolean isItYahtzee = true;
int counterYaht = 1;
while (counterYaht < numberOfDie)
{
if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
{
isItYahtzee = false;
counterYaht++;
break;
}
else
{
counterYaht++;
continue;
}
}
if ((numberOfRolls % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread2 has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
#endregion
#region ifYahtzee
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = numberOfRolls.ToString();
lock (lockThis)
{
System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
fileA.WriteLine(linesA);
fileA.Close();
System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
fileB.WriteLine(linesB);
fileB.Close();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 2");
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
#endregion
#endregion
#endregion
}
}
private static void Rolling3()
{
Console.WriteLine("Thread3 Started");
while (true)
{
#region rollS
Stopwatch rollTime = new Stopwatch();
rollTime.Start();
UInt64 numberOfRolls = 0;
while (true)
{
numberOfRolls++;
int counter = 0;
int[] valuesOfRoll3 = new int[numberOfDie];
#region Roll
while (counter < numberOfDie)
{
valuesOfRoll3.SetValue((Rand() + 1), counter);
counter++;
}
#region isItYahtzee?
Boolean isItYahtzee = true;
int counterYaht = 1;
while (counterYaht < numberOfDie)
{
if (valuesOfRoll3[counterYaht] != valuesOfRoll3[0])
{
isItYahtzee = false;
counterYaht++;
break;
}
else
{
counterYaht++;
continue;
}
}
#endregion
if ((numberOfRolls % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread3 has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
#region ifYahtzee
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = numberOfRolls.ToString();
lock (lockThis)
{
System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
fileA.WriteLine(linesA);
fileA.Close();
System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
fileB.WriteLine(linesB);
fileB.Close();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 3");
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
#endregion
#endregion
#endregion
}
}
private static void Rolling4()
{
Console.WriteLine("Thread4 Started");
while (true)
{
#region rollS
Stopwatch rollTime = new Stopwatch();
rollTime.Start();
UInt64 numberOfRolls = 0;
while (true)
{
numberOfRolls++;
int counter = 0;
int[] valuesOfRoll = new int[numberOfDie];
#region Roll
while (counter < numberOfDie)
{
valuesOfRoll.SetValue((Rand() + 1), counter);
counter++;
}
#region isItYahtzee?
Boolean isItYahtzee = true;
int counterYaht = 1;
while (counterYaht < numberOfDie)
{
if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
{
isItYahtzee = false;
counterYaht++;
break;
}
else
{
counterYaht++;
continue;
}
}
#endregion
if ((numberOfRolls % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread4 has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
#region ifYahtzee
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = numberOfRolls.ToString();
lock (lockThis)
{
System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
fileA.WriteLine(linesA);
fileA.Close();
System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
fileB.WriteLine(linesB);
fileB.Close();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 4");
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
#endregion
#endregion
#endregion
}
}
private static void Rolling5()
{
Console.WriteLine("Thread5 Started");
while (true)
{
#region rollS
Stopwatch rollTime = new Stopwatch();
rollTime.Start();
UInt64 numberOfRolls = 0;
while (true)
{
numberOfRolls++;
int counter = 0;
int[] valuesOfRoll = new int[numberOfDie];
#region Roll
while (counter < numberOfDie)
{
valuesOfRoll.SetValue((Rand() + 1), counter);
counter++;
}
#region isItYahtzee?
Boolean isItYahtzee = true;
int counterYaht = 1;
while (counterYaht < numberOfDie)
{
if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
{
isItYahtzee = false;
counterYaht++;
break;
}
else
{
counterYaht++;
continue;
}
}
#endregion
if ((numberOfRolls % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread5 has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
#region ifYahtzee
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = numberOfRolls.ToString();
lock (lockThis)
{
System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
fileA.WriteLine(linesA);
fileA.Close();
System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
fileB.WriteLine(linesB);
fileB.Close();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 5");
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
#endregion
#endregion
#endregion
}
}
private static void Rolling6()
{
Console.WriteLine("Thread6 Started");
while (true)
{
#region rollS
Stopwatch rollTime = new Stopwatch();
rollTime.Start();
UInt64 numberOfRolls = 0;
while (true)
{
numberOfRolls++;
int counter = 0;
int[] valuesOfRoll = new int[numberOfDie];
#region Roll
while (counter < numberOfDie)
{
valuesOfRoll.SetValue((Rand() + 1), counter);
counter++;
}
#region isItYahtzee?
Boolean isItYahtzee = true;
int counterYaht = 1;
while (counterYaht < numberOfDie)
{
if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
{
isItYahtzee = false;
counterYaht++;
break;
}
else
{
counterYaht++;
continue;
}
}
#endregion
if ((numberOfRolls % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread6 has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
#region ifYahtzee
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = numberOfRolls.ToString();
lock (lockThis)
{
System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
fileA.WriteLine(linesA);
fileA.Close();
System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
fileB.WriteLine(linesB);
fileB.Close();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 6");
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
#endregion
#endregion
#endregion
}
}
It really looks like you need to learn some more of the basics of C# before you try something this complicated. You've got an interesting mix of really clever and very basic code. It appears that you've copy-and-pasted a lot from elsewhere.
The basic issue you're having is that you're declaring your thread variables (Thread Rolls6 = new Thread(Rolling2);) within the scope of the first switch (processorCount) statement. When it comes to the second one those variables don't exist. You could simply move the variable declarations higher up in the method to make them visible to both, but that's going to be a mistake. Calling Thread.Abort is a very bad practice. You really need something that lets the thread terminate normally.
In a nutshell your threads should look to see when they should end and they should respond accordingly.
The basic way this code looks is this:
private static void Rolling(CancellationToken ct)
{
while (true)
{
if (ct.IsCancellationRequested)
{
Console.WriteLine("Done with thread " + n);
break;
}
/* Do Stuff Here, But Let The Code Loop Back */
}
}
The code keeps checking the CancellationToken to see if IsCancellationRequested has been set or not.
That's how to end a thread cleanly.
Now I have gotten rid of all of those Rolling{n} methods and replaced them with a single method with the signature void Rolling(int n, int numberOfDie, int numberOfSides, double percent, CancellationToken ct). I've tried to get rid of global variables (which are generally bad) and I'm following the CancellationToken pattern.
To start up the threads I've gotten of the switch statements. The thread creation now looks like this:
CancellationTokenSource[] ctss =
Enumerable
.Range(1, processorCount)
.Select(n =>
{
var cts = new CancellationTokenSource();
var t = new Thread(() =>
Rolling(n, numberOfDie, numberOfSides, percent, cts.Token));
t.Start();
return cts;
})
.ToArray();
To cancel all of the threads the code just becomes:
foreach (var cts in ctss)
{
cts.Cancel();
}
...and then the ct.IsCancellationRequested becomes true in the Rolling method and they will shut down themselves.
That's the approach that you should be taking.
Here's your full code:
private static System.Object lockThis = new System.Object();
public static ConsoleColor oldColor = Console.ForegroundColor;
private static int seed = Environment.TickCount;
private static readonly ThreadLocal<Random> random = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));
static void Main(string[] args)
{
var x = GetValue("How Many Die Would You Like To Roll?", 1);
if (x.HasValue)
{
var y = GetValue("How Many Sides Do You Want Each Die To Have?", 2);
if (y.HasValue)
{
int numberOfDie = x.Value;
int numberOfSides = y.Value;
double percent = 100 * Math.Pow(1.0 / numberOfSides, numberOfDie);
Console.WriteLine("With " + numberOfDie + ", " + numberOfSides + " Sided Die, You Have An " + percent + '%' + " Chance of Getting A Yahtzee With Any Given Roll");
Console.WriteLine("Press Any Key To Commence");
Console.ReadLine();
int processorCount = System.Math.Min(Environment.ProcessorCount, 8);
Console.WriteLine(processorCount);
CancellationTokenSource[] ctss =
Enumerable
.Range(1, processorCount)
.Select(n =>
{
var cts = new CancellationTokenSource();
var t = new Thread(() =>
Rolling(n, numberOfDie, numberOfSides, percent, cts.Token));
t.Start();
return cts;
})
.ToArray();
while (true)
{
string quit = Console.ReadLine().Substring(0, 1).ToUpper();
if (quit == "Q")
{
Console.WriteLine(Environment.NewLine, "Terminated");
break;
}
}
foreach (var cts in ctss)
{
cts.Cancel();
}
}
}
}
private static int? GetValue(string prompt, int minimum)
{
while (true)
{
Console.WriteLine(prompt + " Type Q To Quit");
var input = Console.ReadLine().Substring(0, 1).ToUpper();
if (input == "Q")
{
return null;
}
int output;
if (int.TryParse(input, out output))
{
if (output < minimum)
{
Console.WriteLine("Please Enter an Integer Greater Than Or Equal To " + minimum);
continue;
}
else
{
return output;
}
}
}
}
private static void Rolling(int n, int numberOfDie, int numberOfSides, double percent, CancellationToken ct)
{
Console.WriteLine("Thread" + n + " Started");
Stopwatch rollTime = Stopwatch.StartNew();
long numberOfRolls = 0;
while (true)
{
if (ct.IsCancellationRequested)
{
Console.WriteLine("Done with thread " + n);
break;
}
int[] valuesOfRoll =
Enumerable
.Range(0, numberOfDie)
.Select(x => random.Value.Next(numberOfSides) + 1)
.ToArray();
Boolean isItYahtzee = valuesOfRoll.All(x => x == valuesOfRoll[0]);
if ((numberOfRolls++ % 100000000) == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thread" + n + " has rolled " + numberOfRolls);
Console.ForegroundColor = oldColor;
}
if (isItYahtzee == true)
{
rollTime.Stop();
string time = rollTime.Elapsed.ToString();
string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
string linesA =
String.Format(
"{0},{1},{2},{3},{4}%,{5},{6},{7},{8}",
numberOfDie,
numberOfSides,
numberOfDie * numberOfSides,
numberOfRolls,
percent,
percent * numberOfRolls,
time,
timeSec,
numberOfRolls / rollTime.Elapsed.TotalSeconds);
string linesB = (numberOfRolls).ToString();
lock (lockThis)
{
File.AppendAllLines(Directory.GetCurrentDirectory() + "\\All.txt", new[] { linesA });
File.AppendAllLines(Directory.GetCurrentDirectory() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", new[] { linesB });
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread " + n);
Console.ForegroundColor = oldColor;
numberOfRolls = 0;
break;
}
}
}

Categories

Resources