ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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:
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
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....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.