Hack Forums
[TUTORIAL]C# Beginner Networking Tutorial Pt. 1 : The Server[TUTORIAL] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45)
+--- Forum: Grayhat • Purple Team • Hybrid Security (https://hackforums.net/forumdisplay.php?fid=449)
+---- Forum: Beginner Hacking (https://hackforums.net/forumdisplay.php?fid=4)
+----- Forum: Hacking Tutorials (https://hackforums.net/forumdisplay.php?fid=47)
+----- Thread: [TUTORIAL]C# Beginner Networking Tutorial Pt. 1 : The Server[TUTORIAL] (/showthread.php?tid=47733)



[TUTORIAL]C# Beginner Networking Tutorial Pt. 1 : The Server[TUTORIAL] - ShockLabs - Jan 28, 2009

Code
using System;
using System.Collections.Generic;
//Networking namespace
using System.Net.Sockets;
//StreamReader namespace
using System.IO;
using System.Text;

namespace CS_Networking_Tutorial
{
    class Server
    {
        static void Main(string[] args)
        {
            //Create a TcpListener to listen for the client's connection! (Listens on port 100, change to what you want.)

            TcpListener Listener = new TcpListener(100);

            //Start the server.

            Listener.Start();

            //Wait for a pending connection
            while(true)
            {
                if (Listener.Pending())
                {
                    //Keep listening until we get a pending connection, then break out of the loop.
                    break;
                }
            }

            //Create a new TcpClient to accept the connection with

            TcpClient Client = Listener.AcceptTcpClient();

            //Gets the stream (data stream) from Client into a streamreader

            StreamReader Reader = new StreamReader(Client.GetStream());

                //Keep reading and displaying data

                while (true)
                {
                    try
                    {
                        //Read a message from the client

                        string Msg = Reader.ReadLine();

                        //Display the message on the console

                        Console.WriteLine("Received: " + Msg + ".");

                    }
                    catch
                    {
                        //Couldn't read data; close connection.
                        Console.WriteLine("Client left!");
                        Reader.Close();
                        Client.Close();
                        Listener.Stop();
                        break;
                    }
                }
            }
        }
    }

Commented the code for ya. If you still have questions just ask through MSN :

[email protected]

OR

post here 😝

I GOT A DOMAIN 😁

yay


- [Evolution] - Jan 28, 2009

Thx for another great program. Works very well.


- ShockLabs - Jan 28, 2009

(Jan 28, 2009 07:03 PM)EvolutioN Wrote:  Thx for another great program. Works very well.
😝


- Scryrain - Jan 28, 2009

Great Tutorial. I don't know C#, but it looks great. 👍


- ShockLabs - Jan 28, 2009

(Jan 28, 2009 08:57 PM)Scryrain Wrote:  Great Tutorial. I don't know C#, but it looks great. 👍
👍


- ohytheng - Jan 28, 2009

ShockLabs,what does this program does?

😏

Thanks..


- }Kernel32 - Jan 29, 2009

yaaay Always Great :)

BravO For You Domain :) ^^


- rogue - Jan 29, 2009

Nice program friend, ive never used C# before (was in jail for 5 yrs) but it looks very elegant just a quick question, do you have to use it in windows with the .NET framework?

-rogue


- ShockLabs - Jan 29, 2009

(Jan 29, 2009 06:49 AM)rogue Wrote:  Nice program friend, ive never used C# before (was in jail for 5 yrs) but it looks very elegant just a quick question, do you have to use it in windows with the .NET framework?

-rogue
Yeah 😝


- jujuismyname - Feb 10, 2009

seems complicated.. ehhe


- satanswrath - Feb 11, 2009

Wow, C# looks way less complicated then C++ in Network programming