We help IT Professionals succeed at work.
Get Started

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

335 Views
Last Modified: 2012-08-13
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 ----
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE