lundi 15 mars 2021

Filtering problem with if statement on server code

So I'm building a server that when receives an input must check the Dictionary I made to check if the username is found in there. My problem is that whenever I try to test this I keep on getting a no entries found message. Is it possible to see what is in the dictionary or perhaps there is an issue with my if statements filter. I'm out of ideas so I was hoping to know if anyone can help me. Here is the code that does the filtering of the input:

  String line = sr.ReadLine().Trim();
        String[] Words = line.Split(new char[] { ' ' }, 2);
        String username, location;
        Console.WriteLine("Message recieved: " + line);

        if (Words.Length == 1)
        {
            username = Words[0];

            if (UID.ContainsKey(username))
            {
                location = UID[username];
                Console.WriteLine(location);
                sw.WriteLine(location);
                sw.Flush();
            }
            else
            {
                location = "ERROR: no entries found\r\n";
                Console.WriteLine(location);
                sw.WriteLine(location);
                sw.Flush();
            }
        }

and if it helps, here is the code in its entirety:

using System;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Collections.Generic;
public class Whois
{
    static Dictionary<string, string> UID = new Dictionary<string, string>();

static void Main(string[] args)
{
    runServer();
}
static void runServer()
{

    TcpListener listener;
    Socket connection;
    NetworkStream socketStream;
    try
    {
        listener = new TcpListener(IPAddress.Any, 43);
        listener.Start();
        Console.WriteLine("Server started listening");
        while (true)
        {
            connection = listener.AcceptSocket();
            connection.SendTimeout = 1000;
            connection.ReceiveTimeout = 1000;
            socketStream = new NetworkStream(connection);
            Console.WriteLine("Connection recieved");
            doRequest(socketStream);
            socketStream.Close();
            connection.Close();
        }
    }  catch(Exception e)
    {
        Console.WriteLine("Exception: " + e);
    }
  
}

  static void doRequest(NetworkStream socketStream)
    {
     
        try
        {
   StreamWriter sw = new StreamWriter(socketStream);
        StreamReader sr = new StreamReader(socketStream);
            String line = sr.ReadLine().Trim();
            String[] Words = line.Split(new char[] { ' ' }, 2);
            String username, location;
            Console.WriteLine("Message recieved: " + line);

            if (Words.Length == 1)
            {
                username = Words[0];

                if (UID.ContainsKey(username))
                {
                    location = UID[username];
                    Console.WriteLine(location);
                    sw.WriteLine(location);
                    sw.Flush();
                }
                else
                {
                    location = "ERROR: no entries found\r\n";
                    Console.WriteLine(location);
                    sw.WriteLine(location);
                    sw.Flush();
                }
            }
            else if (Words.Length == 2)
            {
                username = Words[0];
                location = Words[1];
                Console.WriteLine("OK");
                sw.WriteLine("OK");
                sw.Flush();
            }
            else
            {
                Console.WriteLine("There have been no words inputted!");
                sw.WriteLine("There have been no words inputted!");
                sw.Flush();
            }
        }
        catch
        {
            Console.WriteLine("Something went wrong");
        }      
    }
}

Also, here are the tests that I'm running my server against to see if it works:

Server Code Test

Aucun commentaire:

Enregistrer un commentaire