Results 1 to 4 of 4

Thread: [RESOLVED] Multiple upload is blocked at 2 pictures

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    [RESOLVED] Multiple upload is blocked at 2 pictures

    Hi,
    I'm making a program in order to upload images in Imagevenue etc...
    Send pictures or receive results (http address), there is no problem.
    But I want multithreading for this application.
    In beginning, I used threadpool with variable "maxthreads". But I didn't
    upload more 2 pictures at the same time. I tried 3 pictures at the same time,
    but it's always 2 pictures which are uploading...

    So, I decided to try with backgroundworker. But it's always the same problem.
    The thirst backgrounworker is brake...
    How can I upload more 2 images at the same time ?


    It's my exemple with backgroundworker


    Code:
    Imports System.Threading
    Imports System.Text
    Imports System.IO
    Imports System.Net
    
    Public Class Form1
    
        ' My 3 images
        Dim filepath1 As String = "C:\test\A.jpg"
        Dim filepath2 As String = "C:\test\B.jpg"
        Dim filepath3 As String = "C:\test\C.jpg"
    
        ' My 3 results
        Dim result1 As String = ""
        Dim result2 As String = ""
        Dim result3 As String = ""
       
        ' my first backgroundworker for first upload
        Private Sub bgw1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw1.DoWork
            Dim boundary As String = ("----------" & DateTime.Now.Ticks.ToString("x"))
            Dim Postheader() As Byte
    
            Dim builder As New StringBuilder
            builder.Append("--")
            builder.Append(boundary)
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append("Content-Disposition: form-data; name=""imgcontent""")
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append("Not safe for work")
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append("--")
            builder.Append(boundary)
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append("Content-Disposition: form-data; name=""userfile[]""; filename=""")
            builder.Append(Path.GetFileName(filepath1))
            builder.Append("""")
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append("Content-Type: ")
            builder.Append("image/jpeg")
            builder.Append(ChrW(13) & ChrW(10))
            builder.Append(ChrW(13) & ChrW(10))
            Postheader = Encoding.UTF8.GetBytes(builder.ToString)
    
            Dim myStream As FileStream = Nothing
            Dim requestStream As Stream = Nothing
            Dim myResponse As HttpWebResponse = Nothing
            Dim myReader As StreamReader = Nothing
    
            Try
                Dim myRequest As HttpWebRequest = DirectCast(WebRequest.Create("http://www.imagevenue.com/upload.php"), HttpWebRequest)
                myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; ru; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6"
                myRequest.ContentType = ("multipart/form-data; boundary=" & boundary)
                myRequest.KeepAlive = False
                myRequest.ServicePoint.Expect100Continue = True
                myRequest.Method = "POST"
                myRequest.AllowWriteStreamBuffering = False
                Dim bytes As Byte() = Encoding.ASCII.GetBytes((ChrW(13) & ChrW(10) & "--" & boundary & "--" & ChrW(13) & ChrW(10)))
    
                myStream = New FileStream(filepath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
    
                Dim taille As Long = ((Postheader.Length + myStream.Length) + bytes.Length)
                myRequest.ContentLength = taille
                requestStream = myRequest.GetRequestStream
    
                ' Send first information before image
                requestStream.Write(Postheader, 0, Postheader.Length)
    
                Const BufferSize As Integer = 4096
                Dim content As Byte() = New Byte(BufferSize - 1) {}
                Dim dataRead As Integer
                Dim dataReads As Long = 0
    
                ' Send image
                While True
                    dataRead = myStream.Read(content, 0, BufferSize)
                    If dataRead = 0 Then
                        Exit While
                    End If
                    requestStream.Write(content, 0, dataRead)
                    dataReads += dataRead
                    bgw1.ReportProgress(Math.Round(CDbl((dataReads / myStream.Length) * 100), 0))
                End While
    
                ' Send info after image
                requestStream.Write(bytes, 0, bytes.Length)
    
                requestStream.Close()
                requestStream.Dispose()
                myStream.Close()
                myStream.Dispose()
                myResponse = DirectCast(myRequest.GetResponse, HttpWebResponse)
                myReader = New StreamReader(myResponse.GetResponseStream, Encoding.UTF8)
                Dim myResult As String = myReader.ReadToEnd
                myResponse.Close()
                myReader.Close()
                myReader.Dispose()
    
                If myResult.Contains("successfully") Then
                    If (myResult.Contains("[URL=http://") AndAlso myResult.Contains("[/URL]")) Then
                        myResult = myResult.Substring(myResult.IndexOf("[URL=http://"))
                        myResult = myResult.Remove((myResult.IndexOf("[/URL]", 0) + 6))
                    ElseIf (myResult.Contains("")) Then
                        myResult = myResult.Substring(myResult.IndexOf("[IMG]http://"))
                        myResult = myResult.Remove((myResult.IndexOf("[/IMG]", 0) + 6))
                    End If
    
                    result1 = myResult
                End If
    
            Catch exception As Exception
    
            Finally
                Try
                    If (Not requestStream Is Nothing) Then
                        requestStream.Close()
                        requestStream.Dispose()
                    End If
                    If (Not myStream Is Nothing) Then
                        myStream.Close()
                        myStream.Dispose()
                    End If
                    If (Not myResponse Is Nothing) Then
                        myResponse.Close()
                        myResponse = Nothing
                    End If
                    If (Not myReader Is Nothing) Then
                        myReader.Close()
                        myReader.Dispose()
                    End If
    
                Catch 'obj2 As Object
                End Try
            End Try
          
        End Sub
    
    Private Sub bgw2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw2.DoWork
           
    ' Same of first backgroundworker, but for 2nd image
    
        End Sub
    
        Private Sub bgw3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw3.DoWork
             
    ' Same of first backgroundworker, but for 3rd image
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' launch 3 backgroundworker      
    
            bgw1.RunWorkerAsync()
            bgw2.RunWorkerAsync()
            bgw3.RunWorkerAsync()
        End Sub
    
        ' update of progressbars
        Private Sub bgw1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw1.ProgressChanged
            ProgressBar1.Value = e.ProgressPercentage
        End Sub
    
        Private Sub bgw2_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw2.ProgressChanged
            ProgressBar2.Value = e.ProgressPercentage
        End Sub
    
        Private Sub bgw3_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw3.ProgressChanged
            ProgressBar3.Value = e.ProgressPercentage
        End Sub
    
    End Class

    Sorry for my bad english.
    Bye
    Last edited by nihonsx5; Jun 3rd, 2007 at 09:29 AM.

  2. #2
    Member
    Join Date
    May 2007
    Posts
    37

    Re: Multiple upload is blocked at 2 pictures

    maybe webhost doesn't allow more than 2 connections from same ip and thread pool owns better than making same method 3 times

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    Re: Multiple upload is blocked at 2 pictures

    Imagevenue allow more than 2 connections. I tried with another software which works.
    Threadpool have the same result than backgroundworker : 2 upload max

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    Re: Multiple upload is blocked at 2 pictures

    Sorry, the other software was bad.
    vb2dub you're right, the server accept only 2 connections.
    I saw the connections open in netstat, and there are 2 connections open max.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width