简体   繁体   中英

c# Using static variables in different projects

I have a solution with two projects which act as Server and Client respectively. The Client is a simple console application which sends data to the server. The server is a WPF application which receives the data and displays it in a datagrid. The MVVM approach is used here.

In the Server UI there are three textboxes in which the user can type in:

IP Address: ("127.0.0.1")

Port: (some port)

Delimeter: (some char like '@' for example)

The challenge for me in this one is that, whatever delimeter the user provides, it should be used in the client project, to be put in between the data which is to be sent. For example the client sends:

Name + Delimeter + Surname + Delimeter + Age

What i have tried:

I added a Utils class with static fields for IPAddress, port and delimeter like this:

public class Utils
{
    public static string IP_ADDRESS = " ";
    public static int PORT = 0;
    public static char DELIMETER = '\0';

}

I then tried to change these values in my ViewModel where the respective properties which are bound to the UI are by assigning them:

private void storeData()
{
    Utils.IP_ADDRESS = IP;
    Utils.PORT = Port;
    Utils.DELIMETER = Delimeter;
}

In the client program:

static void Main(string[] args)
    {
        Client client = new Client(Utils.IP_ADDRESS, Utils.PORT);

        while (true)
        {
            client.SendData("some Name" + Utils.DELIMETER + "some Surname" +  Utils.DELIMETER + some Age + Utils.DELIMETER + "something else");
            Thread.Sleep(3000);
        }
    }

The problem here is that whenever i start a new Client instance the values from the util class are still the default ones (null).

Any help is appreciated.

Let's break down your problem:

  1. The server can change ip or ports at will and the clients will somehow guess the new port and connect.
  2. The server changes the delimiter at will and the clients adapt to the new delimiter.

Problem 1 is impossible. Information cannot magically get transferred to clients before the client connects to the server, and the client needs ip and ports to connect to the server. Whatever technique you use to transfer the ip and port to the client is a better communication channel than your client/server, so you don't need a client/server.

Problem 2 has been solved by WCF already . Use WCF and SOAP or REST (which is just HTML).

Here is a sample of what the code would look like for the clients to determine the delimiter before sending the main request:

class Server
{
    private TcpListener _listener = new TcpListener(12312);            
    public void Start()
    {
        _listener.Start();
        while (true)
        {
            var client = _listener.AcceptTcpClient();
            var stream = client.GetStream();
            var request = getRequest(stream);
            if (request == "GetDelimiter")
            {
                SendResponse(Utils.DELIMITER, stream);
            }
            else
            {
                ProcessNameSurnameAge(request);
            }
        }
    }
}

class Client
{
    private TcpClient _client = new TcpClient();
    public void DoTheThing()
    {
        _client.Connect("127.0.0.1", 12312);
        var stream = _client.GetStream();
        SendRequest("GetDelimiter", stream);
        var delimiter = GetResponse(stream);
        var newRequest = "some Name" + delimiter + "some Surname" + delimiter + "some Age" + delimiter + "something else";
        SendRequest(newRequest);
    }
}

Note that I skip over the encoding details of sending data over TCP because it seems like you've already got a handle on that.

I was able to solve this in a rather simple manner. Steps i used to solve are as follow:

In the server:

  1. Created a text file in my solution.
  2. When the server starts in my view model, i saved the properties ip, port and delimeter in a string array.
  3. Next i used the IO File class to write the content of the array in the text file.

In the client:

  1. First i read from the file.
  2. Next i created the client instance and passed the ip and port as parameters to it's constructor.

Thank you D Stanley and Damian Galletini for your suggestions. Also thank you everybody else who tried to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM