Click here to Skip to main content
15,898,987 members
Home / Discussions / C#
   

C#

 
GeneralRe: Socket programming Pin
musefan2-Dec-10 0:12
musefan2-Dec-10 0:12 
GeneralRe: Socket programming Pin
Tichaona J2-Dec-10 1:28
Tichaona J2-Dec-10 1:28 
GeneralRe: Socket programming Pin
musefan2-Dec-10 1:34
musefan2-Dec-10 1:34 
GeneralRe: Socket programming Pin
Luc Pattyn2-Dec-10 2:20
sitebuilderLuc Pattyn2-Dec-10 2:20 
AnswerRe: Socket programming Pin
Tichaona J2-Dec-10 4:08
Tichaona J2-Dec-10 4:08 
QuestionConversion of a object into a particular datetime format Pin
Varun Sareen1-Dec-10 17:43
Varun Sareen1-Dec-10 17:43 
AnswerRe: Conversion of a object into a particular datetime format PinPopular
OriginalGriff1-Dec-10 21:38
mveOriginalGriff1-Dec-10 21:38 
QuestionCan not deserialize [modified] Pin
Jacob D Dixon1-Dec-10 11:39
Jacob D Dixon1-Dec-10 11:39 
Error:

[12/1/2010 4:28:54 PM]: Object passed was not capable of being deserialized. Error: System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at JDMonitoring.Listen.Listener.ReadCallback(IAsyncResult iar) in C:\Users\Jacob\documents\visual studio 2010\Projects\JDMonitoring\JDMonitoring\Listen\Listener.cs:line 99 


Ok this is for a Client/Server application. I marked my custom object as serializable. The problem is when I changed it from using an array to a generic list. But before I send the data it is being sent as an Array and not a generic list.

CLIENT:
private static List<Commons> pendingCommons;

public static Commons[] PendingTasks
{
    get
    {
        if (Reg.Registered == 0)
            SetRegisterDefaults();

        if (pendingCommons == null || pendingCommons.Count < 1)
            SetCheckinDefaults();

        return pendingCommons.ToArray(); // As you can see here before returning it i'm turning it into a Commons[] object
    }
    set
    {
        pendingCommons = new List<Commons>(value);
    }
}


Here is how I'm sending:
Commons[] commons = Tasks.PendingTasks;

// SOCKET CODE
// blah blah

MemoryStream ms = new MemoryStream();
                   IFormatter formatter = new BinaryFormatter();
                   formatter.Serialize(ms, commons);

                   // Send to server
                   s.Send(ms.ToArray());
                   Logging.Log("Transferred " + ms.Length.ToString() + " bytes to server containg these actions: " + Tasks.GetActions(), true);
                   ms.Dispose();


Ok so I see in my log that it tranferred that to the server so it was able to serialize it. The Tasks.GetActions() shows what was sent

Now here is my server
private void ReadCallback(IAsyncResult iar)
        {
            StateObject state = iar.AsyncState as StateObject;
            Socket handler = state.worker;

            try
            {
                int bytesRead = handler.EndReceive(iar);
                if (bytesRead > 0)
                {
                    state.ms.Write(state.buffer, 0, bytesRead);
                    if (bytesRead == StateObject.BufferSize)
                    {
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                            new AsyncCallback(ReadCallback), state);
                    }
                    else
                    {
                        Logging.Log("Finished receiving " + state.ms.Length.ToString() + " bytes from " +
                            handler.RemoteEndPoint.ToString(), true);

                        // All data has been received from client
                        state.ms.Seek(0, SeekOrigin.Begin);
                        IFormatter formatter = new BinaryFormatter();
                        object receivedObject = null;

                        try
                        {
                            receivedObject = formatter.Deserialize(state.ms); // FAILS HERE
                        }
                        catch (SerializationException se)
                        {
                            Logging.Log("Object passed was not capable of being deserialized. Error: " + se.ToString(), false);
                        }

                        // Make sure we didn't receive a null object
                        string agentId = string.Empty;
                        if (receivedObject != null)
                        {
                            if (receivedObject.GetType() == typeof(Commons[]))
                            {
                                // We validated our object is actually of type Commons
                                // so we now cast and perform our tasks
                                Commons[] cObject = (Commons[])receivedObject;
                                if (cObject != null)
                                {
                                    Tasks.Perform(ref cObject, out agentId);
                                }
                            }
                            else
                                Logging.Log("Agent did not send back a Commons object", true);
                        }
                        else
                            Logging.Log("Agent " + handler.RemoteEndPoint.ToString() + " sent back null", true);

                        // Serialize our data and send it back to the agent
                        MemoryStream ms = new MemoryStream();
                        formatter.Serialize(ms, Commands.GetAgentCommands(agentId));

                        handler.BeginSend(ms.ToArray(), 0, (int)ms.Length, 0,
                            new AsyncCallback(SendCallback), handler);

                        formatter = null;
                    }
                }
            }
            catch (SocketException)
            {
                Logging.Log(handler.RemoteEndPoint.ToString() + " has disconnected", true);    
            }
            catch (Exception ex)
            {
                Logging.Log(ex.ToString(), false);
            }
        }


