0

我正在尝试在 wp7(客户端)和 WPF(服务器)上开发基于套接字的应用程序,但我遇到了我不明白的问题。

我编写了“服务器”类,它应该处理与客户端的连接和接收字符串。

问题是服务器只接收客户端发送的第一个字符串,然后连接中断,我必须重置我的客户端应用程序(仅限客户端)。我假设这是服务器端问题,因为我正在使用异步调用重写服务器应用程序。在该客户端运行良好之前。我的服务器端代码:

public class StateObject
    {
        public byte[] Buffer { get; set; }
        public Socket WorkSocket { get; set; }
    }

    public class MessageRecievedEventArgs : EventArgs
    {
        public string Message { get; set; }
    }

    public class Server
    {
        ManualResetEvent _done;
        TcpListener _listener;

        public event EventHandler<MessageRecievedEventArgs> OnMessageRecieved;
        public Server()
        {
            _done = new ManualResetEvent(false);
            _listener = new TcpListener(IPAddress.Any, 4124);
        }

        public void Start()
        {
            Thread th = new Thread(StartListening);
            th.IsBackground = true;
            th.Start();
        }

        private void StartListening()
        {
            _listener.Start();
            while (true)
            {
                _done.Reset();
                _listener.BeginAcceptTcpClient(new AsyncCallback(OnConnected), _listener);
                _done.WaitOne();
            }

        }
        private void OnConnected(IAsyncResult result)
        {
            TcpListener listener = result.AsyncState as TcpListener;
            Socket socket = listener.EndAcceptSocket(result);
            byte[] buffer = new byte[256];
            StateObject state = new StateObject { Buffer = buffer, WorkSocket = socket };
            socket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, new AsyncCallback(OnRead), state);
        }

        private void OnRead(IAsyncResult result)
        {
            var state = (StateObject)result.AsyncState;
            int buffNum = state.WorkSocket.EndReceive(result);
            string message = Encoding.UTF8.GetString(state.Buffer, 0, buffNum);
            if (OnMessageRecieved != null)
            {
                MessageRecievedEventArgs args = new MessageRecievedEventArgs();
                args.Message = message;
                OnMessageRecieved(this, args);
            }
            _done.Set();
        }
    }

客户:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                base.OnNavigatedTo(e);
                _socketEventArgs = new SocketAsyncEventArgs() { RemoteEndPoint = App.Connection.RemoteEndPoint };
                Send("{ECHO}");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void Send(string key)
        {
            var bytes = Encoding.UTF8.GetBytes(key + "$");
            _socketEventArgs.SetBuffer(bytes, 0, bytes.Count());
            if (Socket.Connected)
                Socket.SendAsync(_socketEventArgs);
            else
                MessageBox.Show("Application is not connected. Please reset connection (press 'back' key and 'connect' button). It may be needed to restart server application"); 
        }

“{ECHO}”消息由客户端发送并由服务器接收 - 每个下一个都发送,但不接收。我假设我不了解套接字异步调用机制......有人可以启发我吗?:)

4

1 回答 1

0

好像你只读过一次。可能您想反复调用 read 以耗尽整个流。

于 2012-09-11T11:25:53.027 回答