![](/img/trans.png)
[英]Invalid token ';' in class, struct, or interface member declaration
[英]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.