Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[TUTORIAL]C# Beginner Networking Tutorial Pt. 1 : The Server[TUTORIAL]

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
Thx for another great program. Works very well.
(Jan 28, 2009 07:03 PM)[Evolution] Wrote:  Thx for another great program. Works very well.
😝
Great Tutorial. I don't know C#, but it looks great. 👍
(Jan 28, 2009 08:57 PM)Scryrain Wrote:  Great Tutorial. I don't know C#, but it looks great. 👍
👍
ShockLabs,what does this program does?

😏

Thanks..
yaaay Always Great :)

BravO For You Domain :) ^^
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
(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 😝
seems complicated.. ehhe
Wow, C# looks way less complicated then C++ in Network programming


Possibly Related Threads...
Thread / Author Replies Views Last Post
by TheNeutronJan 24, 2012 07:48 AM 9 346 Jan 24, 2012 07:48 AM
Last Post: roxxor1339
by ToXiiCNov 27, 2011 02:30 AM 5 176 Nov 27, 2011 02:30 AM
Last Post: melsve
by REDRIFLENov 20, 2011 11:53 PM 7 244 Nov 20, 2011 11:53 PM
Last Post: john10294