Multithreading in c++

Discussion in 'Software' started by naren, Jun 27, 2007.

  1. naren

    naren

    Joined:
    Sep 5, 2006
    Messages:
    1
    hi i am using the below code for server side so that a dummy communication between the client and server can be simulated. but only few client get response back other do not get response from server do anyoone help me
    sane type of code is used for client



    DWORD dwStatus;
    WSAEVENT handles[1];
    struct sockaddr_in cli_addr;
    int clilen = sizeof(cli_addr);
    memset(&cli_addr,0,sizeof(struct sockaddr));

    printf("***********server started****************\n");
    while (TRUE)
    {

    handles[0] = WSACreateEvent();
    WSAEventSelect(m_hSockethndl, handles[0], FD_ACCEPT);
    dwStatus = WSAWaitForMultipleEvents(1, handles, TRUE, 50, FALSE);
    if (dwStatus == WSA_WAIT_EVENT_0)
    {
    SOCKET cli_socket;
    cli_socket=-1;
    cli_socket = accept(m_hSockethndl, (struct sockaddr *) &cli_addr, &clilen);

    // set linger option
    LINGER linger;
    linger.l_onoff = 1;
    linger.l_linger = 0;
    setsockopt(cli_socket, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(LINGER));
    //setsockopt(cli_socket, IPPROTO_TCP, TCP_NODELAY, NULL, 0);

    int flag = 1;
    int result = setsockopt(cli_socket, /* socket affected */
    IPPROTO_TCP, /* set option at TCP level */
    TCP_NODELAY, /* name of option */
    (char *) &flag, /* the cast is historical cruft */
    sizeof(int)); /* length of option value */
    if (result < 0)
    {
    printf("Client Socket No Delay Option Failed");
    }

    //printf("Thread started with the server socket=%d and client port=%d and ip=%s\n",cli_socket,cli_addr.sin_port,inet_ntoa(cli_addr.sin_addr));

    CreateThread(NULL, 0, StartThread, &cli_socket, 0, &dwThreadID);
    FD_SET(cli_socket, &set_sockets);
    }
    WSACloseEvent(handles[0]);
    }




    //thread function

    DWORD dwStatus;
    WSAEVENT handles[1];
    struct sockaddr_in cli_addr;
    int clilen = sizeof(cli_addr);
    memset(&cli_addr,0,sizeof(struct sockaddr));

    printf("***********server started****************\n");
    while (TRUE)
    {

    handles[0] = WSACreateEvent();
    WSAEventSelect(m_hSockethndl, handles[0], FD_ACCEPT);
    dwStatus = WSAWaitForMultipleEvents(1, handles, TRUE, 50, FALSE);
    if (dwStatus == WSA_WAIT_EVENT_0)
    {
    SOCKET cli_socket;
    cli_socket=-1;
    cli_socket = accept(m_hSockethndl, (struct sockaddr *) &cli_addr, &clilen);

    // set linger option
    LINGER linger;
    linger.l_onoff = 1;
    linger.l_linger = 0;
    setsockopt(cli_socket, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(LINGER));
    //setsockopt(cli_socket, IPPROTO_TCP, TCP_NODELAY, NULL, 0);

    int flag = 1;
    int result = setsockopt(cli_socket, /* socket affected */
    IPPROTO_TCP, /* set option at TCP level */
    TCP_NODELAY, /* name of option */
    (char *) &flag, /* the cast is historical cruft */
    sizeof(int)); /* length of option value */
    if (result < 0)
    {
    printf("Client Socket No Delay Option Failed");
    }

    //printf("Thread started with the server socket=%d and client port=%d and ip=%s\n",cli_socket,cli_addr.sin_port,inet_ntoa(cli_addr.sin_addr));
    /*threadParmas thparam;
    memset(&thparam,0,sizeof(threadParmas));
    thparam.cli_addrth=cli_addr;
    thparam.threadsocket=cli_socket;*/

    //CreateThread(NULL, 0, StartThread, &thparam, 0, &dwThreadID);
    CreateThread(NULL, 0, StartThread, &cli_socket, 0, &dwThreadID);
    FD_SET(cli_socket, &set_sockets);
    }
    WSACloseEvent(handles[0]);
    }
    }
    //functionality to be done by each Server thread for it's client.
    DWORD WINAPI thread::StartThread (void * thparam)
    {
    SOCKET socket_handle;
    socket_handle=*(SOCKET *)thparam;
    char data[256];
    char Resdata[512];
    memset(data,0,256);
    memset(Resdata,0,512);
    int counter=1;
    int bytes =1;

    while(bytes)
    {

    WSAEVENT we = WSACreateEvent();
    WSAEVENT handles[1];
    handles[0] = we;
    WSAEventSelect(socket_handle, we, FD_READ | FD_CLOSE);
    //receiving data from client
    for (int i = 0; i < WAIT_MULTIPLE; i++)
    {
    DWORD available = WSAWaitForMultipleEvents(1, handles, TRUE, WAIT_TIME, FALSE);
    if (available == WSA_WAIT_EVENT_0)
    {
    if ((bytes = recv(socket_handle, data, 256, 0)) < 0)
    {
    int er = WSAGetLastError();
    printf("Error=%d while receive data from socket %d\n",er,socket_handle);
    bytes = 0;
    break;
    }
    else
    break;
    }
    }

    WSACloseEvent(we);

    strcpy_s(Resdata,512,"Response send Ack");

    if(bytes)
    {

    //sending the dummy response to the client
    if (send(socket_handle, Resdata, strlen(Resdata), 0) != strlen(Resdata))
    {
    int er = WSAGetLastError();
    printf("ERROR %d IN SENDING %d RESPONSE by socket =%d\n",er,counter,socket_handle);
    return 0;
    }

    printf("***Response %3d send by the server socket=%d\n",counter,socket_handle);
    }
    //keeping track how many packets this specific client sends
    counter++;
    }
     
    naren, Jun 27, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mohammad_nazari

    multithreading

    mohammad_nazari, May 7, 2011, in forum: Software
    Replies:
    0
    Views:
    882
    mohammad_nazari
    May 7, 2011
Loading...

Share This Page