LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 04-01-2006, 10:28 AM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 12.1, 2.6.24.5
Posts: 185

Rep: Reputation: 30
problem with bind()


Hello!

1)When I compile the code below, which is from the BSD IPC tutorial, I get this warning:

Quote:
warning: passing arg 2 of 'bind' from incompatible pointer type
Why does it not like it?

2)Also, when I run the program, it just prints out:

Quote:
socket --> socket
and then just sits doing nothing without exiting.

Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>

#define NAME "socket"

int main(void)
{
       int sock, length;
       struct sockaddr_un name;
       char buf[1024];

       sock = socket(AF_UNIX, SOCK_DGRAM, 0);
       if(sock < 0)
       {
             perror("opening datagram socket");
             exit (1);
       }

       name.sun_family = AF_UNIX;
       strcpy(name.sun_path, NAME);
       if(bind(sock, &name, sizeof(struct sockaddr_un)))
       {
             perror("binding name to datagram socket");
             exit (1);
       }

       printf(socket --> %s\n", NAME);

       if(read(sock, buf, 1024) < 0)
       {
             perror("receiving datagram packet");
       }

       printf("--> %s\n", buf);
       close(sock);
       unlink(NAME);

       return 0;
}
 
Old 04-01-2006, 03:13 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,408

Rep: Reputation: 108Reputation: 108
The warning comes because second bind() argument should be const struct sockaddr *. Add a cast and it should be OK.

One warning, however. You know that you won't receive anything when you're calling read() just after bind()?
 
Old 04-01-2006, 04:01 PM   #3
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 12.1, 2.6.24.5
Posts: 185

Original Poster
Rep: Reputation: 30
Thanks Mara, I have added a cast and the warning does not come up any more.

Quote:
You know that you won't receive anything when you're calling read() just after bind()?
What do you mean? Is this to do with my second problem? Because, like I've mentioned before, the program prints out the 'NAME' and then does nothing else. It will not print the value of 'buf' variable. I do not understand why.
 
Old 04-01-2006, 05:27 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
For starters, the code you posted shouldn't even compile (the first fatal error is the missing quote in your "printf"). Here are the errors I get:
Code:
x.c: In function `main':
x.c:24: warning: passing arg 2 of `bind' from incompatible pointer type
x.c:30: error: wrong type argument to decrement
x.c:30: error: parse error before '%' token
x.c:30: error: stray '\' in program
x.c:30:30: missing terminating " character
x.c: At top level:
x.c:37: error: parse error before string constant
x.c:37: warning: conflicting types for built-in function `printf'
x.c:37: warning: data definition has no type or storage class
x.c:38: warning: parameter names (without types) in function declaration
x.c:38: warning: data definition has no type or storage class
x.c:39: error: parse error before string constant
x.c:39: warning: data definition has no type or storage class
What Mara is (correctly) saying is that you typically need to do "something else" before you start reading from a socket. For example, a TCP/IP server might "bind()", "listen()" and "accept()" incoming connections. A TCP/IP client would "connect()". A UDP client could simply do a "recvfrom()" or "sendto ()". But you're not doing any of these things!

And I'm not familiar with using AF_UNIX in conjunction with SOCK_DGRAM. Are you sure that's what you want to do?

For whatever it's worth, here's a tutorial on simple, standard UDP sockets:

http://gnosis.cx/publish/programming/sockets2.html

If you're programming IPCs on Linux, you might want to consider the SysV IPCs (message queues, shared memory, sysV semaphores and friends):

http://www.tldp.org/LDP/lpg/node21.html

'Hope that helps .. PSM

Last edited by paulsm4; 04-01-2006 at 05:33 PM.
 
Old 04-01-2006, 11:01 PM   #5
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 47
yes as everyone else has said for a server (read) you
Code:
bind()
listen(socket,5);
do {
  struct sockaddr_un client_addr;
  socklen_t client_len;
  int client_fd;
  client_fd = accept(socket,&client_addr,&client_len)
  read_junk(client_fd);// write another method here to implement reading from the socket
  } while (); //have to figure out how to get out of this
 
Old 04-03-2006, 03:12 AM   #6
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Sorry everybody but i think the thread is going in a little wrong direction...
hubabuba is writing UDP socket program as he is using SOCK_DGRAM. So he didn't need to listen and accept connection...

The problem is that if you look at the code after printing
socket --> socket
The code executes the read() statement. The read is a blocking call. It means that when you call the read() your program goes to halt state until it reads some data... So when you say that your program is doing nothing... In fact at that time your program is waiting for some input on the socket... Now it is just one part of the program... You need to write another part that will send the data to that socket.. Now when your program will get the data it will display the data as mentioned in the code....
 
Old 04-03-2006, 04:24 AM   #7
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 12.1, 2.6.24.5
Posts: 185

Original Poster
Rep: Reputation: 30
Thank you everyone, I can see what I need to do in order to get the program running and terminate properly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bind problem gubak Linux - Networking 13 07-22-2004 02:40 AM
Bind problem WiWa Linux - Networking 4 06-03-2004 09:53 AM
problem with bind krimson Linux - Networking 3 01-22-2004 06:50 PM
problem with bind krimson Linux - Software 0 01-19-2004 05:42 PM
problem bind 9.xx Rana Saud Linux - Networking 1 07-15-2003 11:45 AM


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

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration