CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Thinking of architecture for network

    Hello people,

    Would need a few ideas and remarks on what I have to do.
    I have a server and I have clients (yay we got both) (both are scripted by me)

    The server is posing me some problems, here it is the schematics of how it's working :

    Server starts and start a TcpListener on port 13000 for instance.
    Client 1 ask for connect on that port
    Server responds with another port number (13001)
    Client 1 closes acutal link
    Server opens listener on 13001 on a new thread. Client is going to talk on 13001.
    Everyone is happy they can talk about whatever-i-dont-care

    Client 2 do a request on 13000 and server responds port 13002
    Yay they also communicate

    Now here's the onion : when Client disconnects/drop out/fly to the moon
    I don't know how to abort a given thread and remove it from list

    Here are the 3 classes :
    1st is the server listening on 13000
    Code:
        static class MyTcpServer
        {
            static public List<Thread> threads;
            static public void MainServer()
            {
                int portcounter = 13001;
                try
                {
                    // Set the TcpListener on port 13000.
                    Int32 port = 13000;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    
                    // TcpListener server = new TcpListener(port);
                    TcpListener server = new TcpListener(localAddr, port);
    
                    // Start listening for client requests.
                    server.Start();
    
                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    String data = null;
    
                    // Enter the listening loop.
                    while (true)
                    {
                        Console.Write("Waiting for a connection... ");
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
                        data = null;
                        NetworkStream stream = client.GetStream();
    
                        int i;
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            Console.WriteLine(String.Format("Received: {0}", data));
    
    
    
                            if (data.Equals("CONNECT_REQUEST"))
                            {
                                byte[] msg = System.Text.Encoding.ASCII.GetBytes(portcounter.ToString());
                                stream.Write(msg, 0, msg.Length);
                                Console.WriteLine(String.Format("Sent: {0}", msg));
                                MyTcpServers m = new MyTcpServers(portcounter);
                                Thread t = new Thread(new ThreadStart(m.StartServer));
                                threads.Add(t);
                                portcounter++;
                            }
                            else
                            {
                                byte[] msg = System.Text.Encoding.ASCII.GetBytes("CONNECTION_ERROR");
                                stream.Write(msg, 0, msg.Length);
                                Console.WriteLine(String.Format("Sent: {0}", data));
                            }
    
                        }
                        client.Close();
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                Console.WriteLine("\nHit enter to continue...");
                Console.Read();
            }
    
            static public void StartServers()
            {
                while (true)
                {
                    if (threads.Count != 0)
                    {
                        foreach (Thread t in MyTcpServer.threads)
                        {
                            if (!t.IsAlive)
                            {
                                t.Start();
                            }
                        }
                    }
                    Console.WriteLine(threads.Count.ToString() + " active servers.");
                    Thread.Sleep(250);
                }
            }
    
        }
    Second are the listener for each client :
    Code:
        
    class MyTcpServers
        {
            private Int32 port;
            private TcpListener server;
            public int waiting_for_connect=0;
            public MyTcpServers(int _port)
            {
                port = _port;
            }
    
            public void StartServer()
            {
                try
                {
                    // Set the TcpListener on port 13000.
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    
                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(localAddr, port);
    
                    // Start listening for client requests.
                    server.Start();
    
                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    String data = null;
    
                    // Enter the listening loop.
                    while (true)
                    {
                        waiting_for_connect = 0;
                        Console.Write("Waiting for a connection on port " + port.ToString());
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
                        data = null;
                        NetworkStream stream = client.GetStream();
    
                        int i;
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            waiting_for_connect = -1;
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            Console.WriteLine(String.Format("Received: {0}", data));
    
                            byte[] msg = System.Text.Encoding.ASCII.GetBytes("ACK");
                            stream.Write(msg, 0, msg.Length);
                            Console.WriteLine(String.Format("Sent: {0}", msg));
    
                        }
                        client.Close();
                    }
                }
                catch (SocketException)
                {
                    server.Stop();
                    Console.Write("Stopping listening on port " + port.ToString());
                    //Console.WriteLine("SocketException: {0}", e);
                }
            }
    
    
        }
    and the main that is.... main ^^
    Code:
        class MainApp2
        {
            static void Main()
            {
                MyTcpServer.threads = new List<Thread>();
                Console.WriteLine("Starting servers");
                Thread t1 = new Thread(new ThreadStart(MyTcpServer.MainServer));
                Thread t2 = new Thread(new ThreadStart(MyTcpServer.StartServers));
                t1.Start();
                t2.Start();
                while (true) ;
            }
        }
    Yeah I know I'm still a full-100&#37;-newbie in programming

    Clients behaviors are this way : they start connecteing after each other (3 clients) and starts talking continously for 10 seconds after that they close one after another. Problem is that my console outputs still say "3 active servers" meaning the 3 threads are not dead while they are supposed to be dead. Anyone can point me the direction where I could aim the double-barrel-shootgun ?

    Of course I never see this : Stopping listening on port " + port.ToString());

  2. #2
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Thinking of architecture for network

    Oh and I add another question : does in fact the package handles the whole making of TCP packet encapulation and the listener do the acknoledge packet alone for the other network card ?

    I'm used to &#181;C where I had to remake the whole TCP protocol and have fun with shifting bits in registers to switch from send/recive

  3. #3
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Thinking of architecture for network

    Question number 3 still on threading :
    Do all m items I removed from the list in that script are removed from memory ? I want to be sure that they are out

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace MyTcpServer
    {
    
        class MyListener
        {
            public String m_args;
            public Thread m_thread;
            public bool m_marked_for_deletion = false;
            public MyListener(String _args)
            {
                m_args = _args;
                m_thread = new Thread(new ThreadStart(Execution));
            }
    
            private void Execution()
            {
                Console.WriteLine(m_args);
                Thread.Sleep(1000);
                m_marked_for_deletion = true;
            }
        }
    
        class Program
        {
            static public List<MyListener> mlist;
    
            static public void ThreadsManager()
            {
                if (mlist.Count != 0)
                {
                    foreach (MyListener m in mlist)
                    {
                        if ((m.m_marked_for_deletion == false) && (m.m_thread.IsAlive == false))
                        {
                            m.m_thread.Start();
                        }
                        else
                        {
                            if (m.m_marked_for_deletion == true)
                            {
                                if (m.m_thread.IsAlive == true)
                                {
                                    m.m_thread.Abort();
                                    m.m_thread.Join();
                                }
                                mlist.Remove(m);
                            }
                        }
                    }
                }
                Thread.Sleep(250);
            }
    
    
            static void Main(string[] args)
            {
                mlist = new List<MyListener>();
                Thread threadManager = new Thread(new ThreadStart(ThreadsManager));
                threadManager.Start();
                int i;
                for (i = 0; i < 10; i++)
                {
                    MyListener m = new MyListener("I am number " + i.ToString());
                    mlist.Add(m);
                }
                Thread.Sleep(10000);
                threadManager.Abort();
    
                Console.WriteLine("End");
                Console.Read();
            }
        }
    }

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Thinking of architecture for network

    Just a general question-asking tip: ask questions as concisely as possible, focusing only on the core issue. People are busy and you'll get better responses if you don't scare away half your potential helpers with a really long post.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured