Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

Socket Programming

Hi Experts


            I am using socket programming  to send and recv data. I have applied cheksum but my check sum always get failed if I recieved bad data.Please help me if you have any idea.source code is given below:

DATA sdata;
      char buff[PAC_LEN] ;
      unsigned short chk = 0;
      int nRet = 0;
      ipacketno =0;
      while(TRUE)
      {
            memset(buff,0x00,sizeof(buff));
            nRet = recv(Soc,buff,sizeof(DATA),0);
            memset(&sdata,0x00,sizeof(DATA));
            memcpy(&sdata,buff,sizeof(DATA));
            if(dlength >= 8192)
                  chk = csum((unsigned short *)sdata.bBuffer,8192 >> 1);
            else
                  chk = csum((unsigned short *)sdata.bBuffer,dlength >> 1);
            if((chk == sdata.checksum ) && (sdata.dwPacket == (ipacketno + 1)) &&  sdata.dwPacket <= iTotalPacket)
            {
                  if(dlength >= 8192)
                  {
                        memcpy(buffer + i64totalbytewritten,sdata.bBuffer,8192);
                        dlength = dlength - 8192;
                        i64totalbytewritten = i64totalbytewritten + 8192;                        
                  }
                  else
                  {
                        memcpy(buffer + i64totalbytewritten,sdata.bBuffer,dlength);
                        i64totalbytewritten = i64totalbytewritten + dlength;
                        dlength = 0;                        
                  }
                  ipacketno = ipacketno + 1;
                  if(ipacketno == iTotalPacket)
                  {
                        nRet = send(Soc,"COMP",4,0);
                        break;
                        
                  }
                  
            }
            else
            {
                  nRet = send(Soc,"FAIL",4,0);      
                  if(nRet == -1)
                  {
                        CheckConnection();
                        shutdown(Soc,SD_BOTH);

                  }
                  break;
                  
            }
            if(nRet == - 1)
            {      
                  CheckConnection();            
                  break;
            }      
      }
      return 1;
0
davinder101
Asked:
davinder101
  • 2
  • 2
1 Solution
 
mrwad99Commented:
>> I have applied cheksum but my check sum always get failed if I recieved bad data

Surely that is what you want to happen?  If you don't receive valid data then your checksum calculation should fail.  Maybe I am not understanding you properly however, so can you elaborate on what your code is supposed to be doing in some more details?
0
 
davinder101Author Commented:
I am reading the data from disk and sending it to another machine. It work fines but ones it fails for a paticular sector. I request for same sector. The checksum keep failing for that sector.Check sum fails even if both the exe are on the same machine. Is this a programming error or network issue.
0
 
mrwad99Commented:
Is the data of the same type as that on the sectors for which it works?  Is the disc on the same physical machine (in particular, is it the same OS?)

Compare the data received to the data sent, byte by byte to see if you can see any difference.  You may need to step through your code to see exactly why the checksum is failing.
0
 
sarabandeCommented:
the following line

nRet = recv(Soc,buff,sizeof(DATA),0);

Open in new window


will not reliable read all data to buff.

the tcp/ip could send a message in portions. do handle that you would do like

total = 0;
nread = 0;
while (total < sizeof(DATA))
{
    nread = recv(Soc,((char*)&buff)+total,sizeof(DATA) - total,0);
    if (nread == -1)
    {
             /* handle socket error */
             ...
             break;
    }
    total += nread;
} 

Open in new window


generally, i wouldn't use short type anymore and if endianess is an issue you also need to convert/reconvert integers by htons/ntohs for short and htonl/ntohl for int.

Sara
0
 
davinder101Author Commented:
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now