What am I doing wrong?? Sending serialized objects with Async Sockets.. [Solved]
-
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 {
-
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 {
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.
-
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.
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.
-
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.
Good You did what you want.. Keep it up :)
-
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.
Ahh will do! Thanks for the tip! Sorry I didn't do it before :-(
-
Good You did what you want.. Keep it up :)
Thanks! Took me a little bit to figure it out but then again I've never worked with sockets before.
-
Thanks! Took me a little bit to figure it out but then again I've never worked with sockets before.
I hate programming. I prefer work in country with a pain in my back