Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称';监听器&x27;在当前上下文中不存在_C# - Fatal编程技术网

C# 名称';监听器&x27;在当前上下文中不存在

C# 名称';监听器&x27;在当前上下文中不存在,c#,C#,这是我第一次在这里发帖,我对编程还比较陌生,如果这是一个有点愚蠢的问题,我深表歉意 基本上,我正在用C#创建相当简单的异步服务器和客户端应用程序。但是,正如您从下面的代码中看到的,我被告知“当前上下文中不存在名称‘listener’。” 我曾尝试过将私有变为公共,但这没有什么区别。我相信这是一个很明显的问题,但如果有人能给我指出正确的方向,我将不胜感激 提前谢谢 using System; using System.Text; using System.Windows.Forms; using

这是我第一次在这里发帖,我对编程还比较陌生,如果这是一个有点愚蠢的问题,我深表歉意

基本上,我正在用C#创建相当简单的异步服务器和客户端应用程序。但是,正如您从下面的代码中看到的,我被告知“当前上下文中不存在名称‘listener’。”

我曾尝试过将私有变为公共,但这没有什么区别。我相信这是一个很明显的问题,但如果有人能给我指出正确的方向,我将不胜感激

提前谢谢

using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Async_Chat_Server
{
    public partial class Form1 : Form
    {
        Socket socket;
        byte[] buffer = new byte[256];
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        private void StartButton_Click(object sender, EventArgs e)
        {
            try
            {
                // Listen for input coming from any IP address on specified port
                int port = int.Parse(PortTextBox.Text);
                TcpListener listener;
                listener = new TcpListener(System.Net.IPAddress.Any, port);
                listener.Start();
                // Create an event handler for dealing with incoming connections
                listener.BeginAcceptTcpClient(
                new AsyncCallback(AcceptIncomingConnection), listener);
            }
            catch (Exception ex)
            {
                // Update display to show error message
                DisplayTextBox.Text = "Socket connection error:\n" + ex.ToString();
            }
        }
        private void AcceptIncomingConnection(IAsyncResult incomingConnection) // CALLBACK
        {
            // Accept incoming socket connection
            socket = listener.EndAcceptSocket(incomingConnection);
            // Set up an event handler for receiving messages
            Receive();
        }
        private void StopButton_Click(object sender, EventArgs e)
        {
            socket.Close();
            listener.Stop();
        }
        private void SendButton_Click(object sender, EventArgs e)
        {
            // Prepare message
            byte[] messageBytes = Encoding.ASCII.GetBytes(MessageTextBox.Text);
            // Send it
            socket.BeginSend(messageBytes, 0, messageBytes.Length, 0,
            new AsyncCallback(TransmitHandler), socket);
        }
        private void TransmitHandler(IAsyncResult info) // CALLBACK
        {
            int bytesSent = socket.EndSend(info);
        }
        private void Receive()
        {
            socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
            ReceiveHandler, socket);
        }
        private void ReceiveHandler(IAsyncResult messageInfo) // CALLBACK
        {
            // Read message
            int numBytesReceived = socket.EndReceive(messageInfo);
            string message = Encoding.ASCII.GetString(buffer, 0, numBytesReceived);
            // Update display
            DisplayTextBox.Text = message;
            // Reset the event handler for new incoming messages
            Receive();
        }
    }
}
将此行移到方法之外

public partial class Form1 : Form
{
    Socket socket;
    TcpListener listener;
    ...
}
public partial class Form1 : Form
{
    Socket socket;
    TcpListener listener;
    ...
}