Как я могу протестировать серверную программу без использования клиентской программы и наоборот? - PullRequest
0 голосов
/ 27 апреля 2019

Предположим, у меня есть следующие серверные и клиентские приложения, написанные на C #:

Серверное приложение

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace SocketsServerStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            // create a socket to 
            Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress ipaddr = IPAddress.Any;

            // end-point is a combination of ip adress and port
            IPEndPoint ipep = new IPEndPoint(ipaddr, 23000);

            try
            {
                // bind the socket to the ip end-point
                listenerSocket.Bind(ipep);

                // start listening. 
                // can handle 5 clients at a time.
                listenerSocket.Listen(5);

                Console.WriteLine("About to accept incoming connection.");

                // block the application for synchronous operation
                Socket client = listenerSocket.Accept();

                Console.WriteLine("Client connected. " + client.ToString() + " - IP End Point: " + client.RemoteEndPoint.ToString());

                byte[] buff = new byte[128];

                int numberOfReceivedBytes = 0;

                while (true)
                {
                    numberOfReceivedBytes = client.Receive(buff);

                    Console.WriteLine("Number of received bytes: " + numberOfReceivedBytes);

                    Console.WriteLine("Data sent by client is: " + buff);

                    string receivedText = Encoding.ASCII.GetString(buff, 0, numberOfReceivedBytes);

                    Console.WriteLine("Data sent by client is: " + receivedText);

                    client.Send(buff);

                    if (receivedText == "x")
                    {
                        break;
                    }

                    Array.Clear(buff, 0, buff.Length);

                    numberOfReceivedBytes = 0;
                }
            }
            catch(Exception excp)
            {
                Console.WriteLine(excp.ToString());
            }            
        }
    }
}

Клиентское приложение

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SocketClientStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket client = null;
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress ipaddr = null;

            try
            {
                Console.WriteLine("Please Type a Valid Server IP Address and Press Enter: ");

                string strIPAddress = Console.ReadLine();

                Console.WriteLine("Please Supply a Valid Port Number 0 - 65535 and Press Enter: ");
                string strPortInput = Console.ReadLine();
                int nPortInput = 0;

                if (strIPAddress == " ") strIPAddress = "127.0.0.1";
                if (strPortInput == " ") strPortInput = "23000";

                if (!IPAddress.TryParse(strIPAddress, out ipaddr))
                {
                    Console.WriteLine("Invalid server IP supplied.");
                    return;
                }
                if (!int.TryParse(strPortInput.Trim(), out nPortInput))
                {
                    Console.WriteLine("Invalid port number supplied, return.");
                    return;
                }

                if (nPortInput <= 0 || nPortInput > 65535)
                {
                    Console.WriteLine("Port number must be between 0 and 65535.");
                    return;
                }

                System.Console.WriteLine(string.Format("IPAddress: {0} - Port: {1}", ipaddr.ToString(), nPortInput));

                client.Connect(ipaddr, nPortInput);

                Console.WriteLine("Connected to the server, type text and press enter to send it to the srever, type <EXIT> to close.");

                string inputCommand = string.Empty;

                while(true)
                {
                    inputCommand = Console.ReadLine();

                    if(inputCommand.Equals("<EXIT>"))
                    {
                        break;
                    }

                    byte[] buffSend = Encoding.ASCII.GetBytes(inputCommand);

                    client.Send(buffSend);

                    byte[] buffReceived = new byte[128];
                    int nRecv = client.Receive(buffReceived);

                    Console.WriteLine("Data received: {0}", Encoding.ASCII.GetString(buffReceived, 0, nRecv));
                }
            }
            catch (Exception excp)
            {
                Console.WriteLine(excp.ToString());
            }
            finally
            {
                if (client != null)
                {
                    if (client.Connected)
                    {
                        client.Shutdown(SocketShutdown.Both);
                    }
                    client.Close();
                    client.Dispose();
                }
            }

            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();

        }
    }
}

Как правило, кто-то должен сделать, чтобы проверить их, запустить обе программы одновременно и наблюдать за их взаимодействием.

Я хочу проверить их по отдельности. То есть без запуска их одновременно.

Как я могу это сделать?

Доступен ли telnet? Если да, то как?

...