網頁程式
<html> <body> </body> Imports System.IO
Imports System.Net Imports System.Net.Sockets Imports System.Text Imports Microsoft.VisualBasic Module Module1 Public Sub Main() Dim Web_file As String = "c:\TestFile5.txt" Dim Web_file1 As String = "c:\mydesert.jpg" Dim Web_file2 As String = "c:\ccc.MHT" Dim Web_file3 As String = "c:\aaaa.mp3" Dim msg(40102400) As Byte Dim server As TcpListener server = Nothing Try Dim port As Int32 = 13000 Dim localAddr As IPAddress = IPAddress.Parse("192.168.0.147") Dim bytes(1024) As Byte Dim data As String = Nothing server = New TcpListener(localAddr, port) server.Start() While True Console.Write("Waiting for a connection... ") Dim client As TcpClient = server.AcceptTcpClient() Console.WriteLine("Connected!" & ControlChars.CrLf) Dim clientInfo As IPEndPoint = CType(client.Client.RemoteEndPoint, IPEndPoint) Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString()) Dim stream As NetworkStream = client.GetStream() Dim i As Integer i = stream.Read(bytes, 0, bytes.Length) data = System.Text.Encoding.ASCII.GetString(bytes, 0, i) Console.WriteLine("Received: {0}", data) If ((data.IndexOf("GET / ")) >= 0) Then ' Set HTML Header Dim htmlHeader As String = _ "HTTP/1.0 200 OK" & ControlChars.CrLf & _ "Server: DahanbServer 1.0" & ControlChars.CrLf & _ "Content-Length: " & data.Length & ControlChars.CrLf & _ "Content-Type: " & "text/html" & _ ControlChars.CrLf & ControlChars.CrLf ' The content Length of HTML Header msg = Encoding.ASCII.GetBytes(htmlHeader) Console.WriteLine("HTML Header: " & ControlChars.CrLf & htmlHeader) ' 回應HTML標題至用戶端瀏覽器 stream.Write(msg, 0, msg.Length) If System.IO.File.Exists(Web_file) = True Then msg = My.Computer.FileSystem.ReadAllBytes(Web_file) End If End If If ((data.IndexOf("GET /homework1.htm")) >= 0) Then ' Set HTML Header If System.IO.File.Exists(Web_file1) = True Then msg = My.Computer.FileSystem.ReadAllBytes(Web_file1) End If End If If ((data.IndexOf("GET /homework2.htm")) >= 0) Then If System.IO.File.Exists(Web_file2) = True Then msg = My.Computer.FileSystem.ReadAllBytes(Web_file2) End If End If If ((data.IndexOf("GET /homework3.htm")) >= 0) Then If (False) Then ' Set HTML Header Dim htmlHeader As String = _ "HTTP/1.0 200 OK" & ControlChars.CrLf & _ "Server: WebServer 1.0" & ControlChars.CrLf & _ "Content-Length: " & data.Length & ControlChars.CrLf & _ "Content-Type: " & "audio/mpeg" & _ ControlChars.CrLf & ControlChars.CrLf ' The content Length of HTML Header msg = Encoding.ASCII.GetBytes(htmlHeader) Console.WriteLine("HTML Header: " & ControlChars.CrLf & htmlHeader) ' 回應HTML標題至用戶端瀏覽器 stream.Write(msg, 0, msg.Length) End If If System.IO.File.Exists(Web_file3) = True Then msg = My.Computer.FileSystem.ReadAllBytes(Web_file3) End If End If stream.Write(msg, 0, msg.Length) client.Close() End While Catch e As SocketException Console.WriteLine("SocketException: {0}", e) Finally server.Stop() End Try Console.WriteLine(ControlChars.Cr + "Hit enter to continue....") End Sub 'Main End Module |