Home > Software design >  Breaking a big class into smaller methods
Breaking a big class into smaller methods

Time:10-19

I have difficulty breaking a class consisting of one method into smaller methods. I have a simple socket class that Initiate, send and receive a message in one step (one method). Now I would like to divide this into separate steps of Initialization, Send, Receive, and End as below. I have tried so many different combinations but I could not get it working.

My program that works fine:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace SocketCom
{
    internal class Program
    {
        public class SyncSocketClient
        {
            public static void StartClient() \\<<-----------
            {
                byte[] bytes = new byte[1024];
                var hostName = Dns.GetHostName();
                IPHostEntry ipHost  = Dns.GetHostEntry(hostName);
                IPAddress ip = ipHost.AddressList[1];
                IPEndPoint remoteEP = new IPEndPoint(ip, 11111);
                Socket sender = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                sender.Connect(remoteEP);
                sender.RemoteEndPoint.ToString();
                byte[] msg = Encoding.ASCII.GetBytes("Hello");
                int byteSent = sender.Send(msg);
                int byteReceived = sender.Receive(msg);
                Console.WriteLine($"[ECHO TEST] {Encoding.ASCII.GetString(bytes, 0, byteReceived)}");
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();     
            }
        }
        static void Main(string[] args)
        {
            SyncSocketClient.StartClient();
        }
    }
}

What I would like to below (not working). I do not care about the access modifier or being static or not. I just want to break one method into three separate methods in any possible way.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace SocketCom
{
    internal class Program
    {
        public class SyncSocketClient
        {
            public static void StartClient()\\<<-----------
            {
                byte[] bytes = new byte[1024];
                var hostName = Dns.GetHostName();
                IPHostEntry ipHost  = Dns.GetHostEntry(hostName);
                IPAddress ip = ipHost.AddressList[1];
                IPEndPoint remoteEP = new IPEndPoint(ip, 11111);
                Socket sender = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                sender.Connect(remoteEP);
            }   
            public static void Send(string message)\\<<-----------
            {   
                sender.RemoteEndPoint.ToString();
                byte[] msg = Encoding.ASCII.GetBytes(message);
                int byteSent = sender.Send(msg);
            }   
                
            public static void Receive()\\<<-----------
            {       
                int byteReceived = sender.Receive(msg);
                Console.WriteLine($"[ECHO TEST] {Encoding.ASCII.GetString(bytes, 0, byteReceived)}");
            }
            
            public static void EndClient()\\<<-----------
            {
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();     
            }
        }

        static void Main(string[] args)
        {
            SyncSocketClient.StartClient();
            SyncSocketClient.StartClient("Hello");
            SyncSocketClient.Receive();
            SyncSocketClient.EndClient();
        }

    }
}

I appreciate your help.

CodePudding user response:

The First step is to keep the Socket variable out of methods. So declare at class level and use this socket in all methods. static Socket sender;

CodePudding user response:

There's a few things I can see you're misunderstanding based on your attempt to do this. One is the attempt to access variables in locations where they are not in scope. sender and msg are two examples. Another is that you're not returning msg from the Send method so it can be used in the Receive method. I would suggest this article to help you understand the basics of methods in C# before proceeding.

If you're using Visual Studio, Visual Studio Automatic Refactor will help with this, however I'd strongly encourage you to understand what the tool has done to your code once you've used it and not proceed until you do understand it, this is a fundamental programming concept.

Select the code you want to extract to a method, right-click, and select "Quick Actions and Refactorings...", then "Extract Method". The IDE will create the method and all the necessary parameters for you.

  •  Tags:  
  • c#
  • Related