網頁文章 網頁文章

 

Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
 
Module Module1
 
    Public Sub Main()
 
        Dim Web_file1 As String = "c:\20141222.htm"
        Dim Web_file2 As String = "c:\a.jpg"
        Dim Web_file3 As String = "c:\b.jpg"
        Dim Web_file4 As String = "c:\c.jpg"
        Dim Web_file5 As String = "c:\d.jpg"
        Dim msg(401002400) As Byte
        Dim server As TcpListener
        server = Nothing
 
        Try
            Dim port As Int32 = 13000
            Dim localAddr As IPAddress = IPAddress.Parse("192.168.0.126")
            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 /20141222.htm")) >= 0) Then
                    Dim htmlHeader As String = _
                    "HTTP/1.1 200 OK" & ControlChars.CrLf & _
                    "Server: DahanbServer 1.0" & ControlChars.CrLf & _
                    "Content-Length: " & data.Length & ControlChars.CrLf & _
                    "Content-Type: " & "text/html" & _
                    ControlChars.CrLf & ControlChars.CrLf
                    If System.IO.File.Exists(Web_file1) = True Then
                        msg = My.Computer.FileSystem.ReadAllBytes(Web_file1)
                    End If
                    stream.Write(msg, 0, msg.Length)
                    client.Close()
                End If
 
                If ((data.IndexOf("GET /a.jpg")) >= 0) Then
                    Dim htmlHeader As String = _
                    "HTTP/1.1 200 OK" & ControlChars.CrLf & _
                    "Server: DahanbServer 1.0" & ControlChars.CrLf & _
                    "Content-Length: " & data.Length & ControlChars.CrLf & _
                    "Content-Type: " & "text/html" & _
                    ControlChars.CrLf & ControlChars.CrLf
                    If System.IO.File.Exists(Web_file2) = True Then
                        msg = My.Computer.FileSystem.ReadAllBytes(Web_file2)
                    End If
                    stream.Write(msg, 0, msg.Length)
                    client.Close()
                End If
 
                If ((data.IndexOf("GET /b.jpg")) >= 0) Then
                    Dim htmlHeader As String = _
                    "HTTP/1.1 200 OK" & ControlChars.CrLf & _
                    "Server: DahanbServer 1.0" & ControlChars.CrLf & _
                    "Content-Length: " & data.Length & ControlChars.CrLf & _
                    "Content-Type: " & "text/html" & _
                    ControlChars.CrLf & ControlChars.CrLf
                    If System.IO.File.Exists(Web_file3) = True Then
                        msg = My.Computer.FileSystem.ReadAllBytes(Web_file3)
                    End If
                    stream.Write(msg, 0, msg.Length)
                    client.Close()
                End If
 
                If ((data.IndexOf("GET /c.jpg")) >= 0) Then
                    Dim htmlHeader As String = _
                    "HTTP/1.1 200 OK" & ControlChars.CrLf & _
                    "Server: DahanbServer 1.0" & ControlChars.CrLf & _
                    "Content-Length: " & data.Length & ControlChars.CrLf & _
                    "Content-Type: " & "text/html" & _
                    ControlChars.CrLf & ControlChars.CrLf
                    If System.IO.File.Exists(Web_file4) = True Then
                        msg = My.Computer.FileSystem.ReadAllBytes(Web_file4)
                    End If
                    stream.Write(msg, 0, msg.Length)
                    client.Close()
                End If
 
                If ((data.IndexOf("GET /d.jpg")) >= 0) Then
                    Dim htmlHeader As String = _
                    "HTTP/1.1 200 OK" & ControlChars.CrLf & _
                    "Server: DahanbServer 1.0" & ControlChars.CrLf & _
                    "Content-Length: " & data.Length & ControlChars.CrLf & _
                    "Content-Type: " & "text/html" & _
                    ControlChars.CrLf & ControlChars.CrLf
                    If System.IO.File.Exists(Web_file5) = True Then
                        msg = My.Computer.FileSystem.ReadAllBytes(Web_file5)
                    End If
                    stream.Write(msg, 0, msg.Length)
                    client.Close()
                End If
            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
 
End Module