LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2006, 08:46 AM   #1
ebsosoft
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Rep: Reputation: 0
socketing problem


Hi,

my project is VOIP. I'm beginer in programming.I use 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 be 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(Target))==-1)
{
perror("\n Data socket cannot be binded to OS\n");
Exit(-1);
}

g=sizeof(Target);

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: (syntax error before struct )

I tried too much to repair this error but I cannot. I beginer in programming,please can you help me, thank you
 
Old 03-25-2006, 09:00 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Please post all code in code tags ([ code ] this is code [ / code ] -- without the spaces) if you want people to read it (and please don't post anything you don't want people to read). Also, your question is related to programming, so it really belongs in the Programming (sub)forum.

grovel through the headers in decreasing order of relevance to see if they define the relevant struct (sockaddr_in, right?). 'grep -C 4 sockaddr_in <filename>' might come in handy.
 
Old 03-25-2006, 02:44 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

It'd be helpful to have the program in code tags, because it's now hard to find out which line is 43. It looks to me that's
Code:
j=recvfrom(sockfd,buf,10,0,(struct sockaddr*
&Target,&g);
There's a simple mistake, no ). It should be
Code:
j=recvfrom(sockfd,buf,10,0,(struct sockaddr*)&Target,&g);
 
Old 03-26-2006, 06:10 PM   #4
ebsosoft
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Hi,
I'm sorry for asking again.
my project is VOIP. I'm beginer in programming.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 be 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 I adjust in this code as you tell me,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(Target))==-1)
{
perror("\n Data socket cannot be binded to OS\n");
Exit(-1);
}

g=sizeof(Target);

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 42 this error is: (warning: pointer targets in passing argument 6 of 'recvfrom' differ in signedness )

I tried too much to repair this error but I cannot. I beginer in programming,please can you help me, thank you
 
Old 03-26-2006, 06:19 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Change "int g" to
Code:
"unsigned int g"
... or ...

Change "j=recvfrom(sockfd,buf,10,0,(struct sockaddr*)&Target,&g);" to
Code:
j=recvfrom(sockfd,buf,10,0,(struct sockaddr*)&Target,(unsigned int *)&g);
Just FYI:
Quote:
NAME
recv, recvfrom, recvmsg - receive a message from a socket

SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>

ssize_t recv(int s, void *buf, size_t len, int flags);

ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockâ
addr *from, socklen_t *fromlen);


ssize_t recvmsg(int s, struct msghdr *msg, int flags);
<= On your platform, "socklen_t" happens to be unsigned, and the C/C++
standard dictates that an int pointer should be flagged as "incompatible" with
an unsigned int pointer, hence the need to cast (unsigned int *)
 
Old 03-29-2006, 07:53 AM   #6
ebsosoft
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Socketing Problem

Tanks for you very very much for helping me to correct the error in my codes,but when I running this programs (sender&receiver),message appear at the bottom of debugger window:

((no debugging symbols found)using host libthread_db library "/lib/libthread_db.so.1")

I cannot understand this message.Are this message affect on running of my programs?.Please can you help me,thank you.
 
Old 03-29-2006, 03:31 PM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The debugger can't find symbol list for the library. It's understandable (they take space) and should not cause any harm (only if something fails in that library - debugging may be harder).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration