• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

help

1)Which protocol more usefull for sending file?

2)There is an application  to bottom.Sometimes; sended files in This application,cannot open to server pc
Sometimes can open;sometimes cannot open.
when 0t may?

client
   
 Dim fileBuffer As Byte()
        Dim fileStream As Stream
        fileStream = File.OpenRead("c:\2.jpg")
        ReDim fileBuffer(fileStream.Length)
        fileStream.Read(fileBuffer, 0, fileStream.Length)
        Dim clientSocket As New TcpClient("10.0.0.11", 10001)
        Dim netWorkStream As NetworkStream
        netWorkStream = clientSocket.GetStream()
        netWorkStream.Write(fileBuffer, 0, fileStream.Length)
        netWorkStream.Close()


server

Imports System
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private alSockets As ArrayList

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lblStatus As System.Windows.Forms.TextBox
    Friend WithEvents lbxConnections As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblStatus = New System.Windows.Forms.TextBox
        Me.lbxConnections = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'lblStatus
        '
        Me.lblStatus.Location = New System.Drawing.Point(88, 88)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(296, 20)
        Me.lblStatus.TabIndex = 0
        Me.lblStatus.Text = ""
        '
        'lbxConnections
        '
        Me.lbxConnections.Location = New System.Drawing.Point(120, 152)
        Me.lbxConnections.Name = "lbxConnections"
        Me.lbxConnections.Size = New System.Drawing.Size(120, 95)
        Me.lbxConnections.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 374)
        Me.Controls.Add(Me.lbxConnections)
        Me.Controls.Add(Me.lblStatus)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim IPHost As IPHostEntry
        IPHost = Dns.GetHostByName(Dns.GetHostName())
        lblStatus.Text = "My IP address is " + IPHost.AddressList(0).ToString()
        alSockets = New ArrayList
        Dim thdListener As New Thread(New ThreadStart(AddressOf ListenerThread))
        thdListener.Start()
    End Sub
    Public Sub listenerThread()
        Dim tcpListener As New TcpListener(IPAddress.Any, 1024)
        Dim handlerSocket As Socket
        Dim thdstHandler As ThreadStart
        Dim thdHandler As Thread
        tcpListener.Start()
        Do
            handlerSocket = tcpListener.AcceptSocket
            If handlerSocket.Connected Then
                lbxConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.")
                SyncLock (Me)
                    alSockets.Add(handlerSocket)
                End SyncLock
                thdstHandler = New ThreadStart(AddressOf handlerThread)
                thdHandler = New Thread(thdstHandler)
                thdHandler.Start()
            End If
        Loop
    End Sub

    Public Sub handlerThread()
        Dim handlerSocket As Socket
        handlerSocket = alSockets(alSockets.Count - 1)
        Dim networkStream As NetworkStream = New NetworkStream(handlerSocket)
        Dim blockSize As Int16 = 1024
        Dim thisRead As Int16
        Dim dataByte(blockSize) As Byte
        SyncLock Me
            Dim fileStream As Stream
            fileStream = File.OpenWrite("C:\upload.txt")
            While (True)
                thisRead = networkStream.Read(dataByte, 0, blockSize)            '// I'm Getting error in this line
                If thisRead = 0 Then
                    Exit While
                End If
                fileStream.Write(dataByte, 0, dataByte.Length)                         '// I'm Getting error in this line
            End While
            fileStream.Close()
        End SyncLock
        lbxConnections.Items.Add("File Written")
        handlerSocket = Nothing
    End Sub
End Class
0
remotedesktop
Asked:
remotedesktop
  • 2
1 Solution
 
ericwong27Commented:
Using FTP protocol will be a better solution for file uploading

Reference
http://www.csharphelp.com/archives/archive9.html
http://sourceforge.net/projects/dotnetftpclient/
0
 
remotedesktopAuthor Commented:
thanks
so

0s there ftp application in visual basic.net?

0
 
ericwong27Commented:
C# component can be used in VB.NET and vice versa

You can use following tool to convert C# to VB.NET
http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx

0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now