在控制台应用程序中我没有收到错误但在Windows窗体应用程序中我收到错误

时间:2016-09-03 11:18:56

标签: c# visual-studio

在控制台应用程序中我没有收到错误但在Windows窗体应用程序中我收到错误 我使用相同的脚本 控制台应用程序脚本:

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

namespace FASERVERCMD
{
class Program
{
    public static Hashtable clientsList = new Hashtable();

    static void Main(string[] args)
    {
        TcpListener serverSocket = new TcpListener(8888);
        TcpClient clientSocket = default(TcpClient);
        int counter = 0;

        serverSocket.Start();
        Console.WriteLine("Chat Server Started ....");
        counter = 0;
        while ((true))
        {
            counter += 1;
            clientSocket = serverSocket.AcceptTcpClient();

            byte[] bytesFrom = new byte[10025];
            string dataFromClient = null;

            NetworkStream networkStream = clientSocket.GetStream();
            networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
            dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

            clientsList.Add(dataFromClient, clientSocket);

            broadcast(dataFromClient + " Joined ", dataFromClient, false);

            Console.WriteLine(dataFromClient + " Joined chat room ");
            handleClinet client = new handleClinet();
            client.startClient(clientSocket, dataFromClient, clientsList);
        }

        clientSocket.Close();
        serverSocket.Stop();
        Console.WriteLine("exit");
        Console.ReadLine();
    }

    public static void broadcast(string msg, string uName, bool flag)
    {
        foreach (DictionaryEntry Item in clientsList)
        {
            TcpClient broadcastSocket;
            broadcastSocket = (TcpClient)Item.Value;
            NetworkStream broadcastStream = broadcastSocket.GetStream();
            Byte[] broadcastBytes = null;

            if (flag == true)
            {
                broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
            }
            else
            {
                broadcastBytes = Encoding.ASCII.GetBytes(msg);
            }

            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
            broadcastStream.Flush();
        }
    }  //end broadcast function
}//end Main class


public class handleClinet
{
    TcpClient clientSocket;
    string clNo;
    Hashtable clientsList;

    public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
    {
        this.clientSocket = inClientSocket;
        this.clNo = clineNo;
        this.clientsList = cList;
        Thread ctThread = new Thread(doChat);
        ctThread.Start();
    }

    private void doChat()
    {
        int requestCount = 0;
        byte[] bytesFrom = new byte[10025];
        string dataFromClient = null;
        Byte[] sendBytes = null;
        string serverResponse = null;
        string rCount = null;
        requestCount = 0;

        while ((true))
        {
            try
            {
                requestCount = requestCount + 1;
                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                rCount = Convert.ToString(requestCount);

                Program.broadcast(dataFromClient, clNo, true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }//end while
    }//end doChat
}
}

windows表单应用程序脚本:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Collections;


namespace FARATSERVER
{
public partial class Form1 : Form
{
    public static Hashtable clientsList = new Hashtable();
    public Form1()
    {
        InitializeComponent();
    }

    private void btnStartserver_Click(object sender, EventArgs e)
    {
        TcpListener serverSocket = new TcpListener(8888);
        TcpClient clientSocket = default(TcpClient);
        int counter = 0;

        serverSocket.Start();
        Console.WriteLine("Chat Server Started ....");
        counter = 0;
        while ((true))
        {
            counter += 1;
            clientSocket = serverSocket.AcceptTcpClient();

            byte[] bytesFrom = new byte[10025];
            string dataFromClient = null;

            NetworkStream networkStream = clientSocket.GetStream();
            networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
            dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

            clientsList.Add(dataFromClient, clientSocket);

            broadcast(dataFromClient + " Joined ", dataFromClient, false);

            Console.WriteLine(dataFromClient + " Joined chat room ");
            handleClinet client = new handleClinet();
            client.startClient(clientSocket, dataFromClient, clientsList);
        }

        clientSocket.Close();
        serverSocket.Stop();
        Console.WriteLine("exit");
        Console.ReadLine();
    }
    public static void broadcast(string msg, string uName, bool flag)
    {
        foreach (DictionaryEntry Item in clientsList)
        {
            TcpClient broadcastSocket;
            broadcastSocket = (TcpClient)Item.Value;
            NetworkStream broadcastStream = broadcastSocket.GetStream();
            Byte[] broadcastBytes = null;

            if (flag == true)
            {
                broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
            }
            else
            {
                broadcastBytes = Encoding.ASCII.GetBytes(msg);
            }

            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
            broadcastStream.Flush();
        }
    }
}
public class handleClinet
{
    TcpClient clientSocket;
    string clNo;
    Hashtable clientsList;

    public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
    {
        this.clientSocket = inClientSocket;
        this.clNo = clineNo;
        this.clientsList = cList;
        Thread ctThread = new Thread(doChat);
        ctThread.Start();
    }

    private void doChat()
    {
        int requestCount = 0;
        byte[] bytesFrom = new byte[10025];
        string dataFromClient = null;
        Byte[] sendBytes = null;
        string serverResponse = null;
        string rCount = null;
        requestCount = 0;

        while ((true))
        {
            try
            {
                requestCount = requestCount + 1;
                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                rCount = Convert.ToString(requestCount);

                Program.broadcast(dataFromClient, clNo, true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }//end while
    }//end doChat
}
}

错误:

  

严重级代码描述项目文件行抑制状态   警告CS0618' TcpListener.TcpListener(int)'已经过时了:'这个   方法已被弃用。请使用TcpListener(IPAddress   相反,localaddr,int port)。   http://go.microsoft.com/fwlink/?linkid=14202' FARATSERVER D:\ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 27有效   警告CS0162无法访问的代码   检测到FARATSERVER D:\ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 56有效   警告CS0219变量' serverResponse'已分配但其值   永远不会   使用FARATSERVER D:\ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 105 Active   警告CS0219变量' sendBytes'已分配,但其值为   决不   使用FARATSERVER D:\ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 104 Active   错误CS0117'程序'不包含的定义   '广播' FARATSERVER D:\ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 121 Active

1 个答案:

答案 0 :(得分:3)

broadcast方法不再存在于Program类中。它现在位于Form1类中,因此更改Winforms程序中第104行的代码:

Program.broadcast(dataFromClient, clNo, true);

Form1.broadcast(dataFromClient, clNo, true);

(代码位于handleClinet => doChat => while => try

根据Steve的建议进行更新:

你的错误"主要是警告,但只有1是错误。

有了警告,你的代码就会编译并运行,但是编译器是"警告"你所做的事情并非真正正确/如果可能应该改变。

错误确实需要修复。所以,当你发布所有"错误"你实际上只发布了1个错误和4个警告。