[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

[no subject]



hi ,
im new to the list..i am trying to do some amt of socket programming here on raw sockets. i am basically new to linux and C cause i mostly wrkd on microcontrollers and microprocessors on assembly and was somewhat out of touch with C.. well my prob is simple.. i am enclosing the server and clienrt file


1st the client :

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>


#define MAXDATASIZE 4096

int main (int argc, char *argv[])
{  int flag=1;
  int sockfd,numbytes;
  char buf[MAXDATASIZE];
  struct hostent *he ;
  struct sockaddr_in their_addr ;

 if (argc !=2) {
  fprintf (stderr, "usage: client hostname\n");
  exit (1);
 }

 if ((he=gethostbyname(argv[1]))==NULL) {
 perror ("gethostbyname");
 exit (1);
 }

if ((sockfd=socket (PF_INET,SOCK_RAW,13))==-1) {
  perror ("socket");
  exit (1);
 }

their_addr.sin_family=PF_INET;
/*their_addr.sin_port = htons(PORT);*/
their_addr.sin_addr=*((struct in_addr*)he->h_addr);
bzero (&(their_addr.sin_zero),8);
/*
if (connect (sockfd, (struct sockaddr*)&their_addr, sizeof(struct sockaddr))==-1) {
  perror ("connect");
  exit (1);
 }*/
while (flag==1)

sendto (sockfd,"hello",6,0,&their_addr,sizeof (struct sockaddr));
/*if ((numbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1) {
  perror ("recv");
  exit (1);
 }

buf[numbytes]='\0' ;
printf ("Received: %s\n",buf);
close (sockfd);

return 0;*/
}

---------------------------------------------------
now the server side
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>


#define MAXDATASIZE 4096

main()
{
 int sockfd,new_fd ;
 struct sockaddr_in my_addr;
 struct sockaddr_in their_addr ;
 int sin_size;
 int numbytes=0 ; int flag=1;
 char buf[MAXDATASIZE] ;
 if ((sockfd=socket (PF_INET , SOCK_RAW , 13))==-1) {
 perror ("socket");
 exit (1);
 }

my_addr.sin_family=PF_INET ;
my_addr.sin_addr.s_addr=INADDR_ANY ;
bzero (&(my_addr.sin_zero),8) ;

if (bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))== -1) {
 perror ("bind");
 exit (1);
 }

while (flag==1)
{ printf ("am listening\n");
numbytes=recvfrom(sockfd,buf,MAXDATASIZE,0,(struct sockaddr*)&their_addr,&sin_size);
printf ("got connection from %s \n",inet_ntoa(their_addr.sin_addr));
buf[numbytes]='\0' ;
printf ("Received %s , %d\n",buf[2],numbytes ) ;
flag=1;
}

}





my problem is my recived bytes always returns "E ,26" no matter what i send.. is mesage at the ip layer not in regular text format?.. is what i am trying possible? .. i have a similar code using TCP sockets (SOCK_STREAM) and SOCK_DGRAM and all is fine with them.. any idea whts different here?
Alok



________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com