mardi 20 octobre 2020

When Unit Testing a TcpClient in C# I receive No connection could be made because the target machine actively refused it [::ffff:127.0.0.1]:5000

So I'm a noob at networking and Unit Testing, but I'm supposed to validate methods from a pre-made Unit Test, such as whether a string contains certain characters, integers or has a certain length etc, but I have some issue with testing it.

Whenever I try to do the Unit Test with the TCP client and Server, I receive a System.Net.Internals.SocketExceptionFactory+ExtendedSocketException error. I have tried to change the port,IPAddress, turning off my firewall, as well as run VisuaL Studio as administrator, with no luck. I am able to connect to the server, send and read data from the client to the server just fine, which is why its odd I receive the no connection error, only when Unit Testing. It seems the Unit Test thinks there is no connection, even though there clearly is.

Code for the TcpClient:

     Class ClientUnitTest {   


         private static TcpClient Connect(){

        var client = new TcpClient();

        client.Connect(IPAddress.Loopback, 5000);
        
        return client;
    }


        public static void Main(string[] args){

        var client = Connect();

        var stream = client.GetStream();

        var data = Encoding.UTF8.GetBytes( "Connected to Server"); 

        stream.Write(data);

        data = new byte[client.ReceiveBufferSize];

        var cnt = stream.Read(data);

        var msg = Encoding.UTF8.GetString(data, 0, cnt);

        Console.WriteLine($"Message from the server: {msg}"); }

    }
    
   
 public static class Utilization
{ 

 public static void SendRequest( this TcpClient, string request)
{  
 
  var msg = Encoding.UTF8.GetBytes(request); 

client.GetStream().Write(msg,0,msg.Length); 

}



        public static Response ReadResponse(this TcpClient client)
        {
            var strm = client.GetStream();
            //strm.ReadTimeout = 250;
            byte[] resp = new byte[2048];
            using (var memStream = new MemoryStream())
            {
                int bytesread = 0;
                do
                {
                    bytesread = strm.Read(resp, 0, resp.Length);
                    memStream.Write(resp, 0, bytesread);

                } while (bytesread == 2048);
                
                var responseData = Encoding.UTF8.GetString(memStream.ToArray());
                return JsonSerializer.Deserialize<Response>(responseData, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase});
            }
        }
    }
}

The server is the exactly the same as the TcpClass example by Microsoft , with the only difference being the Port number and IPaddress. I'm able to send and read data from several clients just fine using the same server. Could it be because you arent supposed to be connected to the server when Unit Testing? You are only supposed to test the "logic" of the connection or something?

Aucun commentaire:

Enregistrer un commentaire