Submitted by abbasco on Sat, 25/03/2006 - 17:54.
( categories: )

Hi,

I'm sorry for asking again. I operate on Fedora Core 4.I wrote socket program to send data other computer and this is code:


 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include<unistd.h>
 #include<netinet/in.h>
 #include<sys/wait.h>
 #include<signal.h>

 #define DATA_PORT 5014

int main(){
int j;
int sockfd;
struct sockaddr_in Target,Source;
     Source.sin_family=AF_INET;
     Source.sin_addr.s_addr=inet_addr("192.168.0.2");
     Source.sin_port=htons(0);
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
 {
 printf ("\n Socket for data msgs cannot created\n");
 exit(-1);
 }
if(bind(sockfd,(struct sockaddr*)&Source,sizeof(Source))==-1)
 { 
perror("\n Data socket cannot be binded to OS\n");
 exit(-1);
 }
 Target.sin_family=AF_INET;
 Target.sin_port=htons(DATA_PORT);
 Target.sin_addr.s_addr=inet_addr("192.168.0.4");

while(1)
{
printf("sent ok\n");
j=sendto(sockfd,"alah akbar",10,0,(struct sockaddr*)&Target, sizeof(Target));
}
return 0;
}

and no error in this code

then I wrote socket program to receive the data from the first computer and this is code:


#include <stdio.h>              
#include <string.h>                          
#include <stdlib.h>                        
#include <assert.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>

#define DATA_PORT 5000
int buf[10];                                
int g;

int main()                              
{                           
int j;            
int sockfd;            
struct sockaddr_in Target;
  Target.sin_family=AF_INET; 
  Target.sin_port=htons(DATA_PORT);
  Target.sin_addr.s_addr=inet_addr("192.168.0.3");   
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)   
{                                               
printf("\n Socket for data msgs cannot be created\n");
exit(-1);                                   
}                                                     

if(bind(sockfd,(struct sockaddr*)&Target,sizeof(struct sockaddr))==-1)
{                                            
perror("\n Data socket cannot be binded to OS\n");    
Exit(-1);                                             
}                                       

g=sizeof(struct sockaddr);

while(1)
{                                         
j=recvfrom(sockfd,buf,10,0,(struct sockaddr*
&Target,&g);                                         
printf("%s\n",buf);
}

return 0;   
}

but this code has error in line 43 this error is: (line 43:warning:pointer targets in passing argument 6 of recvfrom differ in signdness )

I tried too much to repair this error but I cannot. I beginer in programming,please can you help me, thank you


to make your code readable

Conceptor's picture
to make your code readable on the forum use pre tag or before code {{{ and }}} after your code
{{{
my code
}}}

Diaa Radwan

doesn't work.

MSameer's picture

The HTML filter is stripping what's between the &lt; &amp; &gt;

And the wiki syntax filter is interpreting the hash (#).

I enabled the code filter module but it didn't work.


WWW: The place for organized randoms!

code input format should

Conceptor's picture

code input format should work well :(.


Diaa Radwan

nope not true, when you

Alaa's picture

nope not true, when you enclose in

 
it escapes everything perfectly under wiki syntax

I think code filter should be mixed with wiki

the problem is in html filter, this is the one that refuses to play nice

cheers,
Alaa


http://www.manalaa.net "context is over-rated. who are you anyway?"

remove the cast

MSameer's picture

should be: j = recvfrom (sockfd, buf, 10, 0, &Target, &g);

I suggest you google for "beej network programming tutorial" and read it.


WWW: The place for organized randoms!

MMM

zxeem's picture

I actually dunno if you couldn't solve this till now or what ,, but ,, here what I found


this line is totally worng as u forgot the comma ,

printf("%s\n"buf);

correction: printf("%s\n",buf);


this line is also worng

j=recvfrom(sockfd,buf,10,0,(struct sockaddr* &Target,&g);

you didn't complete the casting parenthes () ,, it shall be

j=recvfrom(sockfd,buf,10,0,(struct sockaddr*) &Target,&g);


a sematic error ,, is to assign an output of the sizeof() operator to int ,, it is an operator not a function ,,

so here is the error prone code fixed :

while(1) { j=recvfrom(sockfd,buf,10,0,(struct sockaddr*) &Target,sizeof(Target)); printf("%s\n",buf); }


Oh by the way ,, the Exit function ,, the e is small not capital E ,,

so it is exit(-1) not Exit(-1)

,,,,,,,, C language is Case sensetive as u know ,, try harder ,,

Try using cc or gcc not an IDE ,,

Rabena yewafa2ak in sha2 ALLAH,,


Zxeem*

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.