So what has changed? I mean it is a Common[] object array before it is sent to the server. It has something with the client converting it from the List<> to an Array.

At first evyerhing was an Array. But I found it was more difficult for me removing and determining if something was null. Had issues, so for now I turned it into a List then back to a array before sending.

BTW this was working before I did the List<commons>. When it was just straight Commons[] everywhere it worked fine.

modified on Wednesday, December 1, 2010 6:04 PM

AnswerRe: Can not deserialize Pin
jschell1-Dec-10 12:16
jschell1-Dec-10 12:16 
AnswerRe: Can not deserialize Pin
Luc Pattyn1-Dec-10 12:17
sitebuilderLuc Pattyn1-Dec-10 12:17 
GeneralRe: Can not deserialize Pin
Jacob D Dixon1-Dec-10 12:30
Jacob D Dixon1-Dec-10 12:30 
AnswerRe: Can not deserialize Pin
Luc Pattyn1-Dec-10 13:06
sitebuilderLuc Pattyn1-Dec-10 13:06 
GeneralRe: Can not deserialize Pin
Jacob D Dixon1-Dec-10 14:48
Jacob D Dixon1-Dec-10 14:48 
AnswerRe: Can not deserialize Pin
Luc Pattyn1-Dec-10 15:02
sitebuilderLuc Pattyn1-Dec-10 15:02 
GeneralRe: Can not deserialize Pin
Jacob D Dixon1-Dec-10 16:21
Jacob D Dixon1-Dec-10 16:21 
GeneralRe: Can not deserialize Pin
Luc Pattyn1-Dec-10 16:46
sitebuilderLuc Pattyn1-Dec-10 16:46 
GeneralRe: Can not deserialize [modified] Pin
Jacob D Dixon1-Dec-10 17:11
Jacob D Dixon1-Dec-10 17:11 
GeneralRe: Can not deserialize Pin
Luc Pattyn1-Dec-10 22:22
sitebuilderLuc Pattyn1-Dec-10 22:22 
GeneralRe: Can not deserialize Pin
Jacob D Dixon2-Dec-10 4:57
Jacob D Dixon2-Dec-10 4:57 
AnswerRe: Can not deserialize Pin
Luc Pattyn2-Dec-10 5:19
sitebuilderLuc Pattyn2-Dec-10 5:19 
GeneralRe: Can not deserialize Pin
Jacob D Dixon2-Dec-10 6:09
Jacob D Dixon2-Dec-10 6:09 
GeneralRe: Can not deserialize Pin
Luc Pattyn2-Dec-10 6:30
sitebuilderLuc Pattyn2-Dec-10 6:30 
GeneralRe: Can not deserialize Pin
Jacob D Dixon2-Dec-10 7:56
Jacob D Dixon2-Dec-10 7:56 
GeneralRe: Can not deserialize Pin
Luc Pattyn2-Dec-10 8:20
sitebuilderLuc Pattyn2-Dec-10 8:20 
QuestionTrying to deal with the memory leak in the Flash Player Pin
jbradshaw1-Dec-10 6:09
jbradshaw1-Dec-10 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.