Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What am I doing wrong?? Sending serialized objects with Async Sockets.. [Solved]

What am I doing wrong?? Sending serialized objects with Async Sockets.. [Solved]

Scheduled Pinned Locked Moved C#
csharpsysadminhelpquestionworkspace
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jacob D Dixon
    wrote on last edited by
    #1

    Ok I decided to give it a try using Async sockets. I'm trying to get the client and server to talk. Right now I can get the SERVER to receive data from the client.. but when I try to get the server to send data back to the client it doesn't work!! Client:

       static void Checkin()
        {
            IPEndPoint endPoint = new IPEndPoint(serverIp, 5555);
            Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sendSocket.Connect(endPoint);
    
            Operations ops = new Operations();
            ops.Task = Operations.Tasks.CHECKIN;
            ops.NetBios = System.Environment.MachineName;
            ops.ClientId = 1;
    
            IFormatter formatter = new BinaryFormatter();
            NetworkStream ns = new NetworkStream(sendSocket);
            formatter.Serialize(ns, ops);
    
            ns.Flush();
            ns.Close();
    
            // IF I DON'T PUT THE CODE BELOW THIS LINE THEN THE SERVER WILL RECEIVE THE DATA LIKE NORMAL
            // THE PROBLEM IS WHEN I PUT THIS CODE THE SERVER DOESN'T EVEN RECEIVE
    
            Console.WriteLine("Beginning to get data");
            Receive(sendSocket);
            receiveDone.WaitOne();
    
            sendSocket.Shutdown(SocketShutdown.Both);
            sendSocket.Close();
        }
        private static void Receive(Socket client)
        {
            try
            {
                // Create the state object.
                StateObject state = new StateObject();
                state.workSocket = client;
    
                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        static void ReceiveCallback(IAsyncResult iar)
        {
            StateObject state = iar.AsyncState as StateObject;
            Socket client = state.workSocket;
    
            try
            {
                int recv = client.EndReceive(iar);
    
                if (recv > 0)
                {
                    state.ms.Write(state.buffer, 0, recv);
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
    
    J 1 Reply Last reply
    0
    • J Jacob D Dixon

      Ok I decided to give it a try using Async sockets. I'm trying to get the client and server to talk. Right now I can get the SERVER to receive data from the client.. but when I try to get the server to send data back to the client it doesn't work!! Client:

         static void Checkin()
          {
              IPEndPoint endPoint = new IPEndPoint(serverIp, 5555);
              Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              sendSocket.Connect(endPoint);
      
              Operations ops = new Operations();
              ops.Task = Operations.Tasks.CHECKIN;
              ops.NetBios = System.Environment.MachineName;
              ops.ClientId = 1;
      
              IFormatter formatter = new BinaryFormatter();
              NetworkStream ns = new NetworkStream(sendSocket);
              formatter.Serialize(ns, ops);
      
              ns.Flush();
              ns.Close();
      
              // IF I DON'T PUT THE CODE BELOW THIS LINE THEN THE SERVER WILL RECEIVE THE DATA LIKE NORMAL
              // THE PROBLEM IS WHEN I PUT THIS CODE THE SERVER DOESN'T EVEN RECEIVE
      
              Console.WriteLine("Beginning to get data");
              Receive(sendSocket);
              receiveDone.WaitOne();
      
              sendSocket.Shutdown(SocketShutdown.Both);
              sendSocket.Close();
          }
          private static void Receive(Socket client)
          {
              try
              {
                  // Create the state object.
                  StateObject state = new StateObject();
                  state.workSocket = client;
      
                  // Begin receiving the data from the remote device.
                  client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                      new AsyncCallback(ReceiveCallback), state);
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.ToString());
              }
          }
          static void ReceiveCallback(IAsyncResult iar)
          {
              StateObject state = iar.AsyncState as StateObject;
              Socket client = state.workSocket;
      
              try
              {
                  int recv = client.EndReceive(iar);
      
                  if (recv > 0)
                  {
                      state.ms.Write(state.buffer, 0, recv);
                      client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                          new AsyncCallback(ReceiveCallback), state);
                  }
                  else
                  {
      
      J Offline
      J Offline
      Jacob D Dixon
      wrote on last edited by
      #2

      Figured it out... I wasn't paying attention... In my code I was only reading the first 1024 bytes. So if it was under 1024 bytes it would work fine. I put in code to check if the amount read equaled the buffer size and then called the beginreceive again until we read all the data.

      P R 2 Replies Last reply
      0
      • J Jacob D Dixon

        Figured it out... I wasn't paying attention... In my code I was only reading the first 1024 bytes. So if it was under 1024 bytes it would work fine. I put in code to check if the amount read equaled the buffer size and then called the beginreceive again until we read all the data.

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        Just a friendly suggestion. When you answer your own question like this (or in fact, if anyone answers it well), add "[solved]" to the subject of the original. Helps people searching the archives. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

        J 1 Reply Last reply
        0
        • J Jacob D Dixon

          Figured it out... I wasn't paying attention... In my code I was only reading the first 1024 bytes. So if it was under 1024 bytes it would work fine. I put in code to check if the amount read equaled the buffer size and then called the beginreceive again until we read all the data.

          R Offline
          R Offline
          RaviRanjanKr
          wrote on last edited by
          #4

          Good You did what you want.. Keep it up :)

          J 1 Reply Last reply
          0
          • P Peter_in_2780

            Just a friendly suggestion. When you answer your own question like this (or in fact, if anyone answers it well), add "[solved]" to the subject of the original. Helps people searching the archives. Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994.

            J Offline
            J Offline
            Jacob D Dixon
            wrote on last edited by
            #5

            Ahh will do! Thanks for the tip! Sorry I didn't do it before :-(

            1 Reply Last reply
            0
            • R RaviRanjanKr

              Good You did what you want.. Keep it up :)

              J Offline
              J Offline
              Jacob D Dixon
              wrote on last edited by
              #6

              Thanks! Took me a little bit to figure it out but then again I've never worked with sockets before.

              U 1 Reply Last reply
              0
              • J Jacob D Dixon

                Thanks! Took me a little bit to figure it out but then again I've never worked with sockets before.

                U Offline
                U Offline
                User 183941
                wrote on last edited by
                #7

                I hate programming. I prefer work in country with a pain in my back

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups