private static Socket connectSocket(string server, int port)
{
Socket s = null;
IPHostEntry iphe = null;
try
{
// Get host related information.
iphe = Dns.Resolve(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception to be thrown if the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress ipad in iphe.AddressList)
{
IPEndPoint ipe = new IPEndPoint(ipad, port);
Socket tmpS = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
Try
If File.Exists(page.MapPath(strFileName)) Then
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
'清除缓冲区流中的所有内容输出.
page.Response.Clear()
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))
'向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
'表明输出的HTTP为流[stream],因此客户端只能下载.
page.Response.ContentType = "application/octet-stream"
'发送文件流到客户端.
page.Response.WriteFile(TargetFile.FullName)
'停止执行当前页
page.Response.End()
End If
Catch ex As Exception
Throw ex
End Try
End Sub