Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 socket Dictionary
How can a dictionary be sent to the client From Server ?
 
I want to do I send the following dictionary
 
Dictionary rates = new Dictionary<string,>(); 
        rates.Add("usd", 47.5); 
        rates.Add("eur", 60.4);
        rates.Add("ukp", 78.8);
 
Codes do not work.Where is my problem? Whether this approach is wrong ?
 
Please show me the correct code
 
code in server :
 

public void GetListn()
    {
        var dataToSend = new DataToSend { Rates = new Dictionary<string,>() };
 
        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,> Rates { get; set; }
}
 
code in Client :
 
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,> Rates { get; set; }
    }
Posted 10-Nov-11 8:45am
Edited 10-Nov-11 9:43am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

your code should work, if you are sending using TCP protocol then dictionary object stream should work only thing is at receiver you have to return back to dictionary object. you can use TCP Serialization.
 
If its HTTP then you can use HTTP Serialization but only thing on receiver side you need to have same Class to marshal the stream back to Object.
In your case, with strings it should work without any problem.

Regards
Rushi
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

How about:
var dataToSend = new DataToSend { Rates = new Dictionary<string,>() };
dataToSend.Rates = (DataToSend)formatter.Deserialize(stream);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 10 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100