Solved

Sockets Question

Posted on 2006-11-27
7
239 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
  • 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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18023168
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 170
how do i create updater to My Activex application? 3 79
changeXy challenge 13 67
Excel file not created as expected 7 49
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now