I'm trying to create a file sharing program. So far, the connection and the file transfer part is taken care of. But I'm currently having problems on how to receive and save the file properly.
I can successfully receive a file if and only if I will specify its name and extension on the server side. Now my question is, how can I retrieve the name and extension of the file so that I can successfully receive the file on the server? What I mean is that I don't want to specify a name every time a file is received. Is this possible? I'm thinking of sending the file name via another network stream, but I guess there is a better way to do it.
Thanks in advance!
Here's a screen shot of the error.
https://i.stack.imgur.com/XkpZ2.png
Server Side Code
While True
Dim c As TcpClient = server.AcceptTcpClient
Dim s As NetworkStream = c.GetStream
FileOpen(1, filePath, OpenMode.Binary)
Dim buffer(1024 - 1) As Byte
Do While True
Dim bytesRead As Integer = s.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then Exit Do
FilePut(1, buffer)
Loop
FileClose(1)
s.Close()
c.Close()
End While
Client Side Code
Dim nstm As Stream = cli.GetStream()
Dim fstm As Stream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim buffer(1024 - 1) As Byte
Do While True
Dim bytesRead As Integer = fstm.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then Exit Do
nstm.Write(buffer, 0, bytesRead)
Loop