2016-06-13 103 views
-3

此代碼有一個錯誤:method must have a return type。我該如何解決它?C#代理窗體窗體應用程序

public Server() 
{ 
} 

誤差以上

服務器字
public void createListener() 
{ 
    TcpListener tcpListener = null; 
    IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0]; 

    try 
    { 
     tcpListener = new TcpListener(ipAddress, 13); 
     tcpListener.Start(); 
     output = "Waiting for a connection..."; 
    } 
    catch (Exception e) 
    { 
     output = "Error: " + e.ToString(); 
     MessageBox.Show(output); 
    } 

    while (true) 
    { 
     Thread.Sleep(10); 
     TcpClient tcpClient = tcpListener.AcceptTcpClient(); 
     byte[] bytes = new byte[256]; 
     NetworkStream stream = tcpClient.GetStream(); 
     stream.Read(bytes, 0, bytes.Length); 
     SocketHelper helper = new SocketHelper(); 
     helper.processMsg(tcpClient, stream, bytes); 
    } 
} 

我應該在哪裏增加嗎?

static void Main() 
{ 
    Application.Run(new Server()); 
} 
+0

'公共類服務器 { }' 需要說的是什麼樣的對象的'Server'是 此刻,你的代碼是假設服務器是一種方法和上應該有一個返回類型。既然你在你的Main中調用'new Server()'並且得到錯誤請求返回類型,我假設你打算創建一個類對象,並且你沒有在你的類文件頂部聲明這個類 '公共服務器(){ } '會的工作,如果你正在使用它作爲一個構造函數,但應該是: '公共類服務器 { 公共服務器(){ } // 其他方法 }' – Draken

+0

如果您從此[鏈接](https://msdn.microsoft.com/zh-cn/library/bb397809(v = vs.90).aspx)複製代碼,則必須將代碼單獨放入類。創建一個新類,將其命名爲Server,然後將代碼複製到其中。 – Han

+0

我創建了3個類 –

回答

0

在「公共」和「服務器」之間,你需要你要返回的對象的類型。如果你不想返回任何東西,那麼你會想使用「void」來代替返回類型。

在你提供的代碼中,你有「新的Server()」,所以你可能想把服務器定義爲一個類的類型。

相關問題