?
Solved

Sockets Question

Posted on 2006-11-27
7
Medium Priority
?
264 Views
Last Modified: 2013-11-20
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
Comment
Question by:Cyber-Drugs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:bdunz19
ID: 18023014
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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18023082
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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18023208
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
 
LVL 8

Expert Comment

by:mxjijo
ID: 18023529

>> 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
 
LVL 4

Accepted Solution

by:
bdunz19 earned 2000 total points
ID: 18024197
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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18029536
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question