简体   繁体   English

C#套接字:第二个(及更多)用户无法连接

[英]C# Sockets: 2nd (and more) users can't connect

I connect to my (C#) server and from an App built in Corona SDK but for the second person can never connect. 我连接到(C#)服务器并通过Corona SDK内置的应用程序进行连接,但是对于第二个人而言,它永远无法连接。

I have tried using different IP's ie two cellphones with external IP's with no difference. 我尝试使用不同的IP,即两部手机与外部IP没什么区别。

This is how my server listener works: 这是我的服务器侦听器的工作方式:

server.cs server.cs

void Listener()
    {
        while (isRunning)
        {
            try
            {
                Socket socket = listener.AcceptSocket();

                foreach (var worker in workers)
                    if (worker.IsAvailable)
                    {
                        worker.ProcessNewSocket(socket);
                        break;
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

    public void Run()
    {
        Console.WriteLine("Server.Run()");
        listener.Start();
        isRunning = true;

        foreach (var worker in workers)
        {
            Thread t = new Thread(worker.Run);
            t.Start();
        }

        Listener();
    }

ServerWorker.cs ServerWorker.cs

public void ProcessNewSocket(Socket socket)
    {
        var pc = new PlayerClient(this);            
        sockets.Add(socket, pc);
    }

    // this method will be called in cycle
    public void Run()
    {

        while (server.IsRunning)
        {
            List<Socket> readList = sockets.Keys.ToList(); //List of sockets that have some data from client available for reading.
            List<Socket> writeList = sockets.Keys.ToList(); //List of sockets that are ready to write (send) to the client. An action was made to a table and the change was sent to PlayerClient.Write and is now stored in the queue (MemoreStream)
            List<Socket> errorList = sockets.Keys.ToList();

            if (readList.Count() != 0 || writeList.Count() != 0 || errorList.Count() != 0)
            {
                // for providing security you can use  System.Net.Security.SslStream here when read/write data,
                // see http://msdn.microsoft.com/ru-ru/library/system.net.security.sslstream(v=vs.110).aspx

                Socket currentSocket = null;

                // foreach socket with events
                try
                {
                    foreach (var s in readList)
                    {
                        currentSocket = s;
                        //TODO: Get the actual length of the message.
                        byte[] data = new byte[2048];
                        s.Receive(data);
                        sockets[s].OnData(data);
                    }

                    foreach (var s in writeList)
                    {
                        currentSocket = s;
                        if (sockets[s].IsWriteDataAvailable())
                        {
                            s.Send(sockets[s].GetWriteBuffer());
                            sockets[s].ClearWriteBuffer();
                        }
                    }

                    foreach (var s in errorList)
                    {
                        //OnError   
                    }
                }
                // we got exception, depending on the type...
                catch (SocketException ex)
                {
                    //Console.WriteLine(ex.ToString());
                    // send client error message, this is not always possible(for example network failure)
                    // maybe we would like to notify his opponent about connection failure
                    // terminate connection

                    if (ex.ErrorCode == (int)SocketError.ConnectionAborted || ex.ErrorCode == (int)SocketError.ConnectionReset)
                        RemoveSocket(currentSocket);
                    else
                        Console.WriteLine("Other problem .. " + ex.ErrorCode.ToString());
                }
            }

        }    

    }

I'm new in network programming so I'm not really sure what to do. 我是网络编程的新手,所以我不确定该怎么做。 I have read about using ASync but first I would like to know if there is something I can do with this code and/or if I should change it completely? 我已经阅读过有关使用ASync的信息,但首先我想知道是否可以使用此代码执行某些操作和/或是否应该完全更改它?

I think the "BREAK" statement in your Listener() Block; 我认为您的Listener()块中的“ BREAK”语句;

           foreach (var worker in workers)

           if (worker.IsAvailable)
                {
                    worker.ProcessNewSocket(socket);
                    break; // this BREAK WILL END YOUR loop AFTER first CLIENT FOUND.
                }

So, try removing break as; 因此,尝试删除break as;。

           foreach (var worker in workers)
           {
              if (worker.IsAvailable)
               {    
                    worker.ProcessNewSocket(socket);
               }
           }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM