2016-06-13 104 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()”,所以你可能想把服务器定义为一个类的类型。

相关问题