1st of all, i'm new in socket programming...
I try to received file.txt n show it in a form...
I try my code below, but it always fails...
it seem like become endless loops...
I try to trace it, 1st loop seem ok, but it's not stoping after 1st loop.. it's continue for the 2nd loops, after that, an error show up, saying, "cannot read file.txt, because it's blank" ---> or something like that...

how I close connection after received n read file.txt, and return it to waiting connection state ??

btw, this code run on backgroundworker..

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim serverSocket As New TcpListener(15000)
        Dim clientSocket As TcpClient
        Dim requestCount As Integer
        serverSocket.Start()
        clientSocket = serverSocket.AcceptTcpClient()
                requestCount = 0
        While (True)
            Try
                requestCount = requestCount + 1
                '(1) INCOMING FILE.TXT 
                Dim networkStream As NetworkStream = clientSocket.GetStream()
                Dim bytesFrom(10024) As Byte
                networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)

                Dim fs As New FileStream("C:\Program Aplikasi Greeting Cards\GreetCard\GreetCard\bin\Debug\datakartu.txt", FileMode.Open, FileAccess.Write, FileShare.Write)
                Dim sw As New StreamWriter(fs)
                sw.WriteLine(dataFromClient)
                sw.Close()
                fs.Close()

                Dim serverResponse As String = "Data FILE.TXT received"
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                networkStream.Flush()
                '(1) DONE

                'read file
                Dim arrayTeks As New ArrayList
                Dim file As New FileStream("C:\Program Aplikasi Greeting Cards\GreetCard\GreetCard\bin\Debug\datakartu.txt", FileMode.Open, FileAccess.Read, FileShare.Read)
                Dim sr As New StreamReader(file)

                While (sr.Peek > -1)
                    arrayTeks.Add(sr.ReadLine)
                End While
                sr.Close()
                file.Close()
                'done read file

' show file.txt in form kiriman
                kiriman.Show()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

        End While

many thanks before...

Recommended Answers

All 10 Replies

Use Flush() method with an instance of StreamWriter class.

i have done smt similar long time ago...here is the code:

