如何從客戶端發送字典到客戶端?發送字典與套接字從服務器到客戶端編程
我想做我發送以下字典
Dictionary rates = new Dictionary();
rates.Add("usd", 47.5);
rates.Add("eur", 60.4);
rates.Add("ukp", 78.8);
UPDATE:
密碼不work.Where是我的問題嗎?這種方法是否錯誤?
請告訴我正確的代碼
代碼在服務器:
public void GetListn()
{
var dataToSend = new DataToSend { Rates = new Dictionary<string, decimal>() };
dataToSend.Rates.Add("asd", decimal.Parse("12.23"));
dataToSend.Rates.Add("asd", decimal.Parse("41.11"));
dataToSend.Rates.Add("asd", decimal.Parse("25.23"));
var stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, dataToSend.Rates);
client.Send(stream.ToArray());
}
public class DataToSend
{
public Dictionary<string, decimal> Rates { get; set; }
}
代碼在客戶端:
private void WindowLoaded(object sender, RoutedEventArgs e)
{
try
{
_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_server.Connect(_ipep);
_server.Send(Encoding.Unicode.GetBytes("GetList"));
_data = new byte[BufferSize];
_recv = _server.Receive(_data);
var stream = new MemoryStream(_server.Receive(_data));
var formatter = new BinaryFormatter();
var dataToSend = (DataToSend)formatter.Deserialize(stream);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
public class DataToSend
{
public Dictionary<string, string> Rates { get; set; }
}
無答案?????? –