hi..i am new to network prog in c++...this thing is really eatn my mind...ive created code for creating a tcp syn packet.....sending part is fine...but cannot recieve.....also when i use IPPROTO_TCP during socket description sending also fails.......the source code is till recvfrom..since it returns 0 and not the incoming packet size....plz help..
the source code:

#include <stdio.h>

#include <iostream.h>
#include <cstdio>
#include <winsock2.h>
#include <windows.h>
#include<conio.h>
#include<ws2tcpip.h>
// define the tcp flags....
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TCP_ACE 0x40
#define TCP_CWR 0x80


//define header lengths

#define ETHER_LENGTH 14
#define IP_LENGTH 20
#define TCP_LENGTH 20
#define UDP_LENGTH 8
#define ICMP_LENGTH 8
#define ARP_LENGTH 28
#define PSEUDO_LENGTH 12
#define DATA_LENGTH 32


//Protocol
#define PROTO_ICMP 1
#define PROTO_IGMP 2
#define PROTO_TCP 6
#define PROTO_UDP 17


struct ip_header
{
unsigned char ver_ihl; // Version (4 bits) + Internet header length (4 bits)
unsigned char tos; // Type of service
unsigned short tlen; // Total length
unsigned short identification; // Identification
unsigned short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
unsigned char ttl; // Time to live
unsigned char proto; // Protocol
unsigned short crc; // Header checksum
unsigned int saddr; // Source address
unsigned int daddr; // Destination address

// u_int op_pad; // Option + Padding
};
struct tcp_header //20 bytes
{
unsigned short sport; //Source port
unsigned short dport; //Destination port
unsigned long seqno; //Sequence no
unsigned long ackno; //Ack no
unsigned char offset; //Higher level 4 bit indicates data offset
unsigned char flag; //Message flag
//FIN - 0x01
//SYN - 0x02
//RST - 0x04
//PUSH- 0x08
//ACK- 0x10
//URG- 0x20
//ACE- 0x40
//CWR- 0x80

unsigned short win;
unsigned short checksum;
unsigned short uptr;
};

struct pseudo_header //12 bytes
{
unsigned int saddr; // Source address
unsigned int daddr; // Destination address
unsigned char zero;
unsigned char proto; // Protocol
unsigned short tcp_len;
tcp_header tcp;
};
unsigned short ComputeChecksum(u_short *data,int size);

int main()
{
WSADATA wsaData;



int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);



if(iResult != NO_ERROR)

printf("Error at WSAStartup().\n");

else

printf("WSAStartup() is OK.\n");



char *ip1="10.1.77.163";


SOCKET sock;
sock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW ); //Create a raw socket which will use ICMP

SOCKADDR_IN dest; //Dest address to send the ICMP request
dest.sin_addr.S_un.S_addr = inet_addr (ip1);
dest.sin_family = AF_INET;
dest.sin_port =htons (80) ;

SOCKADDR_IN dest1;
int len=sizeof(dest1);
fd_set fdRead;

int nResult;
ip_header ih,*rih;
tcp_header th,*rth;
pseudo_header psh;

for(int j=0;j<4;j++)
{
char frame[100] ;
memset(frame,0,100);
memset(&ih,0,sizeof(ih));

//source address


//destination address
ih.saddr=inet_addr(ip1);
ih.daddr=inet_addr(ip1);
ih.proto=PROTO_TCP;

ih.flags_fo=htons(0x4000); //For TCP Flag fixed
// ih.flags_fo=htons(0x0000); //For ICMP Flag fixed
ih.identification=htons(0x0150); //Any number


ih.tlen=htons(IP_LENGTH+TCP_LENGTH); //Depends upon data and tcp protocol
ih.tos=0;
ih.ttl=128;
ih.ver_ihl=0x45; //Version (v4) and header length(5 nibbles)
ih.crc=0;
ih.crc=ComputeChecksum((unsigned short *)&ih,IP_LENGTH);

memcpy(&frame,&ih,IP_LENGTH);




/*************** TCP Header ****************************/


memset(&th,0,sizeof(th));
th.sport=rand(); // source port
th.dport=htons(21); // dest port changes dynamically
th.ackno=htonl(0);
th.seqno=htonl(5555l);
th.flag=TCP_SYN; // SYN packet
th.offset=0x50; // data offset
th.uptr=0;
th.win=htons(512) ;
th.checksum=0;

//set up the pseudo header
memset(&psh,0,sizeof(psh));
psh.saddr=ih.saddr;
psh.daddr=ih.daddr;
psh.proto=ih.proto;
psh.zero=0;
psh.tcp_len=htons(TCP_LENGTH); //Length of Tcp header + Data in OCTATES
memcpy(&psh.tcp,&th,TCP_LENGTH);

th.checksum=ComputeChecksum((unsigned short*)&psh,TCP_LENGTH+PSEUDO_LENGTH);


memcpy(&frame[IP_LENGTH],&th,TCP_LENGTH);







nResult = sendto (sock, frame, IP_LENGTH+TCP_LENGTH, 0, (SOCKADDR *)&dest, sizeof (SOCKADDR_IN));

if (nResult == SOCKET_ERROR)
{
printf( "An error occured in sendto operation: " );


}
else
printf("\npacket %d sent and size is:%d",j,nResult);

}





char frame1[5000] ;


int nResult1;
if ( nResult1 = recvfrom (sock, frame1, 5000, 0, (SOCKADDR *)&dest1, &len) == SOCKET_ERROR)
{
printf( "An error occured in recvfrom operation: ");
//nResult1 is 0 and not the incoming packet size.

}

printf("\nsize:%d",nResult1);

rih=(ip_header*)(frame1);
if(rih->proto==PROTO_TCP)
{
rth=(tcp_header*)((u_char*)rih+ (rih->ver_ihl & 0x0f)*4);



printf("got reply");


}
getch();
}
unsigned short ComputeChecksum(unsigned short *data,int size)
{
register int nleft=size;
register u_short *w=data;
register int sum=0;
unsigned short answer =0;



while(nleft>1)
{
sum+=*w++;
nleft-=2;
}


if(nleft==1)
{

*(u_char*) (&answer) = *(u_char*)w;
sum+=answer;
}


sum= (sum>>16 )+ (sum & 0xffff);
sum+=(sum>>16);
answer=~sum;
return (answer);

}