Click here to Skip to main content
15,913,709 members
Home / Discussions / C#
   

C#

 
QuestionGet min and max color from a range of RGB colors from listbox Pin
pancakeleh6-Nov-10 8:01
pancakeleh6-Nov-10 8:01 
AnswerRe: Get min and max color from a range of RGB colors from listbox Pin
PIEBALDconsult6-Nov-10 8:17
mvePIEBALDconsult6-Nov-10 8:17 
AnswerRe: Get min and max color from a range of RGB colors from listbox Pin
Luc Pattyn6-Nov-10 8:33
sitebuilderLuc Pattyn6-Nov-10 8:33 
AnswerRe: Get min and max color from a range of RGB colors from listbox Pin
pancakeleh6-Nov-10 8:58
pancakeleh6-Nov-10 8:58 
GeneralRe: Get min and max color from a range of RGB colors from listbox Pin
_Erik_6-Nov-10 10:00
_Erik_6-Nov-10 10:00 
GeneralRe: Get min and max color from a range of RGB colors from listbox [modified] Pin
pancakeleh6-Nov-10 19:59
pancakeleh6-Nov-10 19:59 
GeneralRe: Get min and max color from a range of RGB colors from listbox Pin
Dave Kreskowiak7-Nov-10 2:17
mveDave Kreskowiak7-Nov-10 2:17 
QuestionWhat am I doing wrong?? Sending serialized objects with Async Sockets.. [Solved] Pin
Jacob D Dixon6-Nov-10 7:53
Jacob D Dixon6-Nov-10 7:53 
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
         {
             if (state.ms.Length > 0)
             {
                 state.ms.Seek(0, 0);
                 IFormatter formatter = new BinaryFormatter();
                 Operations ops = (Operations)formatter.Deserialize(state.ms);
                 Console.WriteLine("Got: " + ops.Task.ToString());
                 Console.ReadKey();
             }

             receiveDone.Set();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
         Console.ReadKey();
     }
 }


Server:
private void StartListening(object p)
       {
           int port = Convert.ToInt32(p);
           try
           {
               // Create socket and bind to local adapter
               socketlistener = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                   ProtocolType.Tcp);
               IPEndPoint iep = new IPEndPoint(IPAddress.Any, port);
               socketlistener.Bind(iep);
               socketlistener.LingerState.Enabled = false;
               socketlistener.Listen(100);

               // Log out information
               Logging.Debug("Socket initialized and listening on port " + p.ToString());

               while (true)
               {
                   allDone.Reset();
                   socketlistener.BeginAccept(new AsyncCallback(AcceptClient), socketlistener);
                   allDone.WaitOne();
               }
           }
           catch (Exception ex)
           {
               Logging.Log(ex.ToString());
           }
       }

       private void AcceptClient(IAsyncResult iar)
       {
           allDone.Set();
           try
           {
               Socket old = iar.AsyncState as Socket;
               Socket client = old.EndAccept(iar);

               Logging.Debug("Client (" + client.RemoteEndPoint.ToString() + ") connected");

               StateObject state = new StateObject();
               state.workSocket = client;
               client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                   new AsyncCallback(GetClientData), state);

           }
           catch (Exception ex)
           {
               Logging.Log(ex.ToString());
           }
       }

       private void GetClientData(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(GetClientData), state);
               }
               else
               {
                   if (state.ms.Length > 0)
                   {
                       state.ms.Seek(0, 0);
                       IFormatter formatter = new BinaryFormatter();
                       Operations ops = (Operations)formatter.Deserialize(state.ms);

                       Logging.Debug("Client (" + client.RemoteEndPoint.ToString() + ") sent action " + ops.Task.ToString() +
                           " with " + Convert.ToInt32(state.ms.Length) + " bytes of data");

                       Actions.PerformAction(ops);
                       ops = null;
                       state = null;

                       // Send data to client
                       Send(client);
                   }

                   Logging.Debug("Client (" + client.RemoteEndPoint.ToString() + ") is disconnecting.");
                   client.Shutdown(SocketShutdown.Both);
                   client.Close();
               }
           }
           catch (Exception ex)
           {
               Logging.Log(ex.ToString());
           }
       }

       private void Send(Socket client)
       {
           try
           {
               Operations ops = new Operations();
               ops.Task = Operations.Tasks.UPDATE_DEVICES;
               ops.NetBios = Environment.MachineName;

               Logging.Debug("Sending task (" + ops.Task.ToString() + ") to client " + client.RemoteEndPoint.ToString());

               IFormatter formatter = new BinaryFormatter();
               NetworkStream ns = new NetworkStream(client);
               formatter.Serialize(ns, ops);

               ns.Flush();
               ns.Close();

               client.Disconnect(false);
               client.Close();
           }
           catch (Exception ex)
           {
               Logging.Log(ex.ToString());
           }
       }


