• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Sockets Question

Hi guys,

We are developing a little script which transfers some files with the help of sockets, but it seems to be a bit slow...

My question is this:


Are sockets known to be slow, or is it possibly that our code has something wrong in it?

Cheers guys!
0
Cyber-Drugs
Asked:
Cyber-Drugs
  • 4
  • 2
1 Solution
 
bdunz19Commented:
My guess is your code has something wrong within. Sockets are what basically power all application connections. I wouldn't know how to connect to anything without using a socket (besides winpcap). My current project has the capability of uploading and downloading files. I used a TCP socket stream and it maxes out my bandwidth. I would post some of your recieve/send code and I'll be able to tell if you are doing something really wrong.
0
 
Cyber-DrugsAuthor Commented:
Well we're only after some basic file upload/download...

I'm going to upload the project to EE-stuff.com and let you have a look at it, maybe you can point out the problems, or even provide a better solution. :)

Cheers!
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Cyber-DrugsAuthor Commented:
Sorry, just another note...

We managed to get it to transfer quite fast, but it crashes the Server.exe


If you can't find the problem, could I maybe modify the version you had, as we're basically after moving files from computerA to computerB, nothing fancy...


Cheers!
0
 
mxjijoCommented:

>> Are sockets known to be slow ?
No. sockets are fast ! :)

Its hard to say what is wrong unless you post your code, explain your network/server/local conditions.
What script do you use ? As you can assume, scripts has its own way of socket handling.
So it can be slower than a C++ application.
But, if you are doing only a simple upload/download, scripts shouldn't make a huge difference.

I would recomment you to use populat tools like FTP clients, wget etc first to see how that performs.

best
~J
0
 
bdunz19Commented:
Hi Cyber,

I'm just going jump into things and we'll see if this is how you are designing your file transfers!

Do you have any sort of protocol that you have implemented? If you have then create some sort of command or identifyer to alert the server that you are about to upload or download a file. Then once the server recieves that request it knows to either recv() or send() your data files.

Once the server knows what your doing, I'd implement somecode like this in your client:

// Socket creation, connection, protocol requests, ect here..
...

// Now time to start downloading/uploading:

long recvsize = 0;
char szBuf[1025] = "";

if(m_bupload) // Should we start uploading a file?
{
      FILE *pFile = NULL;
      pFile = fopen(m_filename, "rb");
      if(pFile != NULL)
      {
            int bufsize;
            ULONG filesize = 0; // Total size to reed
            ULONG cursize = 0; // Current size read
            fseek(pFile, 0, SEEK_END);
            filesize = ftell(pFile);
            fseek(pFile, 0, SEEK_SET);
            while(!feof(pFile))
            {
                  bufsize = fread(szBuf, 1, 1024, pFile);
                  cursize += bufsize;
                  if(send(sock, szBuf, bufsize, 0) == SOCKET_ERROR)
                  {
                        error = "- Error Sending Data\r\n";
                        fclose(pFile);
                        shutdown(sock, SD_SEND);
                        closesocket(sock);
                        return FALSE;
                  }
                  m_progress = (USHORT)(((float)cursize/(float)filesize)*100.0);
            }
            fclose(pFile);
            shutdown(sock, SD_SEND);
      }
}
else if(m_bdownload) // Should we start downloading a file?
{
      FILE *pFile = NULL;
      pFile = fopen(m_filename, "wb");
      if(pFile != NULL)
      {
            ULONG cursize = 0; // Current size read
            while((recvsize = recv(sock, szBuf, 1024, 0)) > 0)
            {
                  fwrite(szBuf, 1, recvsize, pFile);
                  cursize += recvsize;
                  m_progress = (USHORT)(((float)cursize/(float)m_filesize)*100.0);
            }
            fclose(pFile);
      }
      else
      {
            error = "- File Write Access Error\r\n";
            m_error = 1;
            closesocket(sock);
            return FALSE;
      }
}

shutdown(sock, SD_BOTH);
closesocket(sock);

Now once you have an idea of what the client should be doing, you can simply reverse the order for the server. So when the client is performing a recv() the server better be doing the oposite with a send().

As you can see, the data is written as it is being recieved or sent as it is being read. I was wondering if youmight have been performing somthing other than that and would cause low performance...

Anyway, let me know if you have any questions!

Good luck,
Brandon
0
 
Cyber-DrugsAuthor Commented:
We managed to create a work-around for what we were after, but thanks for sharing that bit of code, it most likely will come in handy for something else in the near future, cheers!
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now