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...