Click here to Skip to main content
15,441,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all. I was wondering if I could have some help with the following.
I need to convert a project which is on GibHub, namely: [^]

I need to be able to access and/or override the methods which is in the class.
For e.g.
C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;

namespace TcpChatServer
{
    class ChatSession : TcpSession
    {
        public ChatSession(TcpServer server) : base(server) {}

        protected override void OnConnected()
        {
            Console.WriteLine($"Chat TCP session with Id {Id} connected!");

            // Send invite message
            string message = "Hello from TCP chat! Please send a message or '!' to disconnect the client!";
            SendAsync(message);
        }

        protected override void OnDisconnected()
        {
            Console.WriteLine($"Chat TCP session with Id {Id} disconnected!");
        }

        protected override void OnReceived(byte[] buffer, long offset, long size)
        {
            string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
            Console.WriteLine("Incoming: " + message);

            // Multicast message to all connected sessions
            Server.Multicast(message);

            // If the buffer starts with '!' the disconnect the current session
            if (message == "!")
                Disconnect();
        }

        protected override void OnError(SocketError error)
        {
            Console.WriteLine($"Chat TCP session caught an error with code {error}");
        }
    }

    class ChatServer : TcpServer
    {
        public ChatServer(IPAddress address, int port) : base(address, port) {}

        protected override TcpSession CreateSession() { return new ChatSession(this); }

        protected override void OnError(SocketError error)
        {
            Console.WriteLine($"Chat TCP server caught an error with code {error}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // TCP server port
            int port = 1111;
            if (args.Length > 0)
                port = int.Parse(args[0]);

            Console.WriteLine($"TCP server port: {port}");

            Console.WriteLine();

            // Create a new TCP chat server
            var server = new ChatServer(IPAddress.Any, port);

            // Start the server
            Console.Write("Server starting...");
            server.Start();
            Console.WriteLine("Done!");

            Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");

            // Perform text input
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                    break;

                // Restart the server
                if (line == "!")
                {
                    Console.Write("Server restarting...");
                    server.Restart();
                    Console.WriteLine("Done!");
                    continue;
                }

                // Multicast admin message to all sessions
                line = "(admin) " + line;
                server.Multicast(line);
            }

            // Stop the server
            Console.Write("Server stopping...");
            server.Stop();
            Console.WriteLine("Done!");
        }
    }
}


What I have tried:

First of all, I won't need the Console outputs. I've already moved the above code into a class on its own. Now I want to use the OnError method from my code.

How can I do that?
Posted
Updated 12-Apr-20 6:20am

1 solution

It's not that simple: the code you found is based on a console rather than a GUI - and the two environments are totally different.
Where a console is "in charge" and gets the data it wants from the user when it wants, a GUI application doesn't - it responds to user input and processes based on that.
So changing a console app that sits in a loop calling ReadLine to a GUI based app is not a trivial matter - the whole app has to be "remodeled" to fit the GUI paradigm.

It's a bit like taking an ice hockey team and expecting them to win football matches because "there are two teams playing, and they both have goals".

What you need to do is probably to use a GUI thread and at least one background worker thread to handle the communications - modelled on (but not identical to in any way) the functionality of the existing code rather than try to blindly convert code from one paradigm to another. But that's a re-write, not a conversion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900