Why Does Threading Break Named Pipes During Inter-Processing Communications? - c#

I am trying to write a sample C# program for named pipe streaming. I create two executable files, one for server and another for client. Please check my code below. These two applications sometimes work correctly and sometimes client application closes without any reason or exception, resulting Pipe is broken exception in server application. I think, in this situation, these processes are not synchronized, specially when I put some delays in their code such as debug mode delays. Why is client process being suddenly closed when server is still waiting for PipeDrain (pipeServer.WaitForPipeDrain())?
I appreciate your help.
Server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Pipes;
using System.IO;
using System.Threading;
namespace NamedPipeServer
{
class NamedPipeServerClass
{
static void Main(string[] args)
{
NamedPipeServerStream pipeServer = new NamedPipeServerStream("namedPipe", PipeDirection.InOut, 4);
StreamReader SR = new StreamReader(pipeServer);
StreamWriter SW = new StreamWriter(pipeServer);
pipeServer.WaitForConnection();
string temp;
try
{
SW.WriteLine("Waiting");
SW.Flush();
pipeServer.WaitForPipeDrain();
temp = SR.ReadLine();
Console.WriteLine(temp + '\n');
SW.WriteLine("Hello");
SW.Flush();
pipeServer.WaitForPipeDrain();
temp = SR.ReadLine();
Console.WriteLine(temp + '\n');
SW.WriteLine("How are you?");
SW.Flush();
pipeServer.WaitForPipeDrain();
temp = SR.ReadLine();
Console.WriteLine(temp + '\n');
SW.WriteLine("GoodBye");
SW.Flush();
pipeServer.WaitForPipeDrain();
temp = SR.ReadLine();
Console.WriteLine(temp + '\n');
}
catch (Exception ex)
{
throw ex;
}
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected)
{
pipeServer.Disconnect();
}
}
Console.WriteLine("Server: Press any key to exit...");
Console.ReadLine();
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Pipes;
using System.IO;
using System.Threading;
namespace NamedPipeClient
{
class NamedPipeClientClass
{
static NamedPipeClientStream pipeClient;
static StreamReader SR;
static StreamWriter SW;
static Thread Conversation = new Thread(new ThreadStart(Con_function));
static bool HandShake_Ok = false;
static void Main(string[] args)
{
pipeClient = new NamedPipeClientStream(".", "namedPipe", PipeDirection.InOut, PipeOptions.None);
if (pipeClient.IsConnected != true)
{
pipeClient.Connect();
}
SR = new StreamReader(pipeClient);
SW = new StreamWriter(pipeClient);
Conversation.Name = "ConversationThread";
Conversation.IsBackground = true;
Conversation.Start();
}
static void Con_function()
{
string temp;
while (true)
{
try
{
temp = SR.ReadLine();
}
catch (Exception ex)
{
throw ex;
}
if (temp == "Waiting")
{
HandShake_Ok = true;
try
{
SW.WriteLine("Welcome");
SW.Flush();
}
catch (Exception ex)
{
throw ex;
}
}
else if (HandShake_Ok && temp == "Hello")
{
try
{
SW.WriteLine("Hi");
SW.Flush();
}
catch (Exception ex)
{
throw ex;
}
}
else if (HandShake_Ok && temp == "How are you?")
{
try
{
SW.WriteLine("I am fine");
SW.Flush();
}
catch (Exception ex)
{
throw ex;
}
}
else if (HandShake_Ok && temp == "GoodBye")
{
try
{
HandShake_Ok = false;
SW.WriteLine("Good bye :)");
SW.Flush();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
}
Update: In the client the writings and readings into pipe are done in a thread, called Conversation. If I remove this thread and bring all writings and readings into Main function, the problem will be resolved. Why does thread have such an effect on inter-process pipeline communications?

I believe what you are experiencing here is since Conversation.IsBackground = true; the client application will NOT wait for the Conversation thread to complete before exiting
Try:
Conversation.Name = "ConversationThread";
Conversation.IsBackground = true;
Conversation.Start();
Conversation.Join();
The Conversation.Join(); blocks the calling thread from executing until the thread object terminates
See Thread.Join Method & Thread.IsBackground Property on MSDN

Related

If I code a whois server program what functionalities must I supply?

If I code a WHOIS server program, say using languages like C# and Java, what functionalities must I supply?
What I understand is, WHOIS servers listen to the port no. 43. When they receive a command "whois", they respond with various information pertaining to the server itself.
So, it's just a normal TCP/IP server that listens to a special port and serves a specific purpose.
Am I correct?
Whois Client:
using System;
using System.Net.Sockets;
using System.IO;
public class Whois
{
static void Main(string[] args)
{
try
{
TcpClient client = new TcpClient();
client.Connect("localHost", 43);
if (args.Length < 1)
{
Console.WriteLine("Provide more than one Args");
}
else if (args.Length == 1)
{
StreamWriter sw = new StreamWriter(client.GetStream());
StreamReader sr = new StreamReader(client.GetStream());
sw.WriteLine(args[0]);
sw.Flush();
Console.WriteLine(args[0] + " is "+ sr.ReadToEnd());
return;
}
else if (args.Length > 1)
{
StreamWriter sw = new StreamWriter(client.GetStream());
StreamReader sr = new StreamReader(client.GetStream());
string str = "";
foreach (string arg in args)
{
str += arg + " ";
}
sw.WriteLine(str);
sw.Flush();
Console.WriteLine(args[0] + " location changed to be " + args[1]);
sw.Close();
return;
}
else Console.WriteLine("Invalid args ");
return;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Whois Server:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
namespace locationserver
{
public class respond
{
static Dictionary<string, string> SavedLocationa = new Dictionary<String, String>();
static void Main(string[] args)
{
runServer();
}
static void runServer()
{
TcpListener listener;
Socket connection;
NetworkStream socketStream;
try
{
listener = new TcpListener(IPAddress.Any, 43);
listener.Start();
Console.WriteLine("server started listening");
while (true)
{
connection = listener.AcceptSocket();
socketStream = new NetworkStream(connection);
Console.WriteLine("Connection Received");
doRequest(socketStream);
socketStream.Close();
connection.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Exception:" + e.ToString());
}
}
static void doRequest(NetworkStream socketStream)
{
try
{
StreamWriter sw = new StreamWriter(socketStream);
StreamReader sr = new StreamReader(socketStream);
String line = sr.ReadLine();
Console.WriteLine("Respond Received:" + line);
String[] sections = line.Split(new char[] { ' ' }, 2);
String names, location;
if (line.Contains("-h0"))
{
Console.WriteLine(" Is ok");
}
else if (line.Contains("-h9"))
{
Console.WriteLine("Hey you're progressing");
}
else if (line.Contains("-h1"))
{
Console.WriteLine("We're Done");
}
if (sections.Length < 1)
{
Console.WriteLine("Too little words was inputted");
}
else if (sections.Length == 1)
{
names = sections[0];
if (SavedLocationa.ContainsKey(names))
{
sw.WriteLine(SavedLocationa[names]);
}
else
{
sw.WriteLine("Error no entries found");
}
sw.Flush();
Console.WriteLine("Error no entries found");
}
else if (sections.Length == 2)
{
names = sections[0];
location = sections[1];
if (SavedLocationa.ContainsKey(names))
{
SavedLocationa.Remove(names);
}
else
{
SavedLocationa.Add(names, location);
sw.WriteLine("Update Successful");
}
sw.Flush();
}
}
catch (Exception e)
{
Console.WriteLine("Something went wrong");
Console.WriteLine(e.Message);
}
}
}
}
What functionalities are missing in the server portion of the code?

MS Cognitive Face API: Unable to detect faces

I am trying to use Microsoft Cognitive Face API for the first time. Documentation gives quite a simple method to detect face from memory stream. I am trying to detect faces from images located inside a folder. Right now there is only one image inside the folder. The issue is whenever the control reaches the following line:
var faces = await faceServiceClient.DetectAsync(memStream, true, true);
it terminates without any exception or error. Here is the complete code I have written.
using Microsoft.ProjectOxford.Face;
using Microsoft.ProjectOxford.Common;
using Microsoft.ProjectOxford.Face.Contract;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace FaceDetection.FaceDetect
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Face Detect";
Start();
}
static async Task Stop()
{
await Close();
}
private static Task Close()
{
return Task.Run(() =>
{
Environment.Exit(0);
});
}
static async Task ReStart(string _reason = "")
{
Console.WriteLine(_reason + "To restart the process press 'R'. To exit press 'X'");
var _response = Console.ReadLine();
if (_response == "r" || _response == "R")
await Start();
else
await Stop();
}
static async Task Start()
{
Console.Clear();
Console.WriteLine("Enter Folder Path");
string imageFolderPath = Console.ReadLine();
if (!Directory.Exists(imageFolderPath))
{
await ReStart("Folder does not exist! ");
}
else
{
await SaveFiles(imageFolderPath);
}
}
static async Task SaveFiles(string imageFolderPath)
{
try
{
DirectoryInfo dInfo = new DirectoryInfo(imageFolderPath);
string[] extensions = new[] { ".jpg", ".jpeg" };
FileInfo[] files = dInfo.GetFiles()
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
if (files.Length == 0)
await ReStart("No files found in the specified folder! ");
else
{
string subscriptionKey = "ADSFASDFASDFASDFASDFASDFASDF";
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["subscriptionKey"]))
subscriptionKey = ConfigurationManager.AppSettings["subscriptionKey"].ToString();
//var stringFaceAttributeType = new List<FaceAttributeType> { FaceAttributeType.Smile, FaceAttributeType.Glasses, FaceAttributeType.Gender, FaceAttributeType.Age };
//IEnumerable<FaceAttributeType> returnFaceAttributes = stringFaceAttributeType;
IFaceServiceClient faceServiceClient = new FaceServiceClient(subscriptionKey);
foreach (FileInfo file in files)
{
try
{
using (FileStream fileStream = File.OpenRead(imageFolderPath + "\\" + file.Name))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
//Used following commented code to make sure MemoryStream is not corrupted.
//FileStream _file = new FileStream(imageFolderPath + "\\test.jpg", FileMode.Create, FileAccess.Write);
//memStream.WriteTo(_file);
//_file.Close();
//memStream.Close();
try
{
//This line never returns a result. The execution terminates without any exception/error.
var faces = await faceServiceClient.DetectAsync(memStream, true, true);
if (faces != null)
{
foreach (var face in faces)
{
var rect = face.FaceRectangle;
var landmarks = face.FaceLandmarks;
}
}
else
Console.WriteLine("No face found in image: " + file.FullName);
}
catch (Exception ex)
{
Console.WriteLine("Error");
}
}
}
catch (Exception ex)
{
Console.WriteLine("There was an error!");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("There was an error!");
}
await ReStart();
}
}
}
Can someone point out what am I missing. Why is this code not working?
When you read the file in to the MemoryStream, your read pointer is advanced to the end. So memStream passed in to DetectAsync() appears empty. The fact is you need not copy your file to memory. You could simply pass in the FileStream after opening.
using (FileStream fileStream = File.OpenRead(imageFolderPath + "\\" + file.Name))
{
try
{
var faces = await faceServiceClient.DetectAsync(fileStream, true, true);
if (faces != null)
{
foreach (var face in faces)
{
var rect = face.FaceRectangle;
var landmarks = face.FaceLandmarks;
}
}
else
{
Console.WriteLine("No face found in image: " + file.FullName);
}
catch (Exception ex)
{
Console.WriteLine("Error");
}
}
Alternatively, you can rewind the memory stream by setting memStream.Position = 0 before calling DetectAsync.

C# Read Write TextFile End In The Middle

I run a Method, there're three part, part 1 and 3 are all the same to "read text file",
and part2 is to save string to text file,
// The Save Path is the text file's Path, used to read and save
// Encode can use Encoding.Default
public static async void SaveTextFile(string StrToSave, string SavePath, Encoding ENCODE)
{
// Part1
try
{
using (StreamReader sr = new StreamReader(SavePath, ENCODE))
{
string result = "";
while (sr.EndOfStream != true)
result = result + sr.ReadLine() + "\n";
MessageBox.Show(result);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
// Part2
try
{
using (FileStream fs = new FileStream(SavePath, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs, ENCODE))
{
await sw.WriteAsync(StrToSave);
await sw.FlushAsync();
sw.Close();
}
MessageBox.Show("Save");
fs.Close();
}
}
// The Run End Here And didn't Continue to Part 3
catch (Exception e)
{
Console.WriteLine(e);
}
// Part3
try
{
using (StreamReader sr = new StreamReader(SavePath, ENCODE))
{
string result = "";
while (sr.EndOfStream != true)
result = result + sr.ReadLine() + "\n";
MessageBox.Show(result);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
But I find it strange that the process end at the place where Part2 complete, and the process directly end but didn't continue on Part3,
What's the reason to this condition? Generally the process should go through whole method but should not stop in the middle
(one more question)
And is there some other way can do the purpose of part2, and also can continue to part3 to comlplete whole method?
It could be because you are writing an async void method and you are calling some async methods in part 2. Try to change the async methods in part 2 to non-async methods:
using (StreamWriter sw = new StreamWriter(fs, ENCODE))
{
sw.Write(StrToSave);
sw.Flush(); // Non-async
sw.Close(); // Non-async
}
Does it behave as you expect now?
The problem is you are telling your app to await the methods, but never getting the Task result or a giving it a chance to complete. From what you've shown so far, you don't need the async stuff anyway, and greatly simplify the code:
public static void SaveTextFile(string StrToSave, string SavePath, Encoding ENCODE)
{
//Part1
try
{
MessageBox.Show(File.ReadAllText(SavePath, ENCODE));
}
catch (Exception e)
{
Console.WriteLine(e);
}
//Part2
try
{
File.WriteAllText(SavePath, StrToSave, ENCODE);
MessageBox.Show("Save");
}
catch (Exception e)
{
Console.WriteLine(e);
}
//Part3
try
{
MessageBox.Show(File.ReadAllText(SavePath, ENCODE));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

how to return from try/catch/finally in C# ?

If at all returning within try/catch/finally in not considered a structured programming how can I return from the below code block ?
public static string ReadFile()
{
StreamReader streamReader = null;
try
{
try
{
streamReader = new StreamReader(#"C:\Users\Chiranjib\Downloads\C# Sample Input Files\InputParam.txt"); //Usage of the Verbatim Literal
return streamReader.ReadToEnd();
}
catch (FileNotFoundException exfl)
{
string filepath = #"C:\Users\Chiranjib\Downloads\C# Sample Input Files\LogFiles.txt";
if (File.Exists(filepath))
{
StreamWriter sw = new StreamWriter(filepath);
sw.WriteLine("Item you are searching for {0} just threw an {1} error ", exfl.FileName, exfl.GetType().Name);
Console.WriteLine("Application stopped unexpectedly");
}
else
{
throw new FileNotFoundException("Log File not found", exfl);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
return string.Empty;
}
//Code inside finally gets executed even if the catch block returns when an exception happens
finally
{
//Resource de-allocation happens here
if (streamReader != null)
{
streamReader.Close();
}
Console.WriteLine("Finally block executed");
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Log file not found ");
Console.WriteLine("Original exception " + ex.GetType().Name);
Console.WriteLine("Inner Exception " + ex.InnerException.GetType().Name);
}
finally
{
if (streamReader != null)
{
streamReader.Close();
}
Console.WriteLine("Finally block executed");
}
return streamReader.ReadToEnd() ?? string.Empty;
}
Thing is if I at all close the streamReader object before even getting it's value I would not be able to obtain a returned result.
But again it does not allow me to put a return in finally.
Please help me understand and overcome this difficulty in a standard way.
The easiest way for you to resolve this would be to just declare a variable inside your code and then read that out at the end.
For example.
public static string ReadFile()
{
var stringFile = "";
StreamReader streamReader = null;
try
{
try
{
streamReader = new StreamReader(#"C:\Users\Chiranjib\Downloads\C# Sample Input Files\InputParam.txt"); //Usage of the Verbatim Literal
stringFile = streamReader.ReadToEnd();
return stringFile
}
catch (FileNotFoundException exfl)
{
string filepath = #"C:\Users\Chiranjib\Downloads\C# Sample Input Files\LogFiles.txt";
if (File.Exists(filepath))
{
StreamWriter sw = new StreamWriter(filepath);
sw.WriteLine("Item you are searching for {0} just threw an {1} error ", exfl.FileName, exfl.GetType().Name);
Console.WriteLine("Application stopped unexpectedly");
}
else
{
throw new FileNotFoundException("Log File not found", exfl);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
return string.Empty;
}
//Code inside finally gets executed even if the catch block returns when an exception happens
finally
{
//Resource de-allocation happens here
if (streamReader != null)
{
streamReader.Close();
}
Console.WriteLine("Finally block executed");
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Log file not found ");
Console.WriteLine("Original exception " + ex.GetType().Name);
Console.WriteLine("Inner Exception " + ex.InnerException.GetType().Name);
}
finally
{
if (streamReader != null)
{
streamReader.Close();
}
Console.WriteLine("Finally block executed");
}
return stringFile;
}
This should then read out your file by executing the following code
static void Main(string[] args)
{
var file = ReadFile();
Console.WriteLine(file);
Console.ReadLine();
}
I think you could eliminate several of those try/catch sequences and take care of disposing StreamWriter and StreamReader by using "using" statements. Here's an example:
using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var fileContents = ReadFile();
Console.ReadLine(); // cause program to pause at the end
}
public static string ReadFile()
{
try
{
using (var streamReader = new StreamReader(
#"C:\MyTestFile.txt"))
{
var fileContents = streamReader.ReadToEnd();
Console.WriteLine("File was read successfully");
return fileContents;
}
}
catch (FileNotFoundException fileNotFoundException)
{
LogReadFileException(fileNotFoundException);
}
catch (DirectoryNotFoundException directoryNotFoundException)
{
LogReadFileException(directoryNotFoundException);
}
catch (IOException ioException)
{
LogReadFileException(ioException);
}
// If we get here, an exception occurred
Console.WriteLine("File could not be read successfully");
return string.Empty;
}
private static void LogReadFileException(Exception exception)
{
string logFilePath = #"C:\MyLogFile.txt";
using (var streamWriter = new StreamWriter(logFilePath,
append: true))
{
var errorMessage = "Exception occurred: " +
exception.Message;
streamWriter.WriteLine(errorMessage);
Console.WriteLine(errorMessage);
}
}
}
}

IRCBot C# connection issues

I am having an issue with my IRC Bot I am trying to write in c# just as a way to help get my head around the IRC protocol, I am planning on writing a client/server in the future but as you can prolly guess I am far off this :P
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace LolBot
{
struct IRCConfig
{
public string server;
public int port;
public string nick;
public string name;
}
class IRCBot
{
TcpClient IRCConnection = null;
IRCConfig config;
NetworkStream ns = null;
StreamReader sr = null;
StreamWriter sw = null;
public IRCBot(IRCConfig config)
{
this.config = config;
try
{
IRCConnection = new TcpClient(config.server, config.port);
}
catch
{
Console.WriteLine("Connection Error");
}
try
{
ns = IRCConnection.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
sendData("USER", config.nick + config.name);
sendData("NICK", config.nick);
}
catch
{
Console.WriteLine("Communication error");
}
finally
{
if (sr != null)
sr.Close();
if (sw != null)
sw.Close();
if (ns != null)
ns.Close();
if (IRCConnection != null)
IRCConnection.Close();
}
}
public void sendData(string cmd, string param)
{
if (param == null)
{
sw.WriteLine(cmd);
sw.Flush();
Console.WriteLine(cmd);
}
else
{
sw.WriteLine(cmd + " " + param);
sw.Flush();
Console.WriteLine(cmd + " " + param);
}
}
public void IRCWork()
{
string[] ex;
string data;
bool shouldRun = true;
while (shouldRun)
{
data = sr.ReadLine();
Console.WriteLine(data);
char[] charSeparator = new char[] { ' ' };
ex = data.Split(charSeparator, 5);
if (ex[0] == "PING")
{
sendData("PONG", ex[1]);
}
if (ex.Length > 4) //is the command received long enough to be a bot command?
{
string command = ex[3]; //grab the command sent
switch (command)
{
case ":!join":
sendData("JOIN", ex[4]); //if the command is !join send the "JOIN" command to the server with the parameters set by the user
break;
case ":!say":
sendData("PRIVMSG", ex[2] + " " + ex[4]); //if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
break;
case ":!quit":
sendData("QUIT", ex[4]); //if the command is quit, send the QUIT command to the server with a quit message
shouldRun = false; //turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
break;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
IRCConfig conf = new IRCConfig();
conf.name = "LolBot";
conf.nick = "LolBot";
conf.port = 6667;
conf.server = "irc.strictfp.com";
new IRCBot(conf);
Console.WriteLine("Bot quit/crashed");
Console.ReadLine();
}
}
}
Whenever I execute the Bot, it comes up with:
USER AspiBot google.com google.com :AspiBot
NICK AspiBot
Bot quit/crashed
I don't really understand why it is quiting before connecting to the server and I am also looking on how to set it up to join a channel, I am aware that I need to use JOIN but I'm not sure how to implent it.
You should probably not do so much in the constructor, but the problem you are encountering here is that you are not calling IRCWork() after newing up the bot.
var bot = new IRCBot(conf);
bot.IRCWork();
EDIT You are also closing all of your connections in the finally block of your constructor, so IRCWork() isn't going to work anyway. Try implementing IDisposable, and putting your close logic in Dispose():
using (var bot = new IRCBot(conf))
{
bot.IRCWork();
}
Quick refactor of posted code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace LolBot
{
internal struct IRCConfig
{
public string server;
public int port;
public string nick;
public string name;
}
internal class IRCBot : IDisposable
{
private TcpClient IRCConnection = null;
private IRCConfig config;
private NetworkStream ns = null;
private StreamReader sr = null;
private StreamWriter sw = null;
public IRCBot(IRCConfig config)
{
this.config = config;
}
public void Connect()
{
try
{
IRCConnection = new TcpClient(config.server, config.port);
}
catch
{
Console.WriteLine("Connection Error");
throw;
}
try
{
ns = IRCConnection.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
sendData("USER", config.nick + config.name);
sendData("NICK", config.nick);
}
catch
{
Console.WriteLine("Communication error");
throw;
}
}
public void sendData(string cmd, string param)
{
if (param == null)
{
sw.WriteLine(cmd);
sw.Flush();
Console.WriteLine(cmd);
}
else
{
sw.WriteLine(cmd + " " + param);
sw.Flush();
Console.WriteLine(cmd + " " + param);
}
}
public void IRCWork()
{
string[] ex;
string data;
bool shouldRun = true;
while (shouldRun)
{
data = sr.ReadLine();
Console.WriteLine(data);
char[] charSeparator = new char[] {' '};
ex = data.Split(charSeparator, 5);
if (ex[0] == "PING")
{
sendData("PONG", ex[1]);
}
if (ex.Length > 4) //is the command received long enough to be a bot command?
{
string command = ex[3]; //grab the command sent
switch (command)
{
case ":!join":
sendData("JOIN", ex[4]);
//if the command is !join send the "JOIN" command to the server with the parameters set by the user
break;
case ":!say":
sendData("PRIVMSG", ex[2] + " " + ex[4]);
//if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
break;
case ":!quit":
sendData("QUIT", ex[4]);
//if the command is quit, send the QUIT command to the server with a quit message
shouldRun = false;
//turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
break;
}
}
}
}
public void Dispose()
{
if (sr != null)
sr.Close();
if (sw != null)
sw.Close();
if (ns != null)
ns.Close();
if (IRCConnection != null)
IRCConnection.Close();
}
}
internal class Program
{
private static void Main(string[] args)
{
IRCConfig conf = new IRCConfig();
conf.name = "LolBot";
conf.nick = "LolBot";
conf.port = 6667;
conf.server = "irc.strictfp.com";
using (var bot = new IRCBot(conf))
{
bot.Connect();
bot.IRCWork();
}
Console.WriteLine("Bot quit/crashed");
Console.ReadLine();
}
}
}

Categories