DSAPI.DLL のダウンロードリンク
DSAPIを用いたネットワークプログラミングについて説明します。ネットワーク関連の機能は非常に幅広く、ここではDSAPIに封じ込められた便利な機能を紹介します。これらの機能は、開発効率を高めるために簡略化されています。
まずは基本的な機能を見てみましょう:``` '--- IP情報関連 Console.WriteLine(DSAPI.Network.IP.LocalIntranetAddress) Console.WriteLine(DSAPI.Network.IP.PublicAddressWithLocation()) Console.WriteLine(DSAPI.Network.IP.PublicAddressProvince) Console.WriteLine(DSAPI.Network.IP.PublicAddressCity) Console.WriteLine(DSAPI.Network.IP.ResolveDomainToIP("dskj.meibu.net")) '---- ファイルダウンロード関連 Console.WriteLine(DSAPI.Network.Download.GetFileSize("http://dlsw.baidu.com/sw-search-sp/soft/2e/10849/wrar520sc_setup.1418806135.exe")) Console.WriteLine("WinRarサイズ=" & DSAPI.FileUtils.FormatFileSize(DSAPI.Network.Download.GetFileSize("http://dlsw.baidu.com/sw-search-sp/soft/2e/10849/wrar520sc_setup.1418806135.exe")))
出力結果:
192.168.1.53
112.82.187.65,江苏省常州市 联通
江苏省
常州市
114.226.11.7
1867312
WinRarサイズ=1.78MB
**マルチスレッドダウンロード**このようにシンプルなマルチスレッドダウンロード機能は、非常に実用的です。
Private WithEvents _downloader As DSAPI.Network.Download.MultiThreadedDownloader Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim downloader As New DSAPI.Network.Download.MultiThreadedDownloader With downloader .SavePath = "c:\1.exe" .BufferSize = 2000000 ' バッファがこのサイズに達するとファイルに書き込まれる .FileUrl = "http://dlsw.baidu.com/sw-search-sp/soft/2e/10849/wrar520sc_setup.1418806135.exe" .TotalThreads = 5 Console.WriteLine("ダウンロードファイルサイズ=" & .FileSize) .StartDownload() End With _downloader = downloader End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If _downloader IsNot Nothing Then
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine("ダウンロードURL:" & _downloader.FileUrl)
.AppendLine("ファイルサイズ:" & DSAPI.FileUtils.FormatFileSize(_downloader.FileSize))
.AppendLine("スレッド数:" & _downloader.TotalThreads)
.AppendLine("ダウンロード速度:" & DSAPI.FileUtils.FormatFileSize(_downloader.DownloadSpeedBytesPerSecond) & "/S")
.AppendLine("ダウンロード進捗:" & _downloader.Progress)
End With
End If
End Sub
Private Sub _downloader_FileDownloadCompleted(FilePath As String, SourceName As String) Handles _downloader.FileDownloadCompleted
Console.WriteLine(FilePath & " のダウンロードが完了しました")
End Sub
Private Sub _downloader_DownloadError(FilePath As String, SourceName As String, Exception As Exception) Handles _downloader.DownloadError
Console.WriteLine(SourceName & " のダウンロードに失敗しました")
End Sub
上記コードでは、タイマーを使用してダウンロード状況を定期的に取得しています。デフォルトのバッファサイズは2MBで、ダウンロードスレッド数については、大きなファイルには10スレッド以上を推奨し、10MB未満のファイルは3スレッド以下が適しています。