вторник, 23 октября 2012 г.

FileTransfer



Form that sending file

Imports System.Net              
Imports System.Net.Sockets     
Imports System.IO
Imports System.Threading

Public class Form1         
 Private filePath As String    
 Delegate Sub nan()

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button2.Enabled = False        
        filePath = TextBox1.Text
        Dim sendThread As New System.Threading.Thread(AddressOf sendSub)
        sendThread.IsBackground = True
        sendThread.Start()
End Sub 

Private Sub sendSub()  
        Dim cli As New TcpClient
        cli.Connect(TextBox2.Text, 1300)
        Try
            Dim nstm As Stream = cli.GetStream()
            Dim fstm As Stream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
            Dim buffer(1024 - 1) As Byte
            Do While True
                Dim bytesRead As Integer = fstm.Read(buffer, 0, buffer.Length)
                If bytesRead = 0 Then Exit Do
                nstm.Write(buffer, 0, bytesRead)
                nstm.Flush()
            Loop
            cli.Close()
            nstm.Close()
            fstm.Close()
            MsgBox("Уes", MsgBoxStyle.Exclamation, "It Works=)")
        Catch ex As Exception
            MsgBox("Not works" & vbCr & ex.ToString, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub
    End class                 

Form that geting file

Imports System.Net.Sockets
Imports System.Net
Imports System.IO
Public Class Form1
    Private filePath As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        TextBox1.Enabled = False
        filePath = TextBox1.Text
        Dim ListenThread As System.Threading.Thread = New System.Threading.Thread(AddressOf ListenSub)
        ListenThread.IsBackground = True
        ListenThread.Start()
    End Sub

    Private Sub ListenSub()
        Dim server As New TcpListener(IPAddress.Any, 1300)
        server.Start()
        Try
            While True
                Dim c As TcpClient = server.AcceptTcpClient
                Dim s As NetworkStream = c.GetStream

                FileOpen(1, filePath, OpenMode.Binary)
                Dim buffer(1024 - 1) As Byte
                Do While True
                    Dim bytesRead As Integer = s.Read(buffer, 0, buffer.Length)
                    If bytesRead = 0 Then Exit Do
                    FilePut(1, buffer)
                Loop
                FileClose(1)
                s.Close()
                c.Close()
            End While
        Catch ex As Exception
            MsgBox("Error" & vbCr & ex.ToString, MsgBoxStyle.Critical, "")
        End Try
    End Sub
End Class

2 комментария: