简体   繁体   English

传递变量 C# 的问题

[英]Issue with passing a variable C#

I am working on a project where I am going to create a TCP/IP Chat application and I have some problems with passing variables between the classes.我正在开发一个项目,我将在其中创建一个 TCP/IP 聊天应用程序,但在类之间传递变量时遇到了一些问题。 The variable data does not get passed on from the handleClient to the main class where data along with clientSocket is saved to a Hashtable.变量数据不会从handleClient传递到主类,其中数据clientSocket一起保存到 Hashtable。 The Hashtable is later going to be used for broadcasting the message to all clients. Hashtable 稍后将用于向所有客户端广播消息。 The error i get is as follows: "Error: The name 'data' does not exist in the current context"我得到的错误如下:“错误:当前上下文中不存在名称‘数据’”

Serverside code:服务端代码:

using System;
using System.IO;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections;

namespace Chat_server
{
public class Program
{
    //Creates a list with all clients
    public static Hashtable clientList = new Hashtable();

    public void Main(string[] args)
    {

        TcpListener serverSocket = null;
        // Set the TcpListener on port 13000.
        Int32 port = 13000;
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");

        // TcpListener server = new TcpListener(port);
        serverSocket = new TcpListener(localAddr, port);

        // Start listening for client requests.
        serverSocket.Start();


        int counter = 0;
        while (true)
        {
            counter += 1;
            Console.WriteLine("Waiting for a connection... ");
            TcpClient clientSocket = serverSocket.AcceptTcpClient();
            clientList.Add(data, clientSocket);
            Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) +   "       started!");
            handleClient hclient = new handleClient();
            hclient.startClient(clientSocket);

        }

    }
}
}
//Class to handle each connection separatly
public class handleClient
{
    TcpClient clientSocket;
    Hashtable clientList;

    public void startClient(TcpClient inClient)
    {
        this.clientSocket = inClient;
        Thread clientThread = new Thread(new ThreadStart(doChat));
        clientThread.Start();
    }

    public void doChat()
    {
        Byte[] bytes = new Byte[10025];
        String data = null;
        data = null;

        while ((true))
        {
            try
            {
                foreach (DictionaryEntry Item in clientList)
                {
                data = null;
                // Get a stream object for reading and writing
                NetworkStream stream = clientSocket.GetStream();

                int i;

                // Loop to receive all the data sent by the client. 
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine("Received: {0}", data);

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    stream.Flush();

                    Console.WriteLine("Sent: {0}", data);
                }

                // Shutdown and end connection
                clientSocket.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {

            }


        }
    }
}

data isn't defined in that function or in the class at:数据未在该函数或类中定义:

clientList.Add(data, clientSocket); clientList.Add(data, clientSocket);

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

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