Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.arcetri.astro.it/irlab/doc/library/linux/khg/HyperNews/get/khg/61.html
Дата изменения: Thu Mar 23 12:38:07 2000 Дата индексирования: Wed Sep 15 00:47:50 2010 Кодировка: Поисковые слова: п п п п п п п п п п п п |
I have to write a client server program using raw socket. I have written the code for client as well as server but when ever I run it my server hangs up. So I have to reboot the server. I think there is problem with my send and receive. I am sending the code for server. Hope you would be able to help me.
@@@@@@@@@@@@@@@@@@@@@@@ code @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #include <stdio.h>
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h>
#include <netinet/ip.h> #include <netinet/ip_icmp.h> u_short portbase = 0; long time();
#define qlen 6 #define protocol "raw" #ifdef REALLY_RAW #define FIX(x) htons(x) #else #define FIX(x) (x) #endif
main(int argc, char **argv) { int msock, ssock; int alen; char buf[] = "asdfgh"; char recv_buffer[20]; struct servent *pse; struct protoent *ppe; struct sockaddr_in dst; struct hostent *hp; struct ip *ip = (struct ip *)buf; struct icmp *icmp = (struct icmp *)(ip +1);
int s, type, dstL; int q, bind1, lis; int sockopt; int on = 1, address; int offset; int sendbuff; int n; bzero((char *)&dst, sizeof(dst)); dst.sin_family = AF_INET;
dst.sin_port = 6000; ppe = getprotobyname("raw");
setbuf(stdout,NULL); s = socket(AF_INET, SOCK_RAW, 0); printf("\n%d value of s in servsock",s); if (s < 0) printf("\nCann't creat socket");
setbuf(stdout,NULL);
sockopt = setsockopt(s, 0, IP_HDRINCL, &on, sizeof(on)); printf("\n%d value of sockopt", sockopt); if (sockopt < 0) exit(0);
if(( hp = gethostbyname(argv[1])) == NULL){ if(ip->ip_dst.s_addr = inet_addr(argv[1]) == -1) printf("\nERROR: UNKNOWN HOST"); } else bcopy(hp->h_addr_list[0], &ip->ip_dst.s_addr, hp->h_length); printf("\nSending to %s\n", inet_ntoa(ip->ip_dst));
ip->ip_v = 4; fflush(stdin); ip->ip_hl = sizeof *ip >> 2; ip>ip_tos = 0; ip->ip_len = sizeof buf; ip->ip_id = htons(4321); ip->ip_off = 0; ip->ip_ttl = 255; ip->ip_p = 1; ip->ip_sum = 0; ip->ip_src.s_addr = 0; dst.sin_addr = ip->ip_dst; dst.sin_family = AF_INET;
icmp->icmp_type = ICMP_ECHO; icmp->icmp_code = 0; sendbuff = sendto(s, buf, sizeof buf, 0, (struct sockaddr *) &dst, sizeof dst); if(sendbuff < 0) printf(" ERROR sending "); if ( sendbuff != sizeof buf) printf("ERROR packet size"); printf("\n buf is %s value of send is %d ", buf, sendbuff);
dstL = sizeof dst; n = recvfrom(s, recv_buffer, sizeof(recv_buffer), 0, (struct sockaddr *) &dst,&dstL); printf("recv buffer is%s value of n is %d\n", recv_buffer,n); close(s); exit(0); } @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|