无法更改端口或主机/服务器,c#

时间:2017-02-16 16:08:17

标签: c# server client port

我目前正在制作客户端和服务器。服务器将使用字典存储人员及其位置。然后,客户端可以查找位置或更新/添加人员及其位置。例如,我可以输入Lucy' School' School',服务器会将其添加到词典中。如果我输入'露西'它应该回复学校'如果我输入'露西' '首页'它应该与字典有关。

但是,在参数中,用户可以输入/ h后跟主机名和/ p后跟端口号。我目前正在尝试在客户端中实现此功能,但它似乎根本无法正常工作。

以下是我的代码。我为参数创建了一个列表,这样如果它有/ h或/ p后跟相应的信息,我可以减少参数的数量,这样它就不会影响程序的其他部分。

static void Main(string[] args)
{

    String server = "whois.networksolutions.com";
    int port = 43;

    List<string> list = new List<string>(args);

    for (int i = 0; i < args.Length; i++)
    {
        if (args[i].Trim() == "/h")
        {
            string serverString = args[i + 1].Trim();
            server = args[i + 1];
            list.RemoveAt(i);
            //remove h from the list
            list.RemoveAt(i);
            //remove server name from the lst
            args = list.ToArray();
            //update args array
            i = i - 1;
            Console.WriteLine("Server changed to " + serverString);
        }

        else if (args[i].Trim() == "/p")
        {
            string portString = args[i + 1];
            port = Convert.ToInt32(args[i + 1].Trim());
            list.RemoveAt(i);
            //remove p from the list
            list.RemoveAt(i);
            //remover port number from list
            args = list.ToArray();
            //update args array
            i = i - 1;
            Console.WriteLine("Port changed to " + portString);
        }
    }


    try
    {
        TcpClient client = new TcpClient();
        client.Connect(server, port);
        client.ReceiveTimeout = 1000;
        client.SendTimeout = 1000;
        StreamWriter sw = new StreamWriter(client.GetStream());
        StreamReader sr = new StreamReader(client.GetStream());
        sw.AutoFlush = true;
        sw.WriteLine(args[0]);


        if (args.Length == 1)
        {
            if (args[0] == "514872")
            {
                Console.WriteLine("514872 is being tested\r\n");
            }
            else
            {
                Console.WriteLine("ERROR: no entries found\r\n");
            }
        }
        else if (args.Length == 2)
        {
            Console.WriteLine(args[0] + " location changed to be is being tested\r\n");
        }
    }
    catch
    {
        Console.WriteLine("Connection failure.");
    }
}

}

任何帮助都会受到大力赞赏!

谢谢 露

0 个答案:

没有答案