Question : Socket Programming - Problem with size of files received on Listening Server

Hello,

We are in process to develop an application to receive & store data files from our clients using Socket programming (the clients will push the files at the supplied IP & Port no.)..We have completed writing SERVER side code to receive the files and this code will be running continuously in listening mode..

Along with the Server application, we have also built a trial CLIENT application to push the files on the server for testing...During debugging the application, we observed that it sends the complete file contents in single command

The problem we are facing is that our SERVER side code is not able to receive the complete contents of the files sent by the client..sometimes it is successful in receiving & storing whole file contents but sometimes it fails to do so & somehow receives only partial data...

Pleas let us know about the problem in the following code -

' ---- Start CLIENT code to push files to the listening Server ----
Imports System
Imports System.Collections.Generic
Imports System.Net
Imports System.Net.Dns
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
 
Private clientsock As Socket
Private strfileName As String
Private fileNameByte As Byte()
Private fileData As Byte()
Private fileNameLen As Byte()
Private SizeofFile As Integer
Private ipEnd As IPEndPoint
Private arripAddress As IPAddress()
Private portno As Integer


Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Try
            Dim clientData As Byte()
            clientsock = New Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.IP)
            strfileName = selctfile.FileName

            fileNameByte = Encoding.ASCII.GetBytes(strfileName)
            fileData = selctfile.FileBytes
            fileNameLen = BitConverter.GetBytes(fileNameByte.Length)
            clientData = New Byte(4 + fileNameByte.Length + (fileData.Length - 1)) {}
            fileNameLen.CopyTo(clientData, 0)
            fileNameByte.CopyTo(clientData, 4)
            fileData.CopyTo(clientData, 4 + fileNameByte.Length)

            ' Size of file
            SizeofFile = clientData.Length()

            ipEnd = New IPEndPoint(arripAddress(0), portno)
            clientsock.Connect(ipEnd)

            Dim out As Integer = 0
            out = clientsock.Send(clientData, clientData.Length, 0)

        Finally
            clientsock.Close()
        End Try
End Sub
' ---- End CLIENT code to push files to the listening Server ----




' ---- Start SERVER code to receive files on the listening Server ----
Imports System
Imports System.Collections.Generic
Imports System.Net
Imports System.Net.Dns
Imports System.Net.Sockets
Imports System.IO
Imports System.Text

    Private hostname As String
    Private arripAddress As IPAddress()
    Private strPortNo As String
    Private portno As Integer
    Private ipEnd As IPEndPoint
    Private serverSock As Socket
    Private receivedPath As String
    Private clientSock As Socket
    Private clientIpEnd As IPEndPoint
    Private strClientIpAddress As String
    Private clientData As Byte()
    Private receivedBytesLen As Integer
    Private fileNameLen As Integer
    Private fileName As String
    Private strSizeofFile As String
    Private bWrite As BinaryWriter
      
    Sub Main()
        GoToListen()
    End Sub
 
    Private Sub GoToListen()

        Try
            hostname = Dns.GetHostName()
            arripAddress = Dns.GetHostAddresses(hostname)
            strPortNo = System.Configuration.ConfigurationManager.AppSettings("PortNo")
            portno = Convert.ToInt64(strPortNo)
            ipEnd = New IPEndPoint(IPAddress.Parse(arripAddress(0).ToString()), portno)

            serverSock = New Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.IP)
            receivedPath = System.Configuration.ConfigurationManager.AppSettings("ReceivedFullPath")
            serverSock.Bind(ipEnd)
            serverSock.Listen(100)

            clientSock = serverSock.Accept()

            clientIpEnd = clientSock.RemoteEndPoint()
            strClientIpAddress = clientIpEnd.Address.ToString()

            ' Receiving the data from client & Filename,type as well as size of file
            clientData = New Byte(800000 - 1) {}
            receivedBytesLen = 0
            receivedBytesLen = clientSock.Receive(clientData)
                  
                  ' 5 seconds delay between receiving the contents & writing them in file
            System.Threading.Thread.Sleep(5000)

            fileNameLen = BitConverter.ToInt32(clientData, 0)
            fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen)

            ' Size of the File
            strSizeofFile = receivedBytesLen.ToString()

            ' Writing & saving the file
            bWrite = New BinaryWriter(File.Open(receivedPath + fileName,FileMode.Create))
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen)
         
        Catch sockException As SocketException
            MsgBox("Error in Connection " + sockException.Message)
        Finally
            serverSock.Close()
            bWrite.Close()
            clientSock.Close()
            GoToListen()
        End Try
    End Sub
' ---- End SERVER code to receive files on the listening Server ----

Answer : Socket Programming - Problem with size of files received on Listening Server

the recieve buffer is too big and the process is terminating before the client can supply the next block of data

try this

    Function ReceiveData(ByVal skt As Socket) As MemoryStream
        'create a memorystream to hold the incoming data
        Dim ms As New MemoryStream
        While True
            'refresh the buffer
            Dim buf = New Byte(1023) {}

            'recieve some data
            Dim br = skt.Receive(buf, buf.Length, SocketFlags.None)

            'write the data into the memorystream
            ms.Write(buf, 0, br)

            'if the number of bytes is smaller than the buffer capacity we're at the end of the stream
            If br < buf.Length Then Exit While

            'give the client a chance to send more data (10 milliseconds)
            Wait(10)
        End While
        Return ms
    End Function

    Sub Wait(ByVal i As Integer)
        Dim st As New Stopwatch
        st.Start()
        Do Until st.Elapsed.TotalMilliseconds > i
            Application.DoEvents()
        Loop
        st.Stop()
        st = Nothing
    End Sub
Random Solutions  
 
programming4us programming4us