I need to send multiple request (command) to server at port 55005.
My first request get process successfully & I received out put also.
But for second request it gives error (WSAESHUTDOWN - Error 10058). 
After first requets I call 
    shutdown(ConnectSocket, SD_SEND);
Then only server process the first request & send me output.
Now can re-open socket processing next request ?**

How I can process multiple request after shutdown(ConnectSocket, SD_SEND) ?
Thanks in Adavance for your suggestions.



#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <windows.h>
#include <vector>
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 55005

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

//These command / request process by server.
void GetCommandList(std::vector<std::string> & OutCommandList)
{
    std::string SendBuf  = "UE->,evG_Home_MainScreen";
    OutCommandList.push_back(SendBuf);

    SendBuf  = "UE->,evG_Home_AudioSel";
    OutCommandList.push_back(SendBuf);

    SendBuf  = "UE->,evG_Audio_MediaChange";
    OutCommandList.push_back(SendBuf);


    SendBuf  = "UE->,evG_MyMedia_Browse";
    OutCommandList.push_back(SendBuf);


    SendBuf  = "UE->,evG_MyMedia_Artist_Browse";
    OutCommandList.push_back(SendBuf);
}


int main() {

    //----------------------
    // Declare and initialize variables.
    int iResult;
    WSADATA wsaData;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService; 

    int recvbuflen = DEFAULT_BUFLEN;
     char recvbuf[DEFAULT_BUFLEN] = "";


    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    clientService.sin_port = htons( DEFAULT_PORT );

    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if (iResult == SOCKET_ERROR) {
        wprintf(L"connect failed with error: %d\n", WSAGetLastError() );
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
  }


    //get the command request which we want send to severr. 
    std::vector<std::string> CommandList;
    std::vector<std::string>::iterator it;
    GetCommandList(CommandList);

    **//This for loop will send multiple request to Servers.**
    for(it = CommandList.begin(); it != CommandList.end() ; it++ )
    {
        //get each command request & send it to server.
        std::string  sendBuf; // =  (*it);
            sendBuf= *it;
        int length = (int)strlen(sendBuf.c_str());
        //----------------------
        // Send an initial buffer
        iResult = send( ConnectSocket, (char*)sendBuf.c_str(), length, 0 );
        if (iResult == SOCKET_ERROR) {
            wprintf(L"send failed with error: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }

        printf("Bytes Sent: %d\n", iResult);


        // shutdown the connection since no more data will be sent
        iResult = shutdown(ConnectSocket, SD_SEND);
        if (iResult == SOCKET_ERROR) {
            wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }

        // Receive until the peer closes the connection
        do {

            iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
            if ( iResult > 0 )
                wprintf(L"Bytes received: %d\n", iResult);
            else if ( iResult == 0 )
                wprintf(L"Connection closed\n");
            else
                wprintf(L"recv failed with error: %d\n", WSAGetLastError());

        } while( iResult > 0 );
    }

    // close the socket
    iResult = closesocket(ConnectSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"close failed with error: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    WSACleanup();
    return 0;
}

shutdown tell to server first request is received & process it. So server can reply to my first request. Till this it look good. But After Shutdown How I can send my next request ?

You need to make a new socket object again (after calling close). Avoid calling shutdown on the socket until you've truly finished with it as there is no method to re-enable send/receive on it after they have been disabled.

If you wish to use the socket again, call DisconnectEx on it with the TF_REUSE_SOCKET flag. This will disconnect the socket but retain the handle so that you can call ConnectEx/AcceptEx again.

Thanks for reply. DisconnectEx work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.