So what am I doing wrong here? I am trying to get the client to send info to the server (this works if I don't put the code on the client for getting info from the server) and when the server gets it, it needs to send information back to the CLIENT. (this doesn't work).

So Client contacts server and says "Here is the information", the server puts it in a database (not shown), then the server sends information back saying "This is the actions you need to perform right now".
AnswerRe: What am I doing wrong?? Sending serialized objects with Async Sockets.. Pin
Jacob D Dixon7-Nov-10 15:25
Jacob D Dixon7-Nov-10 15:25 
GeneralRe: What am I doing wrong?? Sending serialized objects with Async Sockets.. Pin
Peter_in_27807-Nov-10 15:31
professionalPeter_in_27807-Nov-10 15:31 
GeneralRe: What am I doing wrong?? Sending serialized objects with Async Sockets.. Pin
Jacob D Dixon8-Nov-10 17:56
Jacob D Dixon8-Nov-10 17:56 
GeneralRe: What am I doing wrong?? Sending serialized objects with Async Sockets.. Pin
RaviRanjanKr7-Nov-10 16:47
professionalRaviRanjanKr7-Nov-10 16:47 
GeneralRe: What am I doing wrong?? Sending serialized objects with Async Sockets.. Pin
Jacob D Dixon8-Nov-10 17:57
Jacob D Dixon8-Nov-10 17:57 
GeneralRe: What am I doing wrong?? Sending serialized objects with Async Sockets.. Pin
Member 1841422-Mar-11 22:48
Member 1841422-Mar-11 22:48 
QuestionUSer Control Error Pin
SRJ925-Nov-10 17:19
SRJ925-Nov-10 17:19 
AnswerRe: USer Control Error Pin
OriginalGriff5-Nov-10 22:54
mveOriginalGriff5-Nov-10 22:54 
AnswerRe: USer Control Error Pin
_Erik_5-Nov-10 23:15
_Erik_5-Nov-10 23:15 
QuestionSocket error on service stop Pin
Jacob D Dixon5-Nov-10 14:40
Jacob D Dixon5-Nov-10 14:40 
AnswerRe: Socket error on service stop Pin
RaviRanjanKr5-Nov-10 19:08
professionalRaviRanjanKr5-Nov-10 19:08 
GeneralRe: Socket error on service stop [modified] Pin
Jacob D Dixon6-Nov-10 5:32
Jacob D Dixon6-Nov-10 5:32 
QuestionUnit testing Pin
Tichaona J5-Nov-10 7:51
Tichaona J5-Nov-10 7:51 
AnswerRe: Unit testing Pin
fjdiewornncalwe5-Nov-10 8:12
professionalfjdiewornncalwe5-Nov-10 8:12 
AnswerRe: Unit testing Pin
Dave Kreskowiak5-Nov-10 9:43
mveDave Kreskowiak5-Nov-10 9:43 
GeneralRe: Unit testing Pin
Tichaona J5-Nov-10 12:00
Tichaona J5-Nov-10 12:00 
GeneralRe: Unit testing Pin
Pete O'Hanlon5-Nov-10 12:07
mvePete O'Hanlon5-Nov-10 12:07 

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.