我在 C# 中有以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Networking
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---Connecting to a Host using a Specific Port---");
Console.WriteLine();
Console.WriteLine("Attempting Connection...");
Console.WriteLine();
string hostname = "www.yahoo.com";
int port_no = 21; //HTTP = 80, HTTPS = 443, FTP = 21
IPAddress ipa = (IPAddress)Dns.GetHostAddresses(hostname)[0];
try
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipa, port_no);
if (socket.Connected == true)
{
Console.WriteLine("The connection was successful!");
}
}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10061)
{
Console.WriteLine("The connection was NOT successful!");
}
else
{
Console.WriteLine(ex.Message);
}
}
Console.WriteLine();
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
如您所见,该程序尝试使用特定端口号连接到特定网站。
如何修改程序以便我可以知道数据是否在连接后通过特定端口发送?也许计算发送的字节数或文件类型?谢谢 :)