Tech 2 Game - Ultimate Tech and Game Forum
Welcome to Tech 2 Game - Ultimate Tech and Game Forum Guest!    Register | Lost Password?




Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Share Post: Digg Delicious Reddit Face Book Stumbleupon
Connecting To IRC Servers [C#Tutorial]
02-25-2010, 02:14 PM
Post: #1
Connecting To IRC Servers [C#Tutorial] :
Hey Guys Smile

Alright, Today I'm Going To Teach You How To Connect To IRC Servers In C#!

Ready? Good.

Okay, So First Make A New Console Project. Name It Whatever You'd Like.

All Your Code Ready? Awesome.

So Lets Start Off With Adding Some using Statements.

Code:
using System.IO; //For Reading/Writing To The Server
using System.Net.Sockets; //Connecting To The Server

Now That You've Added Those, What Do We Have To Do To Connect? Well Let Me Show You.

In Your Main Function, Add This:
Code:
TcpClient Client = new TcpClient("irc.server.here", 6667); //The Normal IRC Port Is 6667, However Yours May Be Different
NetworkStream NwStream = Client.GetStream(); //Get The TCP/Network Stream And Assign It To NwStream
StreamReader Reader = new StreamReader(NwStream); //Read The Data From The IRC Server
StreamWriter Writer = new StreamWriter(NwStream); //Write To The Server

Alright, So We've Connected To The Server, And It Expects Some Data To Be Sent Right Away.

So Right After That, Add This:
Code:
Writer.WriteLine("USER Name IRCServerName Domain Description"); //This Gives The Server The Required USER Information.
//Note- Replace IRCServerName With The [irc].server.here And Domain With irc.[server].here
//[Example Would Be USER wesnr verne freenode Wesnr] The Server Address Is verne.freenode.net, So You Should Be Able To Put It Together :)
//If There's More Than One Word In The Description, Put : Before It
Writer.Flush(); //VERY IMPORTANT!!! Make Sure You Flush EACH Time After You Write Something To The Stream!
Writer.WriteLine("NICK NickName"); //Supply NickName
Writer.Flush();
Writer.WriteLine("JOIN #Channel"); //Joins The Channel Specified.

Get That? Cool. Now We Can Start Sending And Receiving Data From The Server.

So Next We Want To Put In This:
Code:
string Data = ""; //To Receive Data
while((Data = Reader.ReadLine()) != null) //While We're Not Getting Any Null Information From The Server
{
      Console.WriteLine(Data); //Write Out Raw IRC Data
      if(Data.Contains(".SayHi!")) //If The Data Contains .SayHi!
      {
           Writer.WriteLine("PRIVMSG #Channel :Hello, Everyone!"); //Write Out to The Channel "Hello, Everyone!" [The : Is To Tell It That There Are Spaces In The Message]
           Writer.Flush(); //Don't Forget! :D
      }
      else
      {
           Writer.WriteLine("PRIVMSG #Channel :Wuzzat?"); //I'm Guessing You Can Understand What That Does :P
           Writer.Flush();
      }
}

Did You Understand All That? Cool!

So Now A Bot, Your Bot, Will Join Your Channel, And Reply To You!

If It Works, Awesome Job! If Not, Read Through The Tutorial And Try Again! But If You Still Have Problems, Feel Free to Message Me Smile

Please Note!: All Of the Data Is RAW IRC, So It's Not The Fancy <UserName>: Message, It's :UserName!DNS ... Etc (You'll Understand What I Mean Once you Look At It) So You May Want To Find A Way To Parse The Message! Smile

I Also Want To Mention That Raw IRC Commands Don't Begin With / They Are Normally CAPITAL Versions Of What You'd Type After / Smile


I Hope I Helped Smile

If You Liked It, Don't Forget To Say Thanks Smile

[Only Registered users can see links. ]
Visit this user's website Find all posts by this user
Sponsored by
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How to Compile C Programs [Tutorial] Sukanjan.K 0 101 10-17-2009 06:15 PM
Last Post: Sukanjan.K


[-]
Permissions Box
You cannot Post Threads.
You cannot Post Replies.
You cannot Post Attachments.
You cannot Edit Your Posts.
HTML is off
MyCode is on
Smilies is on
[img]-code is on