網頁文章 網頁文章

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:\TestFile6.txt"
Dim Web_file2 As String = "c:\TestFile7.txt"
Dim Web_file3 As String = "c:\TestFile8.txt"
Dim TextLine As String
Dim server As TcpListener
server = Nothing

Try

Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

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 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

' MsgBox("File 0 Exist")

If System.IO.File.Exists(Web_file) = True Then

Dim objReader As New System.IO.StreamReader(Web_file, System.Text.Encoding.GetEncoding("big5"))
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & ControlChars.CrLf
Loop
objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
End If


If ((data.IndexOf("GET /homework1.htm")) >= 0) Then

' MsgBox("File 1 Exist")

If System.IO.File.Exists(Web_file1) = True Then

Dim objReader As New System.IO.StreamReader(Web_file1, System.Text.Encoding.GetEncoding("big5"))
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & ControlChars.CrLf
Loop
objReader.Close()
Else
MsgBox("File Does Not Exist")

End If

End If


If ((data.IndexOf("GET /homework2.htm")) >= 0) Then

' MsgBox("File 2 Exist")

If System.IO.File.Exists(Web_file2) = True Then

Dim objReader As New System.IO.StreamReader(Web_file2, System.Text.Encoding.GetEncoding("big5"))
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & ControlChars.CrLf
Loop
objReader.Close()
Else
MsgBox("File Does Not Exist")

End If
End If


If ((data.IndexOf("GET /homework3.htm")) >= 0) Then

' MsgBox("File 3 Exist")

If System.IO.File.Exists(Web_file3) = True Then

Dim objReader As New System.IO.StreamReader(Web_file3, System.Text.Encoding.GetEncoding("big5"))

Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & ControlChars.CrLf
Loop
objReader.Close()
Else
MsgBox("File Does Not Exist")

End If

End If

 

Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(TextLine)
stream.Write(msg, 0, msg.Length)
TextLine = Nothing
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