简体   繁体   中英

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. 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. The Hashtable is later going to be used for broadcasting the message to all clients. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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