Private Sub TCP_Receive(ByVal filename As String)

        Try
            '---get the local IP address
            Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Parse(ips.AddressList(0).ToString)

            '---start listening for incoming connection
            Dim serverSocket As New System.Net.Sockets.TcpListener(localAdd, 15000)
            serverSocket.Start()

            '---read incoming stream
            Dim clientSocket As TcpClient = serverSocket.AcceptTcpClient()
            Dim nws As NetworkStream = clientSocket.GetStream

            '---delete the file if it exists
            If File.Exists("c:\temp\" & filename) Then
                File.Delete("c:\temp\" & filename)
            End If

            '---create the file
            fs = New System.IO.FileStream("c:\temp\" & filename, FileMode.Append, FileAccess.Write)

            Dim totalBytes As Integer = 0

            Do
                '---read the incoming data
                Dim bytesRead As Integer = nws.Read(data, 0, clientSocket.ReceiveBufferSize)
                totalBytes += bytesRead
                fs.Write(data, 0, bytesRead)

                '--update the status label
                ToolStripStatusLabel1.Text = "Receiving " & totalBytes & " bytes...."
            Loop Until Not nws.DataAvailable

            ToolStripStatusLabel1.Text = "Received bytes:  " & totalBytes & "  .... Done."
            fs.Close()
            clientSocket.Close()
            serverSocket.Stop()
            '--read the file
            Dim Filereader As New IO.StreamReader("c:\temp\" & filename)

            While Not Filereader.EndOfStream
                textbox1.appendText(Filereader.ReadLine)
            End While
            Filereader.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

thanks. i'll try it...

thanks before for stopping server's loops...
next problem show up..
after I received all files, I show the received file on a new form..
the new form are show up, but my new form are not responding and I cant see anything because it's blank...
any idea, why this happen ???

well if these files are pretty big then i would say you should read them in a seperate thread and invoke the form for updating with the new lines

look at Threading and delegates

well, actually I send 1 file.txt , 1 background picture, and some little picture. I think it's less than 1MB...
is 1MB is a big file ??

I try to check all my received file, and all file that I received are well received without mistake, but when I try to show it on a form, my form are not responding..

could you please post the code where you include the files into your form? Also its a good idea to debug step by step to see on what step it hangs up.

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim serverSocket As New TcpListener(15000)
        Dim clientSocket As TcpClient
        serverSocket.Start()
        Try
            '(1) incoming FILE.TXT
            clientSocket = serverSocket.AcceptTcpClient()
            Dim networkStream As NetworkStream = clientSocket.GetStream()
            Dim bytesFrom(10024) As Byte
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)

            Dim fs As New FileStream("C:\Program Aplikasi Greeting Cards\GreetCard\GreetCard\bin\Debug\datakartu.txt", FileMode.Open, FileAccess.Write, FileShare.Write)
            Dim sw As New StreamWriter(fs)
            sw.WriteLine(dataFromClient)
            sw.Close()
            fs.Close()

            Dim serverResponse As String = "Data FILE.TXT well received"
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            networkStream.Flush()
            '(1)DONE RECEIVING FILE

            'READ file
            Dim arrayTeks As New ArrayList
            Dim file As New FileStream("C:\Program Aplikasi Greeting Cards\GreetCard\GreetCard\bin\Debug\datakartu.txt", FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim sr As New StreamReader(file)

            While (sr.Peek > -1)
                arrayTeks.Add(sr.ReadLine)
            End While
            sr.Close()
            file.Close()
            'DONE READ file

            'RECEIVED PICTURE
            Dim i as integer = 1
            Dim x As Integer
            Dim z As Integer = 3
            Dim y As Integer = 6
            If arrayTeks(3) <> Nothing And arrayTeks(6) <> Nothing Then
                x = arrayTeks(0) + 1
                For var = i To x
                    Dim PACKET_SIZE As UInt16 = 4096
                    Dim Reader As BinaryReader
                    Dim ReadBuffer(PACKET_SIZE - 1) As Byte
                    Dim NData As Int32
                    Dim MStream As MemoryStream
                    Dim LData As Int32

                    Reader = New BinaryReader(clientSocket.GetStream)
                    ' Read Length of data (Int32)
                    NData = Reader.ReadInt32
                    ' Now comes the data, save it in a memory stream
                    MStream = New MemoryStream
                    While NData > 0
                        LData = clientSocket.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
                        MStream.Write(ReadBuffer, 0, LData)
                        NData -= LData
                    End While

                    SaveFile(arrayTeks(z), MStream.ToArray, False) 'Then
                    z = z + 3
                Next
            'END RECEIVED PICTURE

            clientSocket.Close()
            serverSocket.Stop()

            'build a new form
            kiriman.Show()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

End Sub

code for build a new form

Public Class kiriman
    Dim i As Integer = 1
    Dim var As Integer
    Dim bel(100) As Object
    Dim pbo(100) As Object
    Dim jumz As Integer = 0
    Dim jumx As Integer = 0

    Private Sub kiriman_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim arrayTeks As New ArrayList
        Dim file As New FileStream("C:\Program Aplikasi Greeting Cards\GreetCard\GreetCard\bin\Debug\datakartu.txt", FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim sr As New StreamReader(file)

        While (sr.Peek > -1)
            arrayTeks.Add(sr.ReadLine)
        End While

        sr.Close()
        file.Close()
       
        'BUILD FORM
        If arrayTeks(3) = Nothing Then
            Me.Text = "Kartu Ucapan"
            Me.Height = arrayTeks(4)
            Me.Width = arrayTeks(5)
            Me.StartPosition = FormStartPosition.CenterScreen
        Else
            Me.Text = "Kartu Ucapan"
            Me.BackgroundImage = System.Drawing.Image.FromFile(arrayTeks(3))
            Me.BackgroundImageLayout = arrayTeks(2)
            Me.Height = arrayTeks(4)
            Me.Width = arrayTeks(5)
            Me.StartPosition = FormStartPosition.CenterScreen
        End If

        'ADDING PICTURE
        If arrayTeks(0) > 0 Then
            Dim jmla As Integer = arrayTeks(0)
            jml = 6
            For var = i To jmla
                jumz = jumz + 1
                pbo(jumz) = New PictureBox
                pbo(jumz).ImageLocation = arrayTeks(jml)
                jml += 1
                pbo(jumz).SizeMode = PictureBoxSizeMode.AutoSize
                pbo(jumz).Location = New Point(CInt(arrayTeks(jml)), CInt(arrayTeks(jml + 1)))
                jml += 2
                pbo(jumz).BackColor = Color.Transparent
                pbo(jumz).Visible = True
                Me.Controls.Add(pbo(jumz))
                Next var
        Else
            jml = 6
        End If

        'ADDING SENTENCE
        If arrayTeks(jml) > 0 Then
            Dim juma As Integer = arrayTeks(jml)
            jml += 1
            For var = i To juma
                jumx = jumx + 1
                bel(jumx) = New Label
                bel(jumx).Text = CStr(arrayTeks(jml))
                jml += 1  
'16 font
                If CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Regular, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Italic Xor FontStyle.Strikeout Xor FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Italic, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Strikeout, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Italic, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Strikeout, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Italic Xor FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Italic Xor FontStyle.Strikeout, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Underline Xor FontStyle.Strikeout, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = False And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Italic Xor FontStyle.Strikeout Xor FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = False And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Strikeout Xor FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = False And CBool(arrayTeks(jml + 3)) = True Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Italic Xor FontStyle.Strikeout, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                ElseIf CBool(arrayTeks(jml)) = True And CBool(arrayTeks(jml + 1)) = True And CBool(arrayTeks(jml + 2)) = True And CBool(arrayTeks(jml + 3)) = False Then
                    jml += 4
                    bel(jumx).Font = New System.Drawing.Font(CStr(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), FontStyle.Bold Xor FontStyle.Italic Xor FontStyle.Underline, GraphicsUnit.Point, CByte(0), False)
                    jml += 2
                End If
                bel(jumx).ForeColor = Color.FromArgb(CInt(arrayTeks(jml)), CInt(arrayTeks(jml + 1)), CInt(arrayTeks(jml + 2)), CInt(arrayTeks(jml + 3)))
                jml += 4
                bel(jumx).Location = New Point(CInt(arrayTeks(jml)), CInt(arrayTeks(jml + 1)))
                jml += 2
                bel(jumx).AutoSize = True
                bel(jumx).BackColor = Color.Transparent
                bel(jumx).Visible = True
                Me.Controls.Add(bel(jumx))
                Next var
        End If
    End Sub
End Class

don't laugh please...
ha.. ha... ha...
I think my code are very very noob....
but, please help me...

ooook before i read all that did u try to add a try catch block arround "ADDING SENTENCE" stuff?
i doubt your If's will work the way you wrote i.
to make it easier you might want to attach your project here or send it to my mail. pretty hard to follow your code here

sorry for my absent...

I try to solve not responding problem, but I still can't solve that...

i can attach my project, but because i'm using my sql DB, i thing my project cant be run outside my computer...

any other Idea ??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.