Re: ok why do i loose my socket - Programmer

This is a discussion on Re: ok why do i loose my socket - Programmer ; Hi, I doesn't compile this, but I have some comments > help me find ware my sockt went > > // main prog > #include > #include > #include > > struct PACKETFRAME > > > int num1; > int ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Re: ok why do i loose my socket

  1. Re: ok why do i loose my socket


    Hi,

    I doesn't compile this, but I have some comments

    > help me find ware my sockt went
    >
    > // main prog
    > #include
    > #include
    > #include
    >
    > struct PACKETFRAME
    >
    >
    > int num1;
    > int num2;
    > char junk[50];
    > };
    >
    > typedef struct _PER_CLIENT_DATA
    > {
    > SOCKET socket;
    > OVERLAPPED Overlapped;
    > CHAR OutboundBuffer[1024];
    > DWORD flags;
    > HANDLE hEvent;
    > }
    > PER_CLIENT_DATA, *PPER_CLIENT_DATA;
    > file://struct DATAFRAME
    > file://{
    > file://BYTE flag;
    > file://DWORD data;
    > file://}
    >
    > main(void)
    > {
    > WSADATA WsaData;
    > DWORD i;
    > SOCKET mylisten;
    > SOCKADDR_IN addr;
    > PER_CLIENT_DATA clientData[5];
    > CHAR data[sizeof(PACKETFRAME)];
    > DWORD err;
    > PACKETFRAME thing2;
    > char buffy[50];
    > SOCKET sockbak;
    > file://REM KEEP GAP HERE
    > printf("progrm start \n");
    > WSAStartup (0x0101, &WsaData);
    > mylisten = socket (AF_INET, SOCK_STREAM, 0);
    > ZeroMemory (&addr, sizeof (addr));
    > addr.sin_port = htons(1701);
    > addr.sin_family = AF_INET;
    > bind (mylisten,(PSOCKADDR) & addr, sizeof (addr));
    > listen (mylisten,5);

    Only 5? you can use "SOMAXCONN"

    > for (i=0; i<500; i++)
    > {
    > clientData[i].socket= accept(mylisten,NULL,NULL);
    > printf("we have connected socket %d \n",clientData[i].socket);
    > err = WSAGetLastError();
    > printf("socket %d \n",clientData[i].socket);
    > sockbak = clientData[i].socket;

    Why this? You can use clientData[i].socket

    > err = recv (sockbak,(char *)data,512,0);

    The Variable 'data' hasn't a size of 512 bytes, the size is
    sizeof(PACKETFRAME)
    err = ? and now? use the possible error values
    err = recv(..)
    if( SOCKET_ERROR != err)
    {
    //it's all ok, go on
    }
    else
    {
    err = WSAGetLastError(); //to get the errorcode
    }

    > printf("socket %d \n",clientData[i].socket);
    > memcpy((char*)&thing2,data,sizeof(PACKETFRAME));
    > printf("i receved %d %d %s \n",thing2.num1,thing2.num2,thing2.junk);
    > printf("socket %d \n",clientData[i].socket);
    > err = WSAGetLastError();

    this is to late, many other things can change the global errorvalue, see
    above

    > printf("error # %d \n",err);
    > sprintf(buffy,"you sent me %s",thing2.junk);
    > strcpy(thing2.junk,buffy);

    Oh! First you "copy" thing2.junk + some text into buffy and then you copy
    this back to thing2.junk. This is possible / mostly larger than the size of
    thing2.junk

    > memcpy(data,(char*)&thing2,sizeof(PACKETFRAME));
    > printf("socket %d \n",clientData[i].socket);
    > sockbak = clientData[i].socket;
    > err = send (sockbak,(char*)data,512,0);

    see above, data hasn't a size of 512 bytes

    > printf("socket %d \n",clientData[i].socket);
    > // err = send (mylisten,(char*)data,512,0);
    > printf("%d err=%d",err,WSAGetLastError());
    > printf("packet sent\n");
    > }
    > }
    >


    Regards Frank



  2. Re: ok why do i loose my socket


    > > sockbak = clientData[i].socket;

    > Why this? You can use clientData[i].socket

    trying to bakup the socket
    > > err = recv (sockbak,(char *)data,512,0);

    the problem is just after this command ware my acepted socket discrpter just
    goes null!!!


    il resolve all other problems when they arise



  3. Re: ok why do i loose my socket

    > > > printf("socket %d \n",clientData[i].socket);
    > > > sockbak = clientData[i].socket;

    > > Why this? You can use clientData[i].socket

    > i was trying to back up the socket discriptor (did not work)
    > > > err = recv (sockbak,(char *)data,512,0);

    > and i loose the socket discriptor here it just goes null(both sockbak and
    > clientData[i].socket
    >

    data hasn't a size of 512 bytes! --> sizeof(PACKETFRAME) !!
    (possible) overwriting of other memory than the reserved memory for "data"



+ Reply to Thread