Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone tell me how can I use if condition in this program?

For example if client sends "hello" then server response is "hi" and if client send "how r u" then server send "i am fine".
I have this code and I am trying to do that, but it does not work.

Server program
C#
 public Socket workSocket = null;

           public const int BufferSize = 1024;

            public byte[] buffer = new byte[BufferSize];

            public StringBuilder sb = new StringBuilder();

        }
        public class AsynchronousSocketListener
        {

            public static ManualResetEvent allDone = new ManualResetEvent(false);

public AsynchronousSocketListener()
            {

            }



            public static void StartListening()
            {

                byte[] bytes = new Byte[1024];

                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());

                IPAddress ipAddress = ipHostInfo.AddressList[0];

                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8888);

                Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);


                try
                {

                    listener.Bind(localEndPoint);

                    listener.Listen(100);



                    while (true)
                    {

                        allDone.Reset();


                        Console.WriteLine("Waiting for a connection...");

                        listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);


                        allDone.WaitOne();

                    }



                }
                catch (Exception e)
                {

                    Console.WriteLine(e.ToString());

                }



                Console.WriteLine("\nPress ENTER to continue...");

                Console.Read();

            }



            public static void AcceptCallback(IAsyncResult ar)
            {

                allDone.Set();


                Socket listener = (Socket)ar.AsyncState;

                Socket handler = listener.EndAccept(ar);


                StateObject state = new StateObject();

                state.workSocket = handler;

                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state);

            }



            public static void ReadCallback(IAsyncResult ar)
            {

                String content = String.Empty;


                StateObject state = (StateObject)ar.AsyncState;

                Socket handler = state.workSocket;


                int bytesRead  = handler.EndReceive(ar);



                if (bytesRead > 0)
                {


                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));




                    content = state.sb.ToString();

                    if (content.IndexOf("<eof>") > -1)
                    {
                        if (content == "hello")
                        {
                            Console.WriteLine(content);
                            Send(handler, content);
                        }
                        else if (content == "How r u")
                        {
                            Console.WriteLine(content);
                            Send2(handler, content);
                        }
                    }
                    else
                    {

                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state);

                    }
                    

                }

            }

            private static void Send(Socket handler, String data)
            {
                data = "Hi";

                byte[] byteData = Encoding.ASCII.GetBytes(data);

                handler.BeginSend(byteData, 0, byteData.Length, 0,new AsyncCallback(SendCallback), handler);

            }


            private static void Send2(Socket handler, String data)
            {
                data = "fine";

                byte[] byteData = Encoding.ASCII.GetBytes(data);




         handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);

            }

            private static void SendCallback(IAsyncResult ar)
            {

                try
                {

                    Socket handler = (Socket)ar.AsyncState;

                    int bytesSent = handler.EndSend(ar);

                    handler.Shutdown(SocketShutdown.Both);     

                     handler.Close();



                }
                catch (Exception e)
                {

                    Console.WriteLine(e.ToString());

                }

            }


            public static int Main(String[] args)
            {

                StartListening();

                return 0;

            }

        }
Posted
Updated 19-Dec-12 21:36pm
v4
Comments
ridoy 20-Dec-12 1:40am    
what program!!??

This is basic homework. No-one is going to write this for you. Do your own homework.
 
Share this answer
 
Comments
Thomas Daniels 20-Dec-12 5:40am    
Good answer, +5!
Killzone DeathMan 20-Dec-12 9:46am    
Agree
No, we won't give you solution.. We can't work out for you like this.. You should try it by yourself.. If you got stuck somewhere, you can certainly come here.. But don't ask directly for code..
-Krunal R.
 
Share this answer
 
Comments
Thomas Daniels 20-Dec-12 5:40am    
Good answer, +5!
Jibesh 20-Dec-12 6:05am    
Guys. how many templates I could keep in my database as a reply/solution for such Question. lets create a voting!! so that we can maintain a uniformity in answer???
I solved it. I just have to put < eof > at the end of 'hello' like "hello< eof >" and same to how r u. Its work now.
 
Share this answer
 

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