简体   繁体   English

多客户端服务器应用程序的C#问题

[英]C# problem with multi-clients server application

I have a server application and a client application. 我有一个服务器应用程序和一个客户端应用程序。 I'm using socket to communicate between server and client. 我正在使用套接字在服务器和客户端之间进行通信。 Everything works fine if there's only one client connect: uploading, downloading all work well. 如果只有一个客户端连接,则一切正常:上传,下载一切正常。

But if there's another client connect (I start the client application again, which means there're 2 client apps and 1 server app running on my computer), my server starts to mess up: server doesn't receive file upload from client, client couldn't download from server. 但是,如果还有另一个客户端连接(我再次启动客户端应用程序,这意味着我的计算机上正在运行2个客户端应用程序和1个服务器应用程序),则我的服务器开始混乱:服务器未从客户端,客户端接收文件上传无法从服务器下载。

In server code, I already used multithreading for each client connection so I can't figure out the problem. 在服务器代码中,我已经为每个客户端连接使用了多线程,因此我无法弄清问题所在。 Here is my server code: 这是我的服务器代码:

    private void ServerForm_Load(object sender, System.EventArgs e)
            {

                //...
                Thread th = new Thread(new ThreadStart(ListenForPeers));
                th.Start();
            }

    public void ListenForPeers()
            {
                    serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    serversocket.Blocking = true;
                    IPHostEntry IPHost = Dns.GetHostEntry(server);
                    string[] aliases = IPHost.Aliases;
                    IPAddress[] addr = IPHost.AddressList;
                    IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
                    serversocket.Bind(ipepServer);
                    serversocket.Listen(-1);
                    while (true)
                    {
                        clientsock = serversocket.Accept();
                        if (clientsock.Connected)
                        {
                            total_clients_connected++;
                            AppendText("Client connected...");
                            Thread tc = new Thread(new ThreadStart(listenclient));
                            tc.Start();
                        }
             }


    void listenclient()
        {
             // start communication
        }

Is there something wrong with my server code that makes it unable to become a multi-clients server system? 我的服务器代码是否有问题,使其无法成为多客户端服务器系统? Help is really appreciated. 非常感谢您的帮助。 Thanks in advance. 提前致谢。

It looks like you've defined your clientSocket as a global variable to be used by all server threads, instead you want it to be a local reference for each thread. 看起来您已经将clientSocket定义为所有服务器线程都将使用的全局变量,相反,您希望它成为每个线程的本地引用。 You can do this with a ParameterizedThreadStart: 您可以使用ParameterizedThreadStart执行此操作:

public void listenForPeers()
{
  //Setup the server socket

  while(true){
    Socket newClient = serverSock.Accept();
    if(newClient.Connected){
      Thread tc = new Thread(new ParameterizedThreadStart(listenclient));
      tc.start(newClient);
    }
  }
}

void listenclient(object clientSockObj)
{
  Socket clientSock = (Socket)clientSockObj;

  //communication to client via clientSock.
}

First, you're setting your socket's Blocking property to true. 首先,将套接字的Blocking属性设置为true。 This will block any requests until the previous ones finish... 这将阻止所有请求,直到前一个请求完成为止。

See here: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.blocking(VS.80).aspx 请参阅此处: http : //msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.blocking(VS.80).aspx

If you are in blocking mode, and you make a method call which does not complete immediately, your application will block execution until the requested operation completes. 如果处于阻塞模式,并且进行的方法调用不会立即完成,则您的应用程序将阻塞执行,直到请求的操作完成为止。 If you want execution to continue even though the requested operation is not complete, change the Blocking property to false. 如果即使请求的操作未完成,也要继续执行,请将Blocking属性更改为false。 The Blocking property has no effect on asynchronous methods. Blocking属性对异步方法无效。 If you are sending and receiving data asynchronously and want to block execution, use the ManualResetEvent class. 如果要异步发送和接收数据并希望阻止执行,请使用ManualResetEvent类。

You might want to reconsider doing it this way, and instead use WCF or maybe a web service for uploading the files... it'll be a lot easier, and IIS will handle the threading for you. 您可能需要重新考虑以这种方式进行操作,而不是使用WCF或Web服务来上传文件……这会容易得多,IIS会为您处理线程。 It's pretty easy to write a web service to upload a file... 编写Web服务以上传文件非常容易...

http://www.c-sharpcorner.com/UploadFile/scottlysle/UploadwithCSharpWS05032007121259PM/UploadwithCSharpWS.aspx http://www.c-sharpcorner.com/UploadFile/scottlysle/UploadwithCSharpWS05032007121259PM/UploadwithCSharpWS.aspx

try this 尝试这个

const int noOfClients = 5;

for(int i=0;i<noOfClients;i++)
{
  Thread th = new Thread(new ThreadStart(ListenForPeers));
  th.Start();
}

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

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