Hello all
I am having problems with a Client / Server application I am writing. I want to send a file from a client app to a server app, and I have created code to do so. I am using VB.NET 2003 and testing on WinXP SP2 for both machines (firewalls disabled).
However I have run up against an apparently insurmountable problem (for me). When I test by running both apps on the same computer I have no problems, receiving the test file properly but when I run the client from another PC on my LAN the file only comes through partially (truncated). I have also noted that if I step through the receiving code of the server manually, it works properly even across the LAN, so I can't see where the problem is happening.
At present I have disabled the multithreading code to test just a single file and isolate the problem.
My Client data send subroutine looks like this:
Private Sub SendData() Dim filebuffer As Byte() Dim fileStream As Stream ' open a file and retrieve its bytes fileStream = File.OpenRead(tbFilename.Text) ReDim filebuffer(fileStream.Length) fileStream.Read(filebuffer, 0, fileStream.Length) ' Open a TCP Connection and send the data Dim clientSocket As New System.Net.Sockets.TcpClient(tbServer.Text, 8222) clientSocket.SendBufferSize = 4096 clientSocket.SendTimeout = 5000 '5 seconds Dim networkStream As NetworkStream networkStream = clientSocket.GetStream() networkStream.Write(filebuffer, 0, fileStream.Length) networkStream.Flush() networkStream.Close() clientSocket.Close() End Sub
The relevant Server code looks like this:
Public Sub handlerThread() Try Dim handlerSocket As Socket 'define the socket locally handlerSocket = alSockets(alSockets.Count - 1) 'get the last socket added to the alSockets array handlerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000) Dim networkStream As NetworkStream = New NetworkStream(handlerSocket) 'create a network stream Dim blockSize As Integer = 4096 ' Dim thisRead As Integer Dim dataByte(blockSize) As Byte SyncLock Me Dim fileStream As Stream fileStream = File.OpenWrite("c:\zzzzzz " + RandObj.Next.ToString + ".jpg") 'Make a new file to save to Do thisRead = networkStream.Read(dataByte, 0, blockSize) fileStream.Write(dataByte, 0, thisRead) Loop While networkStream.DataAvailable fileStream.Close() End SyncLock lbConnections.Items.Add("File Written") handlerSocket = Nothing Catch ex As Exception MsgBox("Error was : " + ex.Message) End Try End Sub
I am pulling my hair out as to what might be the problem here. I have tried various socket options, block sizes, disabled the firewalls on both machines, many things really; I dont know where to go next. I have a project file with both components if that is of any help.
If there is anybody able to offer suggestions about what I am doing wrong in my code it would be greatly appreciated.
.NET Development9
|