任何人都可以分享一个简单的VB 2008代码来打开一个端口。我希望它像utorrent一样,如何更改数据传输的侦听端口。非常感谢,如果你能帮助我的话!
答案 0 :(得分:1)
但是,低级通信处理的起点是System.Net.Sockets
命名空间,其中包含Socket
类。它允许低级控制,如打开端口,监听连接和自己处理它们。
Here's关于VB.NET中Socket编程的教程,但是如果你搜索“C#Socket教程”,你可能会找到更多信息。 C#语法与VB.NET略有不同,但它使用相同的类和相同的概念,因此您可能能够将课程应用到您自己的代码中。
答案 1 :(得分:1)
正如Avner所说,uTorrent 不是简单代码。如果您想在该级别上做任何事情,那么您还有很多工作要做。
这是一个可以构建的简单示例套接字程序。
Imports System.Net
Imports System.Net.Sockets
Module Module1
Sub Main()
Console.WriteLine("Enter the host name or IP Address to connect to:")
Dim hostName = Console.ReadLine().Trim()
If hostName.Length = 0 Then
' use the local computer if there is no host provided
hostName = Dns.GetHostName()
End If
Dim ipAddress As IPAddress = Nothing
' parse and select the first IPv4 address
For Each address In Dns.GetHostEntry(hostName).AddressList
If (address.AddressFamily = AddressFamily.InterNetwork) Then
ipAddress = address
Exit For
End If
Next
' you will have to check beyond this point to ensure
' there is a valid address before connecting
Dim client = New TcpClient()
Try
' attempt to connect on the address
client.Connect(ipAddress, 80)
' do whatever you want with the connection
Catch ex As SocketException
' error accessing the socket
Catch ex As ArgumentNullException
' address is null
' hopefully this will never happen
Catch ex As ArgumentOutOfRangeException
' port must be from 0 to 64k (&HFFFF)
' check and ensure you've used the right port
Catch ex As ObjectDisposedException
' the tcpClient has been disposed
' hopefully this will never happen
Catch ex As Exception
' any other exception I haven't dreamt of
Finally
' close the connection
' the TcpClient.Close() method does not actually close the
' underlying connection. You have to close it yourself.
' http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B821625
client.GetStream().Close()
' then close the client's connection
client.Close()
End Try
End Sub
End Module
请注意,套接字编程非常复杂,您必须彻底测试所有边缘情况的代码。
祝你好运!