I am creating a server/client file transferring application in which only server can send file to client through socket. I succeeded in sending the file but the problem is that server can send only one file & when I try to send the next file after the first file is delivered on the client I get the following error
"Operation not allowed on non-connected sockets"
.
Here is my server code:
''#sends filename, filelength, filebytes
Dim info As New IO.FileInfo("D:\setup_ais.exe")
''#writes a String and a Long with binarywriter (wrapping networkstream)
Dim bw As New IO.BinaryWriter(clientSocket.GetStream)
bw.Write(info.Name)
bw.Write(info.Length)
''#using filestream to read file, writes this directly to networkstream
Using fs As New IO.FileStream("D:\setup_ais.exe", IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(8092) As Byte, reads As Integer = -1
Do Until reads = 0
reads = fs.Read(buffer, 0, buffer.Length)
clientSocket.GetStream.Write(buffer, 0, reads)
Loop
End Using
bw.Close()
clientSocket.Close()
serverSocket.Stop()
Here is my client code:
''#receives filename, filelength, filebytes
Dim filename, filepath As String, filelen As Long
''#using binaryreader (wrapped networkstream) to read a String and a Long
Dim br As New IO.BinaryReader(clientSocket.GetStream)
filename = br.ReadString
filelen = br.ReadInt64
''#filepath = IO.Path.Combine(Application.StartupPath, filename)
filepath = IO.Path.Combine("C:\", filename)
Dim buffer(8092) As Byte, readstotal As Long = 0
Dim reads As Integer = -1
''#using filestream to write read filebytes directly from networkstream
Using fs As New IO.FileStream(filepath, IO.FileMode.Create, IO.FileAccess.Write)
Do Until readstotal = filelen
reads = clientSocket.GetStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, reads)
readstotal += reads
Loop
End Using
MsgBox("received: " & filename)
br.Close()
clientSocket.Close()
Please give me some solution.
[Edited]Code is blocked in "pre" tags[/Edited]