如果在这个程序中的条件 [英] if condition in this program

查看:92
本文介绍了如果在这个程序中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有谁能告诉我如何使用此程序中的条件?



例如,如果客户端发送hello,则服务器响应为hi,如果客户端发送how ru,则服务器发送我很好。

我有此代码,我正在尝试这样做,但它不起作用。



服务器程序

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

 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;

            }

        }

推荐答案

这是基本的功课。没有人会为你写这个。做你自己的作业。
This is basic homework. No-one is going to write this for you. Do your own homework.


不,我们不会给你解决方案..我们不能为你这样做..你应该自己尝试..如果你被困在某个地方,你当然可以来这里..但是不要直接问代码..

-Krunal R。
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.


我解决了。我只需要把< eof>在''hello''结尾处,如hello< eof>和你一样。它的工作现在。
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.


这篇关于如果在这个程序中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