异步客户端服务器通信问题

时间:2012-12-04 18:36:40

标签: c# asynchronous client-server

我是编程新手,我正在开发异步客户端服务器应用程序。 我可以从客户端向服务器发送消息,但是当我收到数据到服务器(OnDataReceived Method)并尝试将相同的数据发送回客户端(用于测试目的)时,我不能。 不确定我需要提供什么其他信息,所以请让我知道,我不是故意模糊。

服务器代码

public void OnDataReceived(IAsyncResult asyncResult)
    {
        try
        {
            SocketPacket socketData = (SocketPacket)asyncResult.AsyncState;
            int iRx = 0;
            iRx = socketData.currentSocket.EndReceive(asyncResult);
            char[] chars = new char[iRx];
            Decoder decoder = Encoding.UTF8.GetDecoder();
            int charLen = decoder.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
            String receivedData = new String(chars);

            //BroadCast(receivedData);

            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => lbxMessages.Items.Add(receivedData)));

            //Updated Code
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => broadcast(receivedData)));

            WaitForData(socketData.currentSocket);


        }
        catch (ObjectDisposedException)
        {
            System.Diagnostics.Debugger.Log(0, "1", "\n OnDataRecieved: Socket has been closed\n");
        }
        catch (SocketException se)
        {
            MessageBox.Show(se.Message);
        }
    }

public class SocketPacket
    {
        public Socket currentSocket;
        public byte[] dataBuffer = new byte[50];//allowing the 50 digist to be sent at once
    }

    private void WaitForData(Socket socket)
    {
        try
        {
            if (workerCallBack == null)
            {
                workerCallBack = OnDataReceived;
            }
            SocketPacket sckPack = new SocketPacket();
            sckPack.currentSocket = socket;
            socket.BeginReceive(sckPack.dataBuffer, 0, sckPack.dataBuffer.Length, SocketFlags.None, workerCallBack, sckPack);
        }
        catch(SocketException se)
        {
            MessageBox.Show(se.Message);
        }
    }

更新以回应Andrew的回复

我有一个在客户端连接时会调用的方法

private void OnClientConnect(IAsyncResult asyncResult)
    {
        try
        {
            //Here we complete/end the Beginaccept() asynchronous call by
            //calling EndAccept() - which returns the reference to a new socket object
            workerSocket[clientCount] = listenSocket.EndAccept(asyncResult);

            //Let the worker socket do the further processing for the just connected client
            WaitForData(workerSocket[clientCount]);

            //Now increment the client count
            ++clientCount;

            if (clientCount<4)//allow max 3 clients
            {
                //Adds the connected client to the list
                connectedClients.Add(listenSocket);
                String str = String.Format("Client # {0} connected", clientCount);                   

                this.Dispatcher.Invoke((Action)(() =>
                {
                    //Display this client connection as a status message on the GUI
                    lbxMessages.Items.Add(str);
                    lblConnectionStatus.Content =clientCount + " Connected";
                }));

                //Since the main Socket is now free, it can go back and wait for
                //other clients who are attempting to connect
                listenSocket.BeginAccept(OnClientConnect, null);
            }
        }
        catch (ObjectDisposedException)
        {
            System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
        }
        catch (SocketException)
        {
            HandleClientDisconnect(listenSocket);
        }
    }

(修订版) 广播服务器收到的广播消息的方法返回客户端

public void broadcast(string msg)
    {
        //foreach (Socket item in connectedClients)
        //{
        Socket broadcastSocket;
        broadcastSocket = workerSocket[0]; //sends message to first client connected
        byte[] broadcastBytes = null;
        broadcastBytes = Encoding.ASCII.GetBytes(msg);
        broadcastSocket.Send(broadcastBytes);
        //}
    }

2 个答案:

答案 0 :(得分:0)

服务器端TCP通信涉及两个套接字。第一个套接字是一个侦听套接字,您只能将其用于接受新请求。然后,每当您接受来自客户端的新请求时,您将为每个连接获得另一个套接字。

您尝试通过侦听套接字发回数据,但不是通过您接受的套接字发送数据。

答案 1 :(得分:0)

谢尔盖说得对。如果您希望有一个服务器处理多个客户端,那么您将需要某种ServerTerminal类,它可以侦听新连接,然后设置某种“connectedclient”类来处理该套接字的IO。您的“OnDataReceived”方法将在connectedclient类中。

在套接字接受例程中,它应该类似于:

private void OnClientConnection(IAsyncResult asyn)
    {
        if (socketClosed)
        {
            return;
        }

        try
        {
            Socket clientSocket = listenSocket.EndAccept(asyn);

            ConnectedClient connectedClient = new ConnectedClient(clientSocket, this, _ServerTerminalReceiveMode);

            connectedClient.StartListening();

在接受例程中,您传递了一个套接字 - 我将其命名为“clientSocket”。这是您要写入的套接字,而不是侦听套接字。