Solved

Sockets Question

Posted on 2006-11-27
7
235 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
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
Comment Utility

>> 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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

8 Experts available now in Live!

Get 1:1 Help Now