2016-09-03 29 views
0

在控制檯應用程序我沒有得到錯誤,但在Windows窗體應用程序我得到一個錯誤 我使用相同的腳本 控制檯應用程序腳本:在控制檯應用程序我沒有得到錯誤,但在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:\黑客\ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs中27主動 警告CS0162可達代碼 檢測FARATSERVER d:\黑客\ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs中56主動 警告CS0219變量' serverResponse」被分配,但其值是 從未 使用FARATSERVER d:\黑客\ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs中105主動 警告CS0219變量 'sendBytes' 被分配,但其值是 從未 使用FARATSERVER d: \ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 104活動 錯誤CS0117'程序'不包含定義 'broadcast'FARATSERVER D:\ hacks \ MVS \ FARATSERVER \ FARATSERVER \ Form1.cs 121 Activ e

回答

3

broadcast方法不再駐留在Program類中。它現在是Form1類的內部,所以從在的Winforms程序更改線104的代碼:

Program.broadcast(dataFromClient, clNo, true); 

Form1.broadcast(dataFromClient, clNo, true); 

(代碼可以在handleClinet找到=>doChat =>while = >try


更新基於意見從Steve

你的「錯誤」大多是警告,但只有1是錯誤。

警告,您的代碼編譯和工作,但編譯器是「警告」你,你做的事情是不是真的正確/應該改變,如果可能的話。

錯誤確實需要修復。所以當你發佈所有「錯誤」時,你實際上只發布了1個錯誤和4個警告。

+2

如果代碼正常並且我的答案解決了您的問題,請將其標記爲已接受,然後請他/她通過編譯器瞭解警告和錯誤消息之間的區別 – Steve

+0

thx它正在工作 – fastaykoo

+0

@fastaykoo。這向社區表明,答案是正確的(如果其他人有類似的問題並且遇到您的問題)。有關更多信息,請參閱:http://stackoverflow.com/help/someone-answers。 – SynerCoder

相關問題