Я пишу клиент-серверную программу.
Сервер будет содержать словарь, и клиенты смогут добавлять значения ключей обновления в словарь.
Предположим, клиент- 'A' добавляет элемент (1, 111)
. Когда другой клиент - 'B' хочет обновить значение (1, 111)
, он должен запросить подтверждение у 'A' и наоборот.
Пожалуйста, взгляните на следующую программу:
using MyClientServerLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MyServerProgram
{
class ServerProgram
{
static Dictionary<string, KeyValue> KeyValueDictionary;
static Dictionary<string, ClientClass> ClientDictionary;
static void Main(string[] args)
{
Console.Title = "Server";
Console.WriteLine("Server program started...");
KeyValueDictionary = new Dictionary<string, KeyValue>();
ClientDictionary = new Dictionary<string, ClientClass>();
string ipAddress = "127.0.0.1";
int portNo = 2000;
IPAddress ip = IPAddress.Parse(ipAddress);
TcpListener listener = new TcpListener(ip, portNo);
// poll for clients in a 2nd thread
Thread thread = new Thread(delegate()
{
ServerProgram.PollIncomingClientConns(listener);
});
thread.Start();
}
#region catching client connections
static void PollIncomingClientConns(TcpListener listener)
{
listener.Start();
try
{
bool keepRunning = true;
while (keepRunning)
{
ClientClass client = new ClientClass(listener);
ClientDictionary.Add(client.ID, client);
Thread thread = new Thread(delegate()
{
ServerProgram.ReadFromClient(client);
});
thread.Start();
}
}
catch (Exception ex)
{
var inner = ex.InnerException as SocketException;
if (inner != null && inner.SocketErrorCode == SocketError.ConnectionReset)
Console.WriteLine("Disconnected");
else
Console.WriteLine(ex.Message);
listener.Stop();
}
}
#endregion
static void ReadFromClient(ClientClass client)
{
try
{
ClientClass origClient = null;
while (client.Tcp.Connected)
{
string str = client.ReadString();
switch(str)
{
case Commands.AddKeyValue:
string key = client.ReadString();
string val = client.ReadString();
KeyValue keyValue = new KeyValue(client.ID, key, val);
KeyValueDictionary.Add(key, keyValue);
Console.WriteLine(keyValue.ToString() + " added.");
break;
case Commands.ListKeys:
if (KeyValueDictionary.Keys.Count > 0)
{
foreach (string key1 in KeyValueDictionary.Keys)
{
client.Write("(" + key1 + "," + KeyValueDictionary[key1].Value + ")\t");
}
}
else
{
client.Write("No KV");
}
break;
case Commands.UpdateValue: //obtain update requester client-ID
client.Key = client.ReadString(); //read key
client.Value = client.ReadString(); //read val
KeyValue oldKeyVal = null;
bool currClientFound = KeyValueDictionary.TryGetValue(client.Key, out oldKeyVal); //search the dictionary with the client name
if (currClientFound) //if found
{
if (oldKeyVal.ClientID.Equals(client.ID))//if rem requested by same client...
{
KeyValueDictionary[client.Key].Value = client.Value;
client.Write(client.Key + " is updated.");
}
else //if rem requested by a different client...
{
//get the original client
bool origClientFound = ClientDictionary.TryGetValue(oldKeyVal.ClientID, out origClient); //search the dictionary with the client name
if (origClientFound)
{
origClient.Write(client.ID + " wants to change " + oldKeyVal.ToString() + "(y/n)");
}
}
}
break;
case Commands.Yes:
Console.WriteLine(client.Key + ", " + client.Value);
KeyValueDictionary[client.Key].Value = client.Value;
client.Write(client.Key + " is updated.");
break;
}
}
}
catch
{
client.Disconnect();
}
}
}
}
Проблема в case Commands.UpdateValue
и case Commands.Yes
.
Когда клиент запрашивает обновление на предыдущей итерации, пара ключ-значение сохраняется вместе с экземпляром клиента. Затем на следующей итерации, когда другой клиент отправляет подтверждение, пара ключ-значение обновляется.
Но на следующей итерации client.Key
показывает null
. Следовательно, case Commands.Yes
терпит неудачу.
Как я могу решить эту проблему?
Дополнительные файлы исходного кода:
- ClientProgram.cs
- ClientClass.cs