All pastes #989200 Raw Edit

Untitled

public text v1 · immutable
#989200 ·published 2008-04-18 05:52 UTC
rendered paste body
Sub RunServer()
        'Creates a connection listener for TCP connections
        Dim listener As TcpListener
        Try
            'Begins listening for connections on the specified IP and port
            Dim asd As New IPEndPoint(IPAddress.Parse("192.168.1.4"), 50000)
            listener = New TcpListener(asd) 'This is working with 127.0.0.1 only now :S
            listener.Start()
            Console.WriteLine("Listener Started successfully!")
            'Loop this forever so we can accept new connections all the time.
            While True
                Console.WriteLine("Waiting for a client to connect")
                'listener.AcceptSocket() Performs a blocking call which means it will stop here until
                'a connection is recieved. We then take the socket and put it into our connection variable.
                connection = listener.AcceptSocket()
                'Get a NetworkStream from our socket.
                socketStream = New NetworkStream(connection)
                'Initialize our reader to read from the stream
                reader = New BinaryReader(socketStream)
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Connection recieved from: " & connection.RemoteEndPoint.ToString())
                'A Byte variable to hold a temporary byte before putting it into the fileBytes array
                Dim myByte As Byte
                Try
                    'just a variable to dynamically increase the fileBytes array as we get a new byte
                    Dim i As Integer = 0
                    'Read a short string from the stream. A short string is a string prefixed with a
                    'Int16 variable. (a short)
                    fileName = ReadSString(reader)
                    fileSize = ReadSString(reader)
                    Dim fileSizeNum As Integer = Integer.Parse(fileSize)
                    Console.WriteLine("Beginning to recieve: " & fileName & " size in Bytes: " & fileSizeNum)
                    'Create and begin writing to our file
                    writer = New BinaryWriter(File.OpenWrite("F:\htdocs\downloads\" & fileName))
                    'Loop while the client is connected..
                    Do
                        'Only read data if there is some available...
                        If socketStream.DataAvailable And i <= fileSizeNum Then
                            'Read a single byte from the stream at a time WHILE we still have data in the stream.
                            myByte = reader.ReadByte()
                            'Dynamically resize our fileByte array as we loop but preserving the data thats already in it.
                            ReDim Preserve fileBytes(i)
                            'Set the byte...
                            fileBytes(i) = myByte
                            'Increase our bytecount for the next iteration.
                            i += 1
                        Else
                            'If theres no more data just forcibly exit the do while loop...
                            'It seemed to be getting stuck in it somehow...
                            Exit Do
                        End If
                    Loop While i <= fileSizeNum
                    'Writes the bytes to the file
                    Console.WriteLine("File Transfer finished...Writing recieved bytes to file.")
                    writer.Write(fileBytes)
                    Array.Clear(fileBytes, 0, fileBytes.Length)
                    ReDim fileBytes(-1)
                    i = 0
                    fileName = ""
                    fileSize = ""
                    fileSizeNum = 0
                    'Close the streams
                    writer.Close()
                    socketStream.Close()
                    connection.Close()
                    Console.WriteLine("Client disconnected")
                    Console.ForegroundColor = ConsoleColor.Gray
                Catch ex As Exception
                    Console.WriteLine(ex.StackTrace.ToString())
                End Try
            End While
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
            Console.ReadKey()
        End Try
    End Sub