繁体   English   中英

类,结构或接口成员声明中的无效令牌“ try”

[英]Invalid token 'try' in class, struct, or interface member declaration

今天早上才刚开始使用ASPX文件,但遇到了障碍。

我收到以下错误:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1519: Invalid token 'try' in class, struct, or interface member declaration

Source Error:

Line 5:  int portno = 52791;
Line 6:  IPAddress ipa = Dns.GetHostEntry(hostname).AddressList[0];
Line 7:  try
Line 8:  {
Line 9:      System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);

Source File: c:\inetpub\wwwroot\test.aspx    Line: 7 

从以下ASPX文件代码中:

<%@ Page Language="C#" %>
<%@ Import namespace="System" %>
<script runat="server">
string hostname = "localhost";
int portno = 52791;
IPAddress ipa = Dns.GetHostEntry(hostname).AddressList[0];
try
{
    System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
    sock.Connect(ipa, portno);
    if (sock.Connected == true)  // Port is in use and connection is successful
            string output = "Port is Closed";
    sock.Close();

}
catch (System.Net.Sockets.SocketException ex)
{
    if (ex.ErrorCode == 10061)  // Port is unused and could not establish connection 
        string output = "Port is Open!";
    else
        string output = ex.Message;
}
</script>
<html>...

数小时的谷歌搜索并没有解决我的问题。

<%@ Page Language="C#"  Debug="true" %>
<%@ Import namespace="System.Net" %>
<script runat="server">
public class Whatever
{
public static string output()
{
string hostname = "localhost";
int portno = 52791;
//IPAddress ipa = Dns.GetHostEntry(hostname).AddressList[0];
byte[] ipb = {192,168,1,30};
IPAddress ipa = new IPAddress(ipb);
string ret = "";
try
{
    System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
    sock.Connect(ipa, portno);
    if (sock.Connected == true)  // Port is in use and connection is successful
            ret = "Port is Closed";
    sock.Close();

}
catch (System.Net.Sockets.SocketException ex)
{
    if (ex.ErrorCode == 10061)  // Port is unused and could not establish connection 
        ret = "Port is Open!";
    else
        ret = ex.Message;
}
return ret;
}
}
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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