Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a small server client code where the client sends a control message to the server to execute a job.

The program flow goes as such

1. client sends a control message and goes into wait till a return message is received.
2. server receives the message and sends an execution "success/failure" message to the client and busy waits till a shutdown(i.e. zero byte) message is received.
3. client receives the "success/failure" and sends a close socket message (zero byte shutdown.send) to server and goes into busy wait till it receives a shutdown message.
4. server receives the shutdown messages, sends a shutdown in return and releases the resources of the socket.
5. client receives the shutdown and releases the resources of the socket.

What I have tried:

the code is as follows
client side
C#
try
            {
                sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect(remoteEP);

                string sent_msg = "RESTART CCMCOM";
                MessageLogTextBox.AppendText("Message sent at " + DateTime.Now + " : " + sent_msg + Environment.NewLine);
                byte[] msg = Encoding.ASCII.GetBytes(sent_msg);
                sock.Send(msg);
                
                Thread.Sleep(500);

                msg = new byte[1024];
                sock.Receive(msg);
                string received_msg = Encoding.ASCII.GetString(msg).Trim();
                MessageLogTextBox.AppendText("Message received at " + DateTime.Now + " : " + received_msg + Environment.NewLine);

                MessageLogTextBox.AppendText(Environment.NewLine);
                MessageLogTextBox.AppendText(Environment.NewLine);


                sock.Shutdown(SocketShutdown.Send);
                while (sock.Receive(msg) != 0) ;
                sock.Disconnect(false);
                sock.Close(); 
            }
            catch (Exception ex)
            {
                MessageLogTextBox.AppendText(ex.Message);
            }


server side
C#
try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);
                outputFile.WriteLine("DCA1_CCMCOM_Remote_Restart_Service started at : {0}", DateTime.Now);

                // Start listening for connections.  
                while (true)
                {
                    //Thread is suspended while waiting for an incoming connection.  
                    handler = listener.Accept();

                    string data = null;

                    // An incoming connection is processed.                 
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);

                    if (bytesRec != 0)
                    {
                        data = Encoding.ASCII.GetString(bytes, 0, bytesRec);

                        if (data.IndexOf("RESTART CCMCOM") > -1)
                        {
                            //search the desired service
                            ServiceController sc = new ServiceController("DCA1_CCMCOM");

                            //if the service is running, stop it
                            if (sc != null && sc.Status.Equals(ServiceControllerStatus.Running))
                                sc.Stop();

                            Thread.Sleep(10);
                            sc.Refresh();

                            //busy wait till it is stopped
                            while (sc != null && !sc.Status.Equals(ServiceControllerStatus.Stopped))
                                sc.Refresh();

                            sc.Start();
                            outputFile.WriteLine("Service restarted at  : {0}", DateTime.Now);
                            handler.Send(Encoding.ASCII.GetBytes("Service Restarted"));

                            //Expecting a close request from client end, busy wait till a zero byte (i.e. close) request is received
                            while ( handler.Receive(bytes) != 0) ;
                            //after receiving the close request, send a return shutdown request to client
                            handler.Shutdown(SocketShutdown.Send);
                            handler.Disconnect(true);
                            handler.Close();
                        }
                    }
                    else
                    {
                        handler.Shutdown(SocketShutdown.Send);
                        handler.Disconnect(true);
                        handler.Close();
                    }
                }
            }
            catch (Exception e)
            {
                outputFile.WriteLine("DCA1_CCMCOM_Remote_Restart_Service stopped at : {0} due to {1}", DateTime.Now, e.Message);
                handler.Shutdown(SocketShutdown.Both);
                handler.Disconnect(true);
                handler.Close();
            }


where am i going wrong?
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900