slow down by socket

Posted on 2005-05-16
Last Modified: 2012-05-05
Hi Experts,

After I add a simple socket client code to my original program, the original one seems slow down.
The original one is used to upload data from one DB to another.
And the code for socket is just to send several strings.

Have I missing somewhere to make the speed low, originally the upload speed is quite fast.

Thanks and Regards,


      // Initialize Winsock.
            WSADATA wsaData;
            int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
            if ( iResult != NO_ERROR )
                  printf("Error at WSAStartup()\n");

            // Create a socket.
            SOCKET m_socket;
            m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
            if ( m_socket == INVALID_SOCKET ) {
                  printf( "Error at socket(): %ld\n", WSAGetLastError() );
                  // Connect to a server.
                  sockaddr_in clientService;
                  clientService.sin_family = AF_INET;
                  clientService.sin_addr.s_addr = inet_addr( IP_ADDRESS_PQHUB );
                  clientService.sin_port = htons( PORT_PQHUB );

                  if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
                        printf( "Failed to connect.\n" );
                  else {//connection is successful                        
                        bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
                        printf( "Bytes Sent: %ld, %s \n", bytesSent, sendbuf );
Question by:turbot_yu
    LVL 39

    Assisted Solution

    Actually the code you posted could not be responsible to slow down the program if it runs once. If it runs in a loop, of course it could decrease overall performance, especially if you are calling WSAStartup, socket(), connect(), WSACleanup() any time you are sending. Note, these calls should happen once and only once, best in the constructor of a socket class. Also printf calls as well as debug mode could make as program significantly slower than before, but that wasn't determinable from the code you posted.

    Regards, Alex
    LVL 4

    Accepted Solution


    go to my site i have hosted free code

    go to free code webpage where i have full socket program which has both client and server

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    759 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