C#:如何将套接字保持在列表模式以处理多个传入请求 [英] C#: How to keep Socket in Listning mode for multiple incomming request

查看:70
本文介绍了C#:如何将套接字保持在列表模式以处理多个传入请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#asp.net中将Socket保持在列表模式以处理多个传入请求
任何帮助将不胜感激:)

我正在使用tcpclient和AcceptSocket()方法将字符串发送到服务器,这里的第一次迭代工作正常...但是我很困惑,这是发送多个字符串的正确方法,套接字将保持在列表模式. br/>
这是我的代码.

How to keep Socket in Listning mode for multiple incomming request in c# asp.net
any help would br appreciated :)

I am sendng string to server using tcpclient and AcceptSocket() methods, here the first iteration is working fine...but i am quite confused which would be the right way to send the multiple string and socket would stay in listning mode..

Here is my code..

static void Main(string[] args)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("172.29.32.37");
                TcpListener myList = new TcpListener(ipAd, 5000);
                
                //start Listening
                myList.Start();

                Console.WriteLine("The server is running at port 5000...");
                Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");

                Socket s = myList.AcceptSocket();                
                
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved...");
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(b[i]));

                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("The string was recieved by the server."));
                Console.WriteLine("\nSent Acknowledgement");                
                
                s.Close();
                myList.Stop();
                Console.ReadLine();                
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }

推荐答案

在您的Accept处理程序中,您需要将套接字重新设置为接受模式:

In your Accept handler, you need to put the socket back into accepting mode:

void AcceptCallback(IAsyncResult ar) {
  Socket server = (Socket) ar.AsyncState;
  Socket clientSocket = server.EndAccept(ar);
  
  // Start the thing listening again
  server.BeginAccept(new AsyncCallback(AcceptCallback), server);

  // ... other processing with the new client
}



您可能需要try/catch和一些日志代码,因为如果异步回调中的任何内容引发了转义的异常,尝试处理它确实是令人不快的.



You probably want a try/catch and some logging code because if anything in an asynchronous callback throws an exception that escapes, it''s really unpleasant trying to deal with it.


您的套接字变量已经在第二次调用listen()时为其分配了侦听套接字,这就是为什么它告诉您仅允许一种用法的原因.您需要重复的只是socket.BeginAccept(new AsyncCallback(acceptAsync),socket)调用.因此,尝试用socket.BeginAccept(new AsyncCallback(acceptAsync),socket)替换在receiveAsync(...)方法内对listen()的调用


http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms739168(v=vs.85).aspx [
Your socket variable already has an listening socket assigned to it the second time you call listen(), which is why it tells you only one usage is permitted. All you need to repeat is the socket.BeginAccept(new AsyncCallback(acceptAsync), socket) call. So try replacing the call to listen() inside your receiveAsync(...) method with socket.BeginAccept(new AsyncCallback(acceptAsync), socket)


http://msdn.microsoft.com/en-us/library/windows/desktop/ms739168(v=vs.85).aspx[^]


这篇关于C#:如何将套接字保持在列表模式以处理多个传入请